From 55b22c1210c01ef41eead0e956b265f7c752142d Mon Sep 17 00:00:00 2001 From: ldeluigi <44567586+ldeluigi@users.noreply.github.com> Date: Wed, 8 Feb 2023 16:40:22 +0100 Subject: [PATCH] Fix API --- backend/spellbook/serializers.py | 67 +++++++++++++++++++++++++++----- 1 file changed, 58 insertions(+), 9 deletions(-) diff --git a/backend/spellbook/serializers.py b/backend/spellbook/serializers.py index 513678d5..8c1be040 100644 --- a/backend/spellbook/serializers.py +++ b/backend/spellbook/serializers.py @@ -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): @@ -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 @@ -42,8 +77,6 @@ class Meta: 'needs', 'uses', 'requires', - 'zone_locations', - 'cards_state', 'mana_needed', 'other_prerequisites', 'description'] @@ -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) @@ -73,8 +124,6 @@ class Meta: 'of', 'includes', 'identity', - 'zone_locations', - 'cards_state', 'mana_needed', 'other_prerequisites', 'description',