From 56dc11c7b36c1d05520a2404255a55bb43791444 Mon Sep 17 00:00:00 2001 From: Alexander-D-Karpov Date: Tue, 7 Jun 2022 23:54:26 +0300 Subject: [PATCH] added deck update and delete --- game/api/v1/serializers.py | 36 ++++++++++++++++++++++++------------ game/api/v1/views.py | 20 ++++++++++++++++---- game/models.py | 6 +++++- game/urls.py | 2 +- 4 files changed, 46 insertions(+), 18 deletions(-) diff --git a/game/api/v1/serializers.py b/game/api/v1/serializers.py index a54c81e..be88d30 100644 --- a/game/api/v1/serializers.py +++ b/game/api/v1/serializers.py @@ -56,7 +56,7 @@ class CreatePlayerSerializer(serializers.ModelSerializer): fields = ("ton_wallet", "name") -class DeckCreateSerializer(serializers.ModelSerializer): +class CreateDeckSerializer(serializers.ModelSerializer): hero_ids = serializers.ListSerializer( child=serializers.UUIDField(), min_length=16, max_length=16 ) @@ -66,14 +66,17 @@ class DeckCreateSerializer(serializers.ModelSerializer): fields = ("hero_ids",) def validate_hero_ids(self, value): - for x in value: - if not (hero := Hero.objects.filter(uuid=x)): - raise ValidationError(f"Hero with uuid {x} doesn't exist") + if self.context["request"].method == "POST": + for x in value: + if not (hero := Hero.objects.filter(uuid=x)): + raise ValidationError(f"Hero with uuid {x} doesn't exist") - if deck := HeroInDeck.objects.filter(hero=hero.first()): - raise ValidationError( - f"Hero with uuid {x} is already in deck with id {deck.first().deck.id}" - ) + if deck := HeroInDeck.objects.filter(hero=hero.first()): + raise ValidationError( + f"Hero with uuid {x} is already in deck with id {deck.first().deck.id}" + ) + elif self.context["request"].method in ["PUT", "PATCH"]: + print(value) return value def create(self, validated_data): @@ -82,13 +85,22 @@ class DeckCreateSerializer(serializers.ModelSerializer): HeroInDeck.objects.create(hero_id=x, deck=deck) return deck + def update(self, instance, validated_data): + print(instance, validated_data) + return instance + + + +class GetPlayerSerializer(serializers.ModelSerializer): + class Meta: + model = Player + fields = ("id", "name") + class GetDeckSerializer(serializers.ModelSerializer): + player = GetPlayerSerializer() heroes = ListHeroSerializer(many=True) class Meta: model = Deck - fields = ("player_uuid", "heroes") - - def get_heroes(self, val): - print(val) + fields = ("player", "heroes") diff --git a/game/api/v1/views.py b/game/api/v1/views.py index e684555..21f71dc 100644 --- a/game/api/v1/views.py +++ b/game/api/v1/views.py @@ -11,13 +11,13 @@ from rest_framework.mixins import ( from rest_framework.response import Response from game.authentication import PlayerAuthentication -from game.models import Hero +from game.models import Hero, Deck from game.api.v1.serializers import ( CreateHeroSerializer, GetHeroSerializer, CreatePlayerSerializer, ListHeroSerializer, - DeckCreateSerializer, + CreateDeckSerializer, GetDeckSerializer, ) from game.services.jwt import sign_jwt @@ -85,7 +85,7 @@ class PlayerCreateView(GenericAPIView, CreateModelMixin): class DeckCreateView(GenericAPIView, CreateModelMixin): - serializer_class = DeckCreateSerializer + serializer_class = CreateDeckSerializer authentication_classes = (PlayerAuthentication,) def perform_create(self, serializer): @@ -102,11 +102,23 @@ class DeckCreateView(GenericAPIView, CreateModelMixin): class RetireUpdateDeleteDeckView( RetrieveHeroView, DestroyModelMixin, UpdateModelMixin, GenericAPIView ): - lookup_field = "pk" + lookup_field = "id" + queryset = Deck.objects.all() def get_serializer_class(self): if self.request.method == "GET": return GetDeckSerializer + else: + return CreateDeckSerializer def get(self, request, *args, **kwargs): return self.retrieve(request, *args, **kwargs) + + def put(self, request, *args, **kwargs): + return self.update(request, *args, **kwargs) + + def patch(self, request, *args, **kwargs): + return self.partial_update(request, *args, **kwargs) + + def delete(self, request, *args, **kwargs): + return self.destroy(request, *args, **kwargs) diff --git a/game/models.py b/game/models.py index 3d30375..d42763d 100644 --- a/game/models.py +++ b/game/models.py @@ -116,11 +116,15 @@ class Deck(models.Model): ) def __str__(self): - return f"{self.player.name} deck" + return f"{self.player.name}'s deck" def get_heroes(self): return [x.hero for x in HeroInDeck.objects.filter(deck=self)] + def heroes(self): + # added for better DRF view + return self.get_heroes() + class Meta: db_table = "deck" verbose_name = "deck" diff --git a/game/urls.py b/game/urls.py index 8185009..7f4fee1 100644 --- a/game/urls.py +++ b/game/urls.py @@ -14,6 +14,6 @@ urlpatterns = [ path("v1/player/", PlayerCreateView.as_view(), name="player_create_api"), path("v1/deck/", DeckCreateView.as_view(), name="deck_create_api"), path( - "v1/deck/", RetireUpdateDeleteDeckView.as_view(), name="deck_retire_api" + "v1/deck/", RetireUpdateDeleteDeckView.as_view(), name="deck_retire_api" ), ]