Skip to content

Commit

Permalink
Merge pull request #371 from open5e/v2_better_race_view
Browse files Browse the repository at this point in the history
V2 better race view
  • Loading branch information
augustjohnson authored Nov 4, 2023
2 parents b2c6b38 + cb493fb commit 157ad5e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
11 changes: 5 additions & 6 deletions api_v2/models/race.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,16 @@ class Race(HasName, HasDescription, FromDocument):
null=True,
on_delete=models.CASCADE)

@property
def subraces(self):
"""Returns the set of subraces that are related to this race."""
return self.race_set.all()

@property
def is_subrace(self):
"""Returns whether the object is a subrace."""
return self.subrace_of is not None

@property
def is_selectable(self):
"""Returns whether or not this is a choosable race or subrace."""
# Returns true if this has 0 referencing children.
return len(self.race_set.all()) == 0

@property
def traits(self):
"""Returns the set of traits that are related to this race."""
Expand Down
1 change: 1 addition & 0 deletions api_v2/serializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .feat import FeatSerializer

from .race import TraitSerializer
from .race import SubraceSerializer
from .race import RaceSerializer

from .creature import CreatureSerializer
Expand Down
10 changes: 9 additions & 1 deletion api_v2/serializers/race.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@ class Meta:
model = models.Trait
fields = ['name', 'desc']

class SubraceSerializer(GameContentSerializer):
key = serializers.ReadOnlyField()
traits = TraitSerializer(
many=True)
class Meta:
model = models.Race
fields = '__all__'

class RaceSerializer(GameContentSerializer):
key = serializers.ReadOnlyField()
is_subrace = serializers.ReadOnlyField()
is_selectable = serializers.ReadOnlyField()
subraces = SubraceSerializer(many=True, context={'request': {}})

traits = TraitSerializer(
many=True)

Expand Down
3 changes: 2 additions & 1 deletion api_v2/views/race.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Meta:
'key': ['in', 'iexact', 'exact'],
'name': ['iexact', 'exact'],
'document__key': ['in', 'iexact', 'exact'],
'subrace_of': ['isnull'],
'subrace_of__key':['in', 'iexact', 'exact'],
}


Expand All @@ -24,4 +26,3 @@ class RaceViewSet(viewsets.ReadOnlyModelViewSet):
queryset = models.Race.objects.all().order_by('pk')
serializer_class = serializers.RaceSerializer
filterset_class = RaceFilterSet

0 comments on commit 157ad5e

Please sign in to comment.