Skip to content

Commit

Permalink
Adding some properties and filters.
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildTools committed Nov 4, 2023
1 parent 4859e8c commit 764fd6c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
14 changes: 14 additions & 0 deletions api_v2/models/alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,17 @@ class Alignment(HasName, HasDescription, FromDocument):
"""This is the model for an alignment, which is way to describe the
moral and personal attitudes of a creature."""

@property
def short_name(self):
short_name = ""
for word in self.name.split(" "):
short_name += word[0].upper()
return short_name

@property
def morality(self):
return self.name.split(" ")[-1].lower()

@property
def societal_attitude(self):
return self.name.split(" ")[0].lower()
5 changes: 4 additions & 1 deletion api_v2/serializers/alignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

class AlignmentSerializer(GameContentSerializer):
key = serializers.ReadOnlyField()
morality = serializers.ReadOnlyField()
societal_attitude = serializers.ReadOnlyField()
short_name = serializers.ReadOnlyField()

class Meta:
model = models.Alignment
fields = '__all__'
fields = '__all__'
2 changes: 1 addition & 1 deletion api_v2/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@

from .language import LanguageFilterSet, LanguageViewSet

from .alignment import AlignmentViewSet
from .alignment import AlignmentFilterSet, AlignmentViewSet
15 changes: 12 additions & 3 deletions api_v2/views/alignment.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
from rest_framework import viewsets

from django_filters import FilterSet

from api_v2 import models
from api_v2 import serializers


class AlignmentFilterSet(FilterSet):
class Meta:
model = models.Alignment
fields = {
'key': ['in', 'iexact', 'exact' ],
'name': ['iexact', 'exact','contains'],
'document__key': ['in','iexact','exact'],
}

class AlignmentViewSet(viewsets.ReadOnlyModelViewSet):
"""
list: API endpoint for returning a list of alignments.
retrieve: API endpoint for returning a particular alignment.
"""
queryset = models.Alignment.objects.all().order_by('pk')
serializer_class = serializers.AlignmentSerializer



filterset_class = AlignmentFilterSet

0 comments on commit 764fd6c

Please sign in to comment.