Skip to content

Commit

Permalink
Simplifying how key and url are sent.
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildTools committed Aug 4, 2023
1 parent eb45d86 commit 7b213bd
Showing 1 changed file with 22 additions and 50 deletions.
72 changes: 22 additions & 50 deletions api_v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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')
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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']
fields = '__all__'

0 comments on commit 7b213bd

Please sign in to comment.