Skip to content

Commit

Permalink
Solution-py-api-views v1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
skyfoxwork committed Dec 8, 2024
1 parent 5c7b2c1 commit 6234d67
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions cinema/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from urllib.request import Request

from rest_framework.response import Response
from rest_framework import status
from rest_framework.views import APIView
Expand All @@ -15,42 +17,42 @@


class GenreList(APIView):
def get(self, request):
def get(self, request: Request) -> Response:
genre = Genre.objects.all()
serializer = GenreSerializer(genre, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)

def post(self, request):
def post(self, request: Request) -> Response:
serializer = GenreSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)


class GenreDetail(APIView):
def get_object(self, pk):
def get_object(self, pk: int) -> Genre:
return get_object_or_404(Genre, pk=pk)

def get(self, request, pk):
def get(self, request: Request, pk: int) -> Response:
genre = self.get_object(pk)
serializer = GenreSerializer(genre)
return Response(serializer.data, status=status.HTTP_200_OK)

def put(self, request, pk):
def put(self, request: Request, pk: int) -> Response:
gener = self.get_object(pk)
serializer = GenreSerializer(gener, data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data)

def patch(self, request, pk):
def patch(self, request: Request, pk: int) -> Response:
gener = self.get_object(pk)
serializer = GenreSerializer(gener, data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data)

def delete(self, request, pk):
def delete(self, request: Request, pk: int) -> Response:
gener = self.get_object(pk)
gener.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
Expand All @@ -64,10 +66,10 @@ class ActorList(
queryset = Actor.objects.all()
serializer_class = ActorSerializer

def get(self, request, *args, **kwargs):
def get(self, request: Request, *args, **kwargs) -> Response:
return self.list(request, *args, **kwargs)

def post(self, request, *args, **kwargs):
def post(self, request: Request, *args, **kwargs) -> Response:
return self.create(request, *args, **kwargs)


Expand All @@ -80,16 +82,16 @@ class ActorDetail(
queryset = Actor.objects.all()
serializer_class = ActorSerializer

def get(self, request, *args, **kwargs):
def get(self, request: Request, *args, **kwargs) -> Response:
return self.retrieve(request, *args, **kwargs)

def put(self, request, *args, **kwargs):
def put(self, request: Request, *args, **kwargs) -> Response:
return self.update(request, *args, **kwargs)

def patch(self, request, *args, **kwargs):
def patch(self, request: Request, *args, **kwargs) -> Response:
return self.partial_update(request, *args, **kwargs)

def delete(self, request, *args, **kwargs):
def delete(self, request: Request, *args, **kwargs) -> Response:
return self.destroy(request, *args, **kwargs)


Expand Down

0 comments on commit 6234d67

Please sign in to comment.