From d5b9130d8f5297750d12692c52e0da235d443101 Mon Sep 17 00:00:00 2001 From: Gresh <> Date: Sat, 5 Aug 2023 12:38:33 -0400 Subject: [PATCH 1/6] Created DynamicFieldsHyperlinkedModelSerializer base class --- api/serializers.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/api/serializers.py b/api/serializers.py index 40c3af08..63340999 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 @@ -27,6 +28,13 @@ 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 From 70a19d7860b52d303ef0e52e8f3d70a74dc5ced9 Mon Sep 17 00:00:00 2001 From: Gresh <> Date: Sat, 5 Aug 2023 12:42:43 -0400 Subject: [PATCH 2/6] Inheritance from DFHMS for classes that previously inherited from both DFMS and HMS, as well as classes that only inherited from HMS, in order to provide dynamic field serialization on those classes. --- api/serializers.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/api/serializers.py b/api/serializers.py index 63340999..0478c56b 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -40,7 +40,7 @@ class Meta: model = User fields = ('url', 'username', 'email', 'groups') -class DocumentSerializer(serializers.HyperlinkedModelSerializer): +class DocumentSerializer(DynamicFieldsHyperlinkedModelSerializer): class Meta: model = models.Document fields = ( @@ -62,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() @@ -215,7 +215,7 @@ class Meta: 'document__url' ) -class BackgroundSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer): +class BackgroundSerializer(DynamicFieldsHyperlinkedModelSerializer): class Meta: model = models.Background fields = ( @@ -235,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 = ( @@ -254,7 +254,7 @@ class Meta: 'parent' ) -class FeatSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer): +class FeatSerializer(DynamicFieldsHyperlinkedModelSerializer): class Meta: model = models.Feat fields = ( @@ -268,7 +268,7 @@ class Meta: 'document__url' ) -class ConditionSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer): +class ConditionSerializer(DynamicFieldsHyperlinkedModelSerializer): class Meta: model = models.Condition fields = ( @@ -280,7 +280,7 @@ class Meta: 'document__url' ) -class SubraceSerializer(serializers.HyperlinkedModelSerializer): +class SubraceSerializer(DynamicFieldsHyperlinkedModelSerializer): class Meta: model = models.Subrace fields = ('name', @@ -294,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 @@ -320,7 +320,7 @@ class Meta: 'document__url' ) -class ArchetypeSerializer(serializers.HyperlinkedModelSerializer): +class ArchetypeSerializer(DynamicFieldsHyperlinkedModelSerializer): class Meta: model = models.Archetype fields = ( @@ -333,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 @@ -360,7 +360,7 @@ class Meta: 'document__url' ) -class MagicItemSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer): +class MagicItemSerializer(DynamicFieldsHyperlinkedModelSerializer): class Meta: model = models.MagicItem fields = ( @@ -375,7 +375,7 @@ class Meta: 'document__url' ) -class WeaponSerializer(serializers.HyperlinkedModelSerializer): +class WeaponSerializer(DynamicFieldsHyperlinkedModelSerializer): class Meta: model = models.Weapon fields = ( @@ -392,7 +392,7 @@ class Meta: 'weight', 'properties') -class ArmorSerializer(serializers.HyperlinkedModelSerializer): +class ArmorSerializer(DynamicFieldsHyperlinkedModelSerializer): class Meta: model = models.Armor fields = ( From c64a8d0582b6cccf556b871d768e7ffb9881a902 Mon Sep 17 00:00:00 2001 From: Gresh <> Date: Sat, 5 Aug 2023 12:48:21 -0400 Subject: [PATCH 3/6] Revert "Inheritance from DFHMS for classes that previously" This reverts commit 70a19d7860b52d303ef0e52e8f3d70a74dc5ced9. --- api/serializers.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/api/serializers.py b/api/serializers.py index 0478c56b..63340999 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -40,7 +40,7 @@ class Meta: model = User fields = ('url', 'username', 'email', 'groups') -class DocumentSerializer(DynamicFieldsHyperlinkedModelSerializer): +class DocumentSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = models.Document fields = ( @@ -62,7 +62,7 @@ class Meta: model = Group fields = ('url', 'name') -class MonsterSerializer(DynamicFieldsHyperlinkedModelSerializer): +class MonsterSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer, serializers.ModelSerializer): speed = serializers.SerializerMethodField() environments = serializers.SerializerMethodField() @@ -215,7 +215,7 @@ class Meta: 'document__url' ) -class BackgroundSerializer(DynamicFieldsHyperlinkedModelSerializer): +class BackgroundSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer): class Meta: model = models.Background fields = ( @@ -235,12 +235,12 @@ class Meta: 'document__url' ) -class PlaneSerializer(DynamicFieldsHyperlinkedModelSerializer): +class PlaneSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer): class Meta: model = models.Plane fields = ('slug','name','desc','document__slug', 'document__title', 'document__url') -class SectionSerializer(DynamicFieldsHyperlinkedModelSerializer): +class SectionSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer): class Meta: model = models.Section fields = ( @@ -254,7 +254,7 @@ class Meta: 'parent' ) -class FeatSerializer(DynamicFieldsHyperlinkedModelSerializer): +class FeatSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer): class Meta: model = models.Feat fields = ( @@ -268,7 +268,7 @@ class Meta: 'document__url' ) -class ConditionSerializer(DynamicFieldsHyperlinkedModelSerializer): +class ConditionSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer): class Meta: model = models.Condition fields = ( @@ -280,7 +280,7 @@ class Meta: 'document__url' ) -class SubraceSerializer(DynamicFieldsHyperlinkedModelSerializer): +class SubraceSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = models.Subrace fields = ('name', @@ -294,7 +294,7 @@ class Meta: 'document__url' ) -class RaceSerializer(DynamicFieldsHyperlinkedModelSerializer): +class RaceSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer): subraces = SubraceSerializer(many=True,read_only=True) class Meta: model = models.Race @@ -320,7 +320,7 @@ class Meta: 'document__url' ) -class ArchetypeSerializer(DynamicFieldsHyperlinkedModelSerializer): +class ArchetypeSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = models.Archetype fields = ( @@ -333,7 +333,7 @@ class Meta: 'document__url' ) -class CharClassSerializer(DynamicFieldsHyperlinkedModelSerializer): +class CharClassSerializer(serializers.HyperlinkedModelSerializer): archetypes = ArchetypeSerializer(many=True,read_only=True) class Meta: model = models.CharClass @@ -360,7 +360,7 @@ class Meta: 'document__url' ) -class MagicItemSerializer(DynamicFieldsHyperlinkedModelSerializer): +class MagicItemSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer): class Meta: model = models.MagicItem fields = ( @@ -375,7 +375,7 @@ class Meta: 'document__url' ) -class WeaponSerializer(DynamicFieldsHyperlinkedModelSerializer): +class WeaponSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = models.Weapon fields = ( @@ -392,7 +392,7 @@ class Meta: 'weight', 'properties') -class ArmorSerializer(DynamicFieldsHyperlinkedModelSerializer): +class ArmorSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = models.Armor fields = ( From fd63e13de7b04fdbb78fbdca0f885409c6d07567 Mon Sep 17 00:00:00 2001 From: Gresh <> Date: Sat, 5 Aug 2023 12:54:13 -0400 Subject: [PATCH 4/6] Inheritance from DFHMS for classes that previously inherited from both DFMS and HMS, as well as classes that only inherited from HMS, in order to provide dynamic field serialization on those classes. Subrace is left out due to a bug in the DFHMS, which will be fixed in the following commit. --- api/serializers.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/api/serializers.py b/api/serializers.py index 63340999..49eb4cc0 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -40,7 +40,7 @@ class Meta: model = User fields = ('url', 'username', 'email', 'groups') -class DocumentSerializer(serializers.HyperlinkedModelSerializer): +class DocumentSerializer(DynamicFieldsHyperlinkedModelSerializer): class Meta: model = models.Document fields = ( @@ -62,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() @@ -215,7 +215,7 @@ class Meta: 'document__url' ) -class BackgroundSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer): +class BackgroundSerializer(DynamicFieldsHyperlinkedModelSerializer): class Meta: model = models.Background fields = ( @@ -235,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 = ( @@ -254,7 +254,7 @@ class Meta: 'parent' ) -class FeatSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer): +class FeatSerializer(DynamicFieldsHyperlinkedModelSerializer): class Meta: model = models.Feat fields = ( @@ -268,7 +268,7 @@ class Meta: 'document__url' ) -class ConditionSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer): +class ConditionSerializer(DynamicFieldsHyperlinkedModelSerializer): class Meta: model = models.Condition fields = ( @@ -280,7 +280,7 @@ class Meta: 'document__url' ) -class SubraceSerializer(serializers.HyperlinkedModelSerializer): +class SubraceSerializer(DynamicFieldsHyperlinkedModelSerializer): class Meta: model = models.Subrace fields = ('name', @@ -294,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 @@ -333,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 @@ -360,7 +360,7 @@ class Meta: 'document__url' ) -class MagicItemSerializer(DynamicFieldsModelSerializer, serializers.HyperlinkedModelSerializer): +class MagicItemSerializer(DynamicFieldsHyperlinkedModelSerializer): class Meta: model = models.MagicItem fields = ( @@ -375,7 +375,7 @@ class Meta: 'document__url' ) -class WeaponSerializer(serializers.HyperlinkedModelSerializer): +class WeaponSerializer(DynamicFieldsHyperlinkedModelSerializer): class Meta: model = models.Weapon fields = ( @@ -392,7 +392,7 @@ class Meta: 'weight', 'properties') -class ArmorSerializer(serializers.HyperlinkedModelSerializer): +class ArmorSerializer(DynamicFieldsHyperlinkedModelSerializer): class Meta: model = models.Armor fields = ( From b935d94bfbda863891ec7bae16d3e799b0fb2abd Mon Sep 17 00:00:00 2001 From: Gresh <> Date: Sat, 5 Aug 2023 12:55:18 -0400 Subject: [PATCH 5/6] Fix bug in DFMS (indexing with key that may not exist) --- api/serializers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/serializers.py b/api/serializers.py index 49eb4cc0..36c2936d 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -18,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(',') @@ -280,7 +280,7 @@ class Meta: 'document__url' ) -class SubraceSerializer(DynamicFieldsHyperlinkedModelSerializer): +class SubraceSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = models.Subrace fields = ('name', From f382abede41a51937ee0c9c8101eed4f37a9369c Mon Sep 17 00:00:00 2001 From: Gresh <> Date: Sat, 5 Aug 2023 12:55:48 -0400 Subject: [PATCH 6/6] Implement DFHMS on Subrace --- api/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/serializers.py b/api/serializers.py index 36c2936d..2db33761 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -280,7 +280,7 @@ class Meta: 'document__url' ) -class SubraceSerializer(serializers.HyperlinkedModelSerializer): +class SubraceSerializer(DynamicFieldsHyperlinkedModelSerializer): class Meta: model = models.Subrace fields = ('name',