Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement dynamic field serialization on endpoints that lack it #314

Merged
merged 6 commits into from
Aug 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from api import models
from api import search_indexes


class ManifestSerializer(serializers.ModelSerializer):
class Meta:
model = models.Manifest
Expand All @@ -17,7 +18,7 @@ def __init__(self, *args, **kwargs):
super(DynamicFieldsModelSerializer, self).__init__(*args, **kwargs)

# The request doesn't exist when generating an OAS file, so we have to check that first
if self.context['request']:
if 'request' in self.context:
fields = self.context['request'].query_params.get('fields')
if fields:
fields = fields.split(',')
Expand All @@ -27,12 +28,19 @@ def __init__(self, *args, **kwargs):
for field_name in existing - allowed:
self.fields.pop(field_name)

class DynamicFieldsHyperlinkedModelSerializer(
DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer
):
"""Abstract base class to be inherited by Serializers that both use
dynamic fields as well as hyperlinked relationships."""
pass

class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'username', 'email', 'groups')

class DocumentSerializer(serializers.HyperlinkedModelSerializer):
class DocumentSerializer(DynamicFieldsHyperlinkedModelSerializer):
class Meta:
model = models.Document
fields = (
Expand All @@ -54,7 +62,7 @@ class Meta:
model = Group
fields = ('url', 'name')

class MonsterSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer, serializers.ModelSerializer):
class MonsterSerializer(DynamicFieldsHyperlinkedModelSerializer):

speed = serializers.SerializerMethodField()
environments = serializers.SerializerMethodField()
Expand Down Expand Up @@ -207,7 +215,7 @@ class Meta:
'document__url'
)

class BackgroundSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer):
class BackgroundSerializer(DynamicFieldsHyperlinkedModelSerializer):
class Meta:
model = models.Background
fields = (
Expand All @@ -227,12 +235,12 @@ class Meta:
'document__url'
)

class PlaneSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer):
class PlaneSerializer(DynamicFieldsHyperlinkedModelSerializer):
class Meta:
model = models.Plane
fields = ('slug','name','desc','document__slug', 'document__title', 'document__url')

class SectionSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer):
class SectionSerializer(DynamicFieldsHyperlinkedModelSerializer):
class Meta:
model = models.Section
fields = (
Expand All @@ -246,7 +254,7 @@ class Meta:
'parent'
)

class FeatSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer):
class FeatSerializer(DynamicFieldsHyperlinkedModelSerializer):
class Meta:
model = models.Feat
fields = (
Expand All @@ -260,7 +268,7 @@ class Meta:
'document__url'
)

class ConditionSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer):
class ConditionSerializer(DynamicFieldsHyperlinkedModelSerializer):
class Meta:
model = models.Condition
fields = (
Expand All @@ -272,7 +280,7 @@ class Meta:
'document__url'
)

class SubraceSerializer(serializers.HyperlinkedModelSerializer):
class SubraceSerializer(DynamicFieldsHyperlinkedModelSerializer):
class Meta:
model = models.Subrace
fields = ('name',
Expand All @@ -286,7 +294,7 @@ class Meta:
'document__url'
)

class RaceSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer):
class RaceSerializer(DynamicFieldsHyperlinkedModelSerializer):
subraces = SubraceSerializer(many=True,read_only=True)
class Meta:
model = models.Race
Expand Down Expand Up @@ -325,7 +333,7 @@ class Meta:
'document__url'
)

class CharClassSerializer(serializers.HyperlinkedModelSerializer):
class CharClassSerializer(DynamicFieldsHyperlinkedModelSerializer):
archetypes = ArchetypeSerializer(many=True,read_only=True)
class Meta:
model = models.CharClass
Expand All @@ -352,7 +360,7 @@ class Meta:
'document__url'
)

class MagicItemSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer):
class MagicItemSerializer(DynamicFieldsHyperlinkedModelSerializer):
class Meta:
model = models.MagicItem
fields = (
Expand All @@ -367,7 +375,7 @@ class Meta:
'document__url'
)

class WeaponSerializer(serializers.HyperlinkedModelSerializer):
class WeaponSerializer(DynamicFieldsHyperlinkedModelSerializer):
class Meta:
model = models.Weapon
fields = (
Expand All @@ -384,7 +392,7 @@ class Meta:
'weight',
'properties')

class ArmorSerializer(serializers.HyperlinkedModelSerializer):
class ArmorSerializer(DynamicFieldsHyperlinkedModelSerializer):
class Meta:
model = models.Armor
fields = (
Expand Down