Skip to content

Commit

Permalink
Merge pull request #535 from calumbell/feature/v2-search-fields-for-C…
Browse files Browse the repository at this point in the history
…haracterClass

V2 Feature: `/search` now returns more helpful data
  • Loading branch information
augustjohnson authored Sep 16, 2024
2 parents f7305bf + e1651f0 commit ed22a65
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 11 deletions.
8 changes: 8 additions & 0 deletions api_v2/models/characterclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,12 @@ def as_text(self):

return text

def search_result_extra_fields(self):
return {
"subclass_of": {
"name": self.subclass_of.name,
"key": self.subclass_of.key
} if self.subclass_of else None
}

#TODO add verbose name plural
8 changes: 4 additions & 4 deletions api_v2/models/creature.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ def as_text(self):

def search_result_extra_fields(self):
return {
"armor_class":self.armor_class,
"hit_points":self.hit_points,
"ability_scores":self.get_ability_scores(),
}
"cr": self.challenge_rating_text,
"type": self.type.name,
"size": self.size.name,
}

@property
def creatureset(self):
Expand Down
10 changes: 5 additions & 5 deletions api_v2/models/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ def is_magic_item(self):
return self.rarity is not None

def search_result_extra_fields(self):
fields = {"type":self.category.key}
if self.is_magic_item:
fields["rarity"]=self.rarity.key
return fields

return {
"is_magic_item": self.is_magic_item,
"type": self.category.name if self.is_magic_item else None,
"rarity": self.rarity.name if self.is_magic_item else None,
}


class ItemSet(HasName, HasDescription, FromDocument):
Expand Down
7 changes: 7 additions & 0 deletions api_v2/models/race.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ def traits(self):
"""Returns the set of traits that are related to this race."""
return self.racetrait_set

def search_result_extra_fields(self):
return {
"subrace_of": {
"name": self.subrace_of.name,
"key": self.subrace_of.key
} if self.subrace_of else None
}
class Meta:
"""To assist with the UI layer."""

Expand Down
7 changes: 6 additions & 1 deletion api_v2/models/spell.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,12 @@ def get_range_unit(self):
return self.document.distance_unit
return self.range_unit


def search_result_extra_fields(self):
return {
"school": self.school.name,
"level": self.level,
}

class SpellCastingOption(models.Model):
"""An object representing an alternative way to cast a spell."""

Expand Down
6 changes: 5 additions & 1 deletion api_v2/serializers/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,18 @@ def get_object(self, obj):
result_detail = v1.Spell.objects.get(slug=obj.object_pk)
if obj.object_model == 'Section':
result_detail = v1.Section.objects.get(slug=obj.object_pk)

if obj.schema_version == 'v2':
if obj.object_model == 'Item':
result_detail = models.Item.objects.get(pk=obj.object_pk)
if obj.object_model == 'Creature':
result_detail = models.Creature.objects.get(pk=obj.object_pk)
if obj.object_model == 'Spell':
result_detail = models.Spell.objects.get(pk=obj.object_pk)
if obj.object_model == 'CharacterClass':
result_detail = models.CharacterClass.objects.get(pk=obj.object_pk)
if obj.object_model == 'Race':
result_detail = models.Race.objects.get(pk=obj.object_pk)

if result_detail is not None:
return result_detail.search_result_extra_fields()
Expand Down

0 comments on commit ed22a65

Please sign in to comment.