2022-06-04 19:30:54 +03:00
|
|
|
from rest_framework import status
|
2022-06-04 16:01:23 +03:00
|
|
|
|
2022-06-04 19:30:54 +03:00
|
|
|
from rest_framework.generics import GenericAPIView, UpdateAPIView
|
2022-06-05 13:09:27 +03:00
|
|
|
from rest_framework.mixins import CreateModelMixin, RetrieveModelMixin, ListModelMixin
|
2022-06-04 19:30:54 +03:00
|
|
|
from rest_framework.response import Response
|
|
|
|
|
2022-06-05 13:09:27 +03:00
|
|
|
from game.authentication import PlayerAuthentication
|
2022-06-04 19:30:54 +03:00
|
|
|
from game.models import Hero
|
2022-06-05 13:09:27 +03:00
|
|
|
from game.serializers import CreateHeroSerializer, GetHeroSerializer, CreatePlayerView
|
|
|
|
from game.services.jwt import sign_jwt
|
2022-06-04 19:30:54 +03:00
|
|
|
|
|
|
|
|
2022-06-05 13:09:27 +03:00
|
|
|
class CreateHeroView(GenericAPIView, CreateModelMixin, ListModelMixin):
|
|
|
|
authentication_classes = (PlayerAuthentication,)
|
2022-06-04 19:30:54 +03:00
|
|
|
|
|
|
|
def perform_create(self, serializer):
|
|
|
|
return serializer.save()
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
instance = self.perform_create(serializer)
|
|
|
|
return Response({"uuid": instance.uuid}, status=status.HTTP_201_CREATED)
|
|
|
|
|
2022-06-05 13:09:27 +03:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
return self.list(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_serializer_class(self):
|
|
|
|
if self.request.method == "GET":
|
|
|
|
return GetHeroSerializer
|
|
|
|
else:
|
|
|
|
return CreateHeroSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return Hero.objects.filter(player_id=self.request.user.id)
|
|
|
|
|
2022-06-04 19:30:54 +03:00
|
|
|
|
|
|
|
class RetrieveHeroView(RetrieveModelMixin, UpdateAPIView, GenericAPIView):
|
|
|
|
serializer_class = GetHeroSerializer
|
2022-06-05 13:09:27 +03:00
|
|
|
authentication_classes = (PlayerAuthentication,)
|
2022-06-04 19:30:54 +03:00
|
|
|
lookup_field = "uuid"
|
|
|
|
queryset = Hero.objects.all()
|
|
|
|
|
|
|
|
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)
|
2022-06-05 13:09:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
class PlayerCreateView(GenericAPIView, CreateModelMixin):
|
|
|
|
serializer_class = CreatePlayerView
|
|
|
|
|
|
|
|
def perform_create(self, serializer):
|
|
|
|
return serializer.save()
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
instance = self.perform_create(serializer)
|
|
|
|
|
|
|
|
# TODO: add JTI to refresh token
|
|
|
|
access_jwt = sign_jwt({"id": instance.id, "type": "access"}, t_life=3600)
|
|
|
|
refresh_jwt = sign_jwt({"id": instance.id, "type": "refresh"})
|
|
|
|
return Response({"access_token": access_jwt, "refresh_token": refresh_jwt}, status=status.HTTP_201_CREATED)
|