Skip to content

Commit

Permalink
Items and backgrounds split out.
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildTools committed Nov 4, 2023
1 parent 945fdc9 commit 63301cf
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 84 deletions.
5 changes: 5 additions & 0 deletions api_v2/serializers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
"""The initialization for serializers for open5e's api v2."""

from .serializers import *

from .item import ArmorSerializer
from .item import WeaponSerializer
from .item import ItemSerializer
from .item import ItemSetSerializer
44 changes: 44 additions & 0 deletions api_v2/serializers/abstracts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Abstract serializers."""
from rest_framework import serializers

from api_v2 import models


class GameContentSerializer(serializers.HyperlinkedModelSerializer):

# Adding dynamic "fields" qs parameter.
def __init__(self, *args, **kwargs):
# Add default fields variable.

# Instantiate the superclass normally
super(GameContentSerializer, 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']:
fields = self.context['request'].query_params.get('fields')
if fields:
fields = fields.split(',')
# Drop any fields that are not specified in the `fields` argument.
allowed = set(fields)
existing = set(self.fields.keys())
for field_name in existing - allowed:
self.fields.pop(field_name)

depth = self.context['request'].query_params.get('depth')
if depth:
try:
depth_value = int(depth)
if depth_value > 0 and depth_value < 3:
# This value going above 1 could cause performance issues.
# Limited to 1 and 2 for now.
self.Meta.depth = depth_value
# Depth does not reset by default on subsequent requests with malformed urls.
else:
self.Meta.depth = 0
except ValueError:
pass # it was not castable to an int.
else:
self.Meta.depth = 0 #The default.

class Meta:
abstract = True
Empty file.
50 changes: 50 additions & 0 deletions api_v2/serializers/item.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""Serializer for the Item, Itemset, armor, and weapon models"""
from rest_framework import serializers

from api_v2 import models

from .abstracts import GameContentSerializer


class ArmorSerializer(GameContentSerializer):
key = serializers.ReadOnlyField()
ac_display = serializers.ReadOnlyField()

class Meta:
model = models.Armor
fields = '__all__'

class WeaponSerializer(GameContentSerializer):
key = serializers.ReadOnlyField()
is_versatile = serializers.ReadOnlyField()
is_martial = serializers.ReadOnlyField()
is_melee = serializers.ReadOnlyField()
ranged_attack_possible = serializers.ReadOnlyField()
range_melee = serializers.ReadOnlyField()
is_reach = serializers.ReadOnlyField()
properties = serializers.ReadOnlyField()

class Meta:
model = models.Weapon
fields = '__all__'


class ItemSerializer(GameContentSerializer):
key = serializers.ReadOnlyField()
is_magic_item = serializers.ReadOnlyField()
weapon = WeaponSerializer(read_only=True, context={'request': {}})
armor = ArmorSerializer(read_only=True, context={'request': {}})


class Meta:
model = models.Item
fields = '__all__'


class ItemSetSerializer(GameContentSerializer):
key = serializers.ReadOnlyField()
items = ItemSerializer(many=True, read_only=True, context={'request':{}})

class Meta:
model = models.ItemSet
fields = '__all__'
85 changes: 1 addition & 84 deletions api_v2/serializers/serializers.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,8 @@
from math import floor
from rest_framework import serializers
from django.core.exceptions import ObjectDoesNotExist

from api_v2 import models


class GameContentSerializer(serializers.HyperlinkedModelSerializer):

# Adding dynamic "fields" qs parameter.
def __init__(self, *args, **kwargs):
# Add default fields variable.

# Instantiate the superclass normally
super(GameContentSerializer, 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']:
fields = self.context['request'].query_params.get('fields')
if fields:
fields = fields.split(',')
# Drop any fields that are not specified in the `fields` argument.
allowed = set(fields)
existing = set(self.fields.keys())
for field_name in existing - allowed:
self.fields.pop(field_name)

depth = self.context['request'].query_params.get('depth')
if depth:
try:
depth_value = int(depth)
if depth_value > 0 and depth_value < 3:
# This value going above 1 could cause performance issues.
# Limited to 1 and 2 for now.
self.Meta.depth = depth_value
# Depth does not reset by default on subsequent requests with malformed urls.
else:
self.Meta.depth = 0
except ValueError:
pass # it was not castable to an int.
else:
self.Meta.depth = 0 #The default.

class Meta:
abstract = True



class RulesetSerializer(serializers.HyperlinkedModelSerializer):
Expand Down Expand Up @@ -77,49 +37,6 @@ class Meta:
fields = "__all__"


class ArmorSerializer(GameContentSerializer):
key = serializers.ReadOnlyField()
ac_display = serializers.ReadOnlyField()

class Meta:
model = models.Armor
fields = '__all__'


class WeaponSerializer(GameContentSerializer):
key = serializers.ReadOnlyField()
is_versatile = serializers.ReadOnlyField()
is_martial = serializers.ReadOnlyField()
is_melee = serializers.ReadOnlyField()
ranged_attack_possible = serializers.ReadOnlyField()
range_melee = serializers.ReadOnlyField()
is_reach = serializers.ReadOnlyField()
properties = serializers.ReadOnlyField()

class Meta:
model = models.Weapon
fields = '__all__'


class ItemSerializer(GameContentSerializer):
key = serializers.ReadOnlyField()
is_magic_item = serializers.ReadOnlyField()
weapon = WeaponSerializer(read_only=True, context={'request': {}})
armor = ArmorSerializer(read_only=True, context={'request': {}})


class Meta:
model = models.Item
fields = '__all__'


class ItemSetSerializer(GameContentSerializer):
key = serializers.ReadOnlyField()
items = ItemSerializer(many=True, read_only=True, context={'request':{}})

class Meta:
model = models.ItemSet
fields = '__all__'

class FeatBenefitSerializer(serializers.ModelSerializer):
class Meta:
Expand Down

0 comments on commit 63301cf

Please sign in to comment.