diff --git a/api/serializers.py b/api/serializers.py index 40c3af08..2db33761 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -5,6 +5,7 @@ from api import models from api import search_indexes + class ManifestSerializer(serializers.ModelSerializer): class Meta: model = models.Manifest @@ -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(',') @@ -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 = ( @@ -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() @@ -207,7 +215,7 @@ class Meta: 'document__url' ) -class BackgroundSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer): +class BackgroundSerializer(DynamicFieldsHyperlinkedModelSerializer): class Meta: model = models.Background fields = ( @@ -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 = ( @@ -246,7 +254,7 @@ class Meta: 'parent' ) -class FeatSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer): +class FeatSerializer(DynamicFieldsHyperlinkedModelSerializer): class Meta: model = models.Feat fields = ( @@ -260,7 +268,7 @@ class Meta: 'document__url' ) -class ConditionSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer): +class ConditionSerializer(DynamicFieldsHyperlinkedModelSerializer): class Meta: model = models.Condition fields = ( @@ -272,7 +280,7 @@ class Meta: 'document__url' ) -class SubraceSerializer(serializers.HyperlinkedModelSerializer): +class SubraceSerializer(DynamicFieldsHyperlinkedModelSerializer): class Meta: model = models.Subrace fields = ('name', @@ -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 @@ -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 @@ -352,7 +360,7 @@ class Meta: 'document__url' ) -class MagicItemSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer): +class MagicItemSerializer(DynamicFieldsHyperlinkedModelSerializer): class Meta: model = models.MagicItem fields = ( @@ -367,7 +375,7 @@ class Meta: 'document__url' ) -class WeaponSerializer(serializers.HyperlinkedModelSerializer): +class WeaponSerializer(DynamicFieldsHyperlinkedModelSerializer): class Meta: model = models.Weapon fields = ( @@ -384,7 +392,7 @@ class Meta: 'weight', 'properties') -class ArmorSerializer(serializers.HyperlinkedModelSerializer): +class ArmorSerializer(DynamicFieldsHyperlinkedModelSerializer): class Meta: model = models.Armor fields = (