diff --git a/api_v2/serializers/__init__.py b/api_v2/serializers/__init__.py index c2e9fc92..32c737b6 100644 --- a/api_v2/serializers/__init__.py +++ b/api_v2/serializers/__init__.py @@ -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 \ No newline at end of file diff --git a/api_v2/serializers/abstracts.py b/api_v2/serializers/abstracts.py new file mode 100644 index 00000000..4ef63840 --- /dev/null +++ b/api_v2/serializers/abstracts.py @@ -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 diff --git a/api_v2/serializers/background.py b/api_v2/serializers/background.py new file mode 100644 index 00000000..e69de29b diff --git a/api_v2/serializers/item.py b/api_v2/serializers/item.py new file mode 100644 index 00000000..360b74bc --- /dev/null +++ b/api_v2/serializers/item.py @@ -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__' diff --git a/api_v2/serializers/serializers.py b/api_v2/serializers/serializers.py index cf8641de..e344ff40 100644 --- a/api_v2/serializers/serializers.py +++ b/api_v2/serializers/serializers.py @@ -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): @@ -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: