Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2 split views and serializers #366

Merged
merged 16 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions api_v2/serializers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""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

from .background import BackgroundBenefitSerializer
from .background import BackgroundSerializer

from .document import RulesetSerializer
from .document import LicenseSerializer
from .document import PublisherSerializer
from .document import DocumentSerializer

from .feat import FeatBenefitSerializer
from .feat import FeatSerializer

from .race import TraitSerializer
from .race import RaceSerializer

from .creature import CreatureSerializer
from .creature import CreatureTypeSerializer
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
23 changes: 23 additions & 0 deletions api_v2/serializers/background.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Serializer for the BackgroundBenefit and Background models."""

from rest_framework import serializers

from api_v2 import models

from .abstracts import GameContentSerializer

class BackgroundBenefitSerializer(serializers.ModelSerializer):
class Meta:
model = models.BackgroundBenefit
fields = ['name','desc','type']


class BackgroundSerializer(GameContentSerializer):
key = serializers.ReadOnlyField()
benefits = BackgroundBenefitSerializer(
many=True
)

class Meta:
model = models.Background
fields = '__all__'
168 changes: 6 additions & 162 deletions api_v2/serializers.py → api_v2/serializers/creature.py
Original file line number Diff line number Diff line change
@@ -1,159 +1,13 @@
"""Serializers and helper methods for the Creature model."""

from math import floor

from rest_framework import serializers
from django.core.exceptions import ObjectDoesNotExist

from api_v2 import models

from .abstracts import GameContentSerializer

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):
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 = '__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:
model = models.FeatBenefit
fields = ['desc']

class FeatSerializer(GameContentSerializer):
key = serializers.ReadOnlyField()
has_prerequisite = serializers.ReadOnlyField()
benefits = FeatBenefitSerializer(
many=True)

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


class TraitSerializer(serializers.ModelSerializer):

class Meta:
model = models.Trait
fields = ['name', 'desc']


class RaceSerializer(GameContentSerializer):
key = serializers.ReadOnlyField()
is_subrace = serializers.ReadOnlyField()
is_selectable = serializers.ReadOnlyField()
traits = TraitSerializer(
many=True)

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


def calc_damage_amount(die_count, die_type, bonus):
Expand Down Expand Up @@ -260,7 +114,6 @@ class Meta:
'category',
'size',
'type',
'subtype',
'alignment',
'weight',
'armor_class',
Expand Down Expand Up @@ -369,18 +222,9 @@ def get_actions(self, creature):
return result


class BackgroundBenefitSerializer(serializers.ModelSerializer):
class Meta:
model = models.BackgroundBenefit
fields = ['name','desc','type']


class BackgroundSerializer(GameContentSerializer):
class CreatureTypeSerializer(GameContentSerializer):
key = serializers.ReadOnlyField()
benefits = BackgroundBenefitSerializer(
many=True
)

class Meta:
model = models.Background
model = models.CreatureType
fields = '__all__'
35 changes: 35 additions & 0 deletions api_v2/serializers/document.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Serializers for Ruleset, License, Publisher, and Document models."""
from rest_framework import serializers

from api_v2 import models

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__"
22 changes: 22 additions & 0 deletions api_v2/serializers/feat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""Serializer for the FeatBenefitSerializer and FeatSerializer models."""

from rest_framework import serializers

from api_v2 import models

from .abstracts import GameContentSerializer

class FeatBenefitSerializer(serializers.ModelSerializer):
class Meta:
model = models.FeatBenefit
fields = ['desc']

class FeatSerializer(GameContentSerializer):
key = serializers.ReadOnlyField()
has_prerequisite = serializers.ReadOnlyField()
benefits = FeatBenefitSerializer(
many=True)

class Meta:
model = models.Feat
fields = '__all__'
Loading