From 7b213bd984d18268012915e9400b5732f36b4910 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Fri, 4 Aug 2023 15:10:45 -0500 Subject: [PATCH] Simplifying how key and url are sent. --- api_v2/serializers.py | 72 +++++++++++++------------------------------ 1 file changed, 22 insertions(+), 50 deletions(-) diff --git a/api_v2/serializers.py b/api_v2/serializers.py index 2bd3adc8..09d89d6e 100644 --- a/api_v2/serializers.py +++ b/api_v2/serializers.py @@ -2,11 +2,8 @@ from api_v2 import models -# Default set of fields that almost all gamecontent items will have. -GAMECONTENT_FIELDS = ['url', 'key', 'name', 'desc', 'document'] - -class GameContentSerializer(serializers.ModelSerializer): +class GameContentSerializer(serializers.HyperlinkedModelSerializer): # Adding dynamic "fields" qs parameter. def __init__(self, *args, **kwargs): @@ -15,6 +12,9 @@ def __init__(self, *args, **kwargs): # Instantiate the superclass normally super(GameContentSerializer, self).__init__(*args, **kwargs) + # Add all read-only fields. + # setattr(self.Meta, 'read_only_fields', [*self.fields]) + # The request doesn't exist when generating an OAS file, so we have to check that first if self.context['request']: fields = self.context['request'].query_params.get('fields') @@ -30,8 +30,8 @@ def __init__(self, *args, **kwargs): if depth: try: depth_value = int(depth) - if depth_value == 1: - self.Meta.depth = 1 + if depth_value > 0 and depth_value < 3: + self.Meta.depth = depth_value #This value going above 1 could massively cause performance issues. else: self.Meta.depth = 0 @@ -46,44 +46,48 @@ class Meta: class RulesetSerializer(serializers.HyperlinkedModelSerializer): + key = serializers.ReadOnlyField() + class Meta: model = models.Ruleset fields = '__all__' class LicenseSerializer(serializers.HyperlinkedModelSerializer): + key = serializers.ReadOnlyField() + class Meta: model = models.License fields = '__all__' class PublisherSerializer(serializers.HyperlinkedModelSerializer): + key = serializers.ReadOnlyField() + class Meta: model = models.Publisher fields = '__all__' class DocumentSerializer(serializers.HyperlinkedModelSerializer): + key = serializers.ReadOnlyField() + class Meta: model = models.Document fields = "__all__" class ArmorSerializer(GameContentSerializer): + key = serializers.ReadOnlyField() ac_display = serializers.ReadOnlyField() class Meta: model = models.Armor - fields = ['url','key','name','document'] + [ - 'ac_display', - 'grants_stealth_disadvantage', - 'strength_score_required', - 'ac_base', - 'ac_add_dexmod', - 'ac_cap_dexmod'] + fields = '__all__' class WeaponSerializer(GameContentSerializer): + key = serializers.ReadOnlyField() is_versatile = serializers.ReadOnlyField() is_martial = serializers.ReadOnlyField() is_melee = serializers.ReadOnlyField() @@ -94,53 +98,21 @@ class WeaponSerializer(GameContentSerializer): class Meta: model = models.Weapon - fields = ['url','key','name','document'] + [ # Remove the DESC field. - 'properties', - 'is_versatile', - 'is_martial', - 'is_melee', - 'range_melee', - 'ranged_attack_possible', - 'is_reach', - 'damage_type', - 'damage_dice', - 'versatile_dice', - 'range_reach', - 'range_normal', - 'range_long', - 'is_finesse', - 'is_thrown', - 'is_two_handed', - 'requires_ammunition', - 'requires_loading', - 'is_heavy', - 'is_light', - 'is_lance', - 'is_net', - 'is_simple', - 'is_improvised'] + fields = '__all__' class ItemSerializer(GameContentSerializer): - + key = serializers.ReadOnlyField() is_magic_item = serializers.ReadOnlyField() class Meta: model = models.Item - fields = GAMECONTENT_FIELDS + [ - 'category', - 'cost', - 'weight', - 'weapon', - 'armor', - 'requires_attunement', - 'rarity', - 'is_magic_item'] + fields = '__all__' class ItemSetSerializer(GameContentSerializer): + key = serializers.ReadOnlyField() class Meta: model = models.ItemSet - fields = GAMECONTENT_FIELDS + [ - 'items'] \ No newline at end of file + fields = '__all__'