-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
BuildTools
committed
Nov 4, 2023
1 parent
945fdc9
commit 63301cf
Showing
5 changed files
with
100 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters