Skip to content

Commit

Permalink
Fix API
Browse files Browse the repository at this point in the history
  • Loading branch information
ldeluigi committed Feb 8, 2023
1 parent 96a1128 commit 55b22c1
Showing 1 changed file with 58 additions and 9 deletions.
67 changes: 58 additions & 9 deletions backend/spellbook/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
from rest_framework import serializers
from .models import Card, Template, Feature, Combo, Variant
from .models import Card, Template, Feature, Combo, Variant, CardInCombo, TemplateInCombo, CardInVariant, TemplateInVariant, IngredientInCombination


class ChoiceField(serializers.ChoiceField):
def to_representation(self, obj):
if obj == '' and self.allow_blank:
return obj
return self._choices[obj]

def to_internal_value(self, data):
# To support inserts with the value
if data == '' and self.allow_blank:
return ''

for key, val in self._choices.items():
if val == data:
return key
self.fail('invalid_choice', input=data)


class CardSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -28,11 +45,29 @@ class Meta:
fields = ['id', 'name', 'scryfall_query', 'scryfall_api']


class CardInComboSerializer(serializers.ModelSerializer):
card = CardSerializer(many=False, read_only=True)
zone_location = ChoiceField(choices=IngredientInCombination.ZoneLocation.choices)

class Meta:
model = CardInCombo
fields = ['card', 'zone_location', 'card_state']


class TemplateInComboSerializer(serializers.ModelSerializer):
template = TemplateSerializer(many=False, read_only=True)
zone_location = ChoiceField(choices=IngredientInCombination.ZoneLocation.choices)

class Meta:
model = TemplateInCombo
fields = ['template', 'zone_location', 'card_state']


class ComboDetailSerializer(serializers.ModelSerializer):
produces = FeatureSerializer(many=True, read_only=True)
needs = FeatureSerializer(many=True, read_only=True)
uses = CardSerializer(many=True, read_only=True)
requires = TemplateSerializer(many=True, read_only=True)
uses = CardInComboSerializer(source='cardincombo_set', many=True, read_only=True)
requires = TemplateInComboSerializer(source='templateincombo_set', many=True, read_only=True)

class Meta:
model = Combo
Expand All @@ -42,8 +77,6 @@ class Meta:
'needs',
'uses',
'requires',
'zone_locations',
'cards_state',
'mana_needed',
'other_prerequisites',
'description']
Expand All @@ -55,9 +88,27 @@ class Meta:
fields = ['id']


class CardInVariantSerializer(serializers.ModelSerializer):
card = CardSerializer(many=False, read_only=True)
zone_location = ChoiceField(choices=IngredientInCombination.ZoneLocation.choices)

class Meta:
model = CardInVariant
fields = ['card', 'zone_location', 'card_state']


class TemplateInVariantSerializer(serializers.ModelSerializer):
template = TemplateSerializer(many=False, read_only=True)
zone_location = ChoiceField(choices=IngredientInCombination.ZoneLocation.choices)

class Meta:
model = TemplateInVariant
fields = ['template', 'zone_location', 'card_state']


class VariantSerializer(serializers.ModelSerializer):
uses = CardSerializer(many=True, read_only=True)
requires = TemplateSerializer(many=True, read_only=True)
uses = CardInVariantSerializer(source='cardinvariant_set', many=True, read_only=True)
requires = TemplateInVariantSerializer(source='templateinvariant_set', many=True, read_only=True)
produces = FeatureSerializer(many=True, read_only=True)
of = ComboSerializer(many=True, read_only=True)
includes = ComboSerializer(many=True, read_only=True)
Expand All @@ -73,8 +124,6 @@ class Meta:
'of',
'includes',
'identity',
'zone_locations',
'cards_state',
'mana_needed',
'other_prerequisites',
'description',
Expand Down

0 comments on commit 55b22c1

Please sign in to comment.