Skip to content

Commit bd8f9a6

Browse files
authored
[BUGFIX] Languages field on Creatures endpoint now work as intended (open5e#698)
* rmv'd extra languages when language_desc contained substring 'all' or 'any' * added CreatureLanguageSerializer/LanguageSummarySerializer for handling Creature languages * updated tests
1 parent 9e9c83e commit bd8f9a6

File tree

15 files changed

+241
-3381
lines changed

15 files changed

+241
-3381
lines changed

api_v2/serializers/creature.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from .damagetype import DamageTypeSerializer
1111
from .condition import ConditionSerializer
1212
from .document import DocumentSummarySerializer
13-
from .language import LanguageSerializer
13+
from .language import LanguageSummarySerializer
1414
from .environment import EnvironmentSerializer
1515
from .size import SizeSerializer
1616
from drf_spectacular.utils import extend_schema_field, inline_serializer
@@ -56,6 +56,14 @@ class Meta:
5656
model = models.CreatureTrait
5757
fields = '__all__'
5858

59+
class CreatureLanguageSerializer(GameContentSerializer):
60+
as_string = serializers.CharField(source="languages_desc")
61+
data = LanguageSummarySerializer(source="languages", many=True)
62+
63+
class Meta:
64+
model = models.Creature
65+
fields = ['as_string', 'data']
66+
5967
class CreatureSerializer(GameContentSerializer):
6068
'''The serializer for the Creature object.'''
6169

@@ -79,7 +87,7 @@ class CreatureSerializer(GameContentSerializer):
7987
document = DocumentSummarySerializer()
8088
type = CreatureTypeSerializer()
8189
size = SizeSerializer()
82-
languages = LanguageSerializer(many=True)
90+
languages = CreatureLanguageSerializer(source='*')
8391
environments = EnvironmentSerializer(many=True)
8492
initiative_bonus = serializers.SerializerMethodField()
8593

api_v2/serializers/language.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,14 @@ class LanguageSerializer(GameContentSerializer):
1313
class Meta:
1414
model = models.Language
1515
fields = '__all__'
16+
17+
class LanguageSummarySerializer(GameContentSerializer):
18+
"""
19+
This serializer is used for FKs to the Language model from other
20+
serializers. ie. for the languages spoken by creatures on the
21+
CreatureSerializer.
22+
"""
23+
key = serializers.ReadOnlyField()
24+
class Meta:
25+
model = models.Language
26+
fields = ['name', 'key', 'url', 'desc']

api_v2/tests/responses/TestObjects.test_creature_ancient_example.approved.json

Lines changed: 16 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -252,56 +252,23 @@
252252
"hit_points": 546,
253253
"initiative_bonus": 0,
254254
"key": "srd_ancient-red-dragon",
255-
"languages": [
256-
{
257-
"desc": "Typical speakers are Humans.",
258-
"document": {
259-
"gamesystem": {
260-
"key": "o5e",
261-
"name": "5th Edition",
262-
"url": "http://localhost:8000/v2/gamesystems/o5e/"
263-
},
264-
"key": "srd",
265-
"name": "Systems Reference Document",
266-
"permalink": "https://dnd.wizards.com/resources/systems-reference-document",
267-
"publisher": {
268-
"key": "wizards-of-the-coast",
269-
"name": "Wizards of the Coast",
270-
"url": "http://localhost:8000/v2/publishers/wizards-of-the-coast/"
271-
}
255+
"languages": {
256+
"as_string": "Common, Draconic",
257+
"data": [
258+
{
259+
"desc": "Typical speakers are Humans.",
260+
"key": "common",
261+
"name": "Common",
262+
"url": "http://localhost:8000/v2/languages/common/"
272263
},
273-
"is_exotic": false,
274-
"is_secret": false,
275-
"key": "common",
276-
"name": "Common",
277-
"script_language": "http://localhost:8000/v2/languages/common/",
278-
"url": "http://localhost:8000/v2/languages/common/"
279-
},
280-
{
281-
"desc": "Typical speakers include dragons and dragonborn.",
282-
"document": {
283-
"gamesystem": {
284-
"key": "o5e",
285-
"name": "5th Edition",
286-
"url": "http://localhost:8000/v2/gamesystems/o5e/"
287-
},
288-
"key": "srd",
289-
"name": "Systems Reference Document",
290-
"permalink": "https://dnd.wizards.com/resources/systems-reference-document",
291-
"publisher": {
292-
"key": "wizards-of-the-coast",
293-
"name": "Wizards of the Coast",
294-
"url": "http://localhost:8000/v2/publishers/wizards-of-the-coast/"
295-
}
296-
},
297-
"is_exotic": true,
298-
"is_secret": false,
299-
"key": "draconic",
300-
"name": "Draconic",
301-
"script_language": "http://localhost:8000/v2/languages/draconic/",
302-
"url": "http://localhost:8000/v2/languages/draconic/"
303-
}
304-
],
264+
{
265+
"desc": "Typical speakers include dragons and dragonborn.",
266+
"key": "draconic",
267+
"name": "Draconic",
268+
"url": "http://localhost:8000/v2/languages/draconic/"
269+
}
270+
]
271+
},
305272
"modifiers": {
306273
"charisma": 6,
307274
"constitution": 9,

api_v2/tests/responses/TestObjects.test_creature_goblin_example.approved.json

Lines changed: 16 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -242,56 +242,23 @@
242242
"hit_points": 7,
243243
"initiative_bonus": 2,
244244
"key": "srd_goblin",
245-
"languages": [
246-
{
247-
"desc": "Typical speakers are Humans.",
248-
"document": {
249-
"gamesystem": {
250-
"key": "o5e",
251-
"name": "5th Edition",
252-
"url": "http://localhost:8000/v2/gamesystems/o5e/"
253-
},
254-
"key": "srd",
255-
"name": "Systems Reference Document",
256-
"permalink": "https://dnd.wizards.com/resources/systems-reference-document",
257-
"publisher": {
258-
"key": "wizards-of-the-coast",
259-
"name": "Wizards of the Coast",
260-
"url": "http://localhost:8000/v2/publishers/wizards-of-the-coast/"
261-
}
245+
"languages": {
246+
"as_string": "Common, Goblin",
247+
"data": [
248+
{
249+
"desc": "Typical speakers are Humans.",
250+
"key": "common",
251+
"name": "Common",
252+
"url": "http://localhost:8000/v2/languages/common/"
262253
},
263-
"is_exotic": false,
264-
"is_secret": false,
265-
"key": "common",
266-
"name": "Common",
267-
"script_language": "http://localhost:8000/v2/languages/common/",
268-
"url": "http://localhost:8000/v2/languages/common/"
269-
},
270-
{
271-
"desc": "Typical speakers are goblinoids.",
272-
"document": {
273-
"gamesystem": {
274-
"key": "o5e",
275-
"name": "5th Edition",
276-
"url": "http://localhost:8000/v2/gamesystems/o5e/"
277-
},
278-
"key": "srd",
279-
"name": "Systems Reference Document",
280-
"permalink": "https://dnd.wizards.com/resources/systems-reference-document",
281-
"publisher": {
282-
"key": "wizards-of-the-coast",
283-
"name": "Wizards of the Coast",
284-
"url": "http://localhost:8000/v2/publishers/wizards-of-the-coast/"
285-
}
286-
},
287-
"is_exotic": false,
288-
"is_secret": false,
289-
"key": "goblin",
290-
"name": "Goblin",
291-
"script_language": "http://localhost:8000/v2/languages/dwarvish/",
292-
"url": "http://localhost:8000/v2/languages/goblin/"
293-
}
294-
],
254+
{
255+
"desc": "Typical speakers are goblinoids.",
256+
"key": "goblin",
257+
"name": "Goblin",
258+
"url": "http://localhost:8000/v2/languages/goblin/"
259+
}
260+
]
261+
},
295262
"modifiers": {
296263
"charisma": -1,
297264
"constitution": 0,

0 commit comments

Comments
 (0)