From 0ba6e7bceec29e0f20796f9429756b87a6627001 Mon Sep 17 00:00:00 2001 From: FursykIvan Date: Mon, 16 Dec 2024 02:02:25 +0200 Subject: [PATCH] Solution --- cinema/urls.py | 18 +++++++------ cinema/views.py | 69 +++++++++++++++++++++++-------------------------- 2 files changed, 43 insertions(+), 44 deletions(-) diff --git a/cinema/urls.py b/cinema/urls.py index 46d953e9..b7dfcca3 100644 --- a/cinema/urls.py +++ b/cinema/urls.py @@ -2,16 +2,18 @@ from rest_framework.routers import DefaultRouter from cinema.views import ( - GenreAPIView, - ActorGenericAPIView, + GenreList, + GenreDetail, + ActorList, + ActorDetail, CinemaHallViewSet, - MovieModelViewSet + MovieViewSet ) app_name = "cinema" router = DefaultRouter() -router.register("movies", MovieModelViewSet, basename="movies") +router.register("movies", MovieViewSet, basename="movies") cinema_hall_list = CinemaHallViewSet.as_view( actions={"get": "list", @@ -27,11 +29,11 @@ urlpatterns = [ path("", include(router.urls)), - path("genres/", GenreAPIView.as_view(), name="genre-list"), - path("genres//", GenreAPIView.as_view(), name="genre-detail"), - path("actors/", ActorGenericAPIView.as_view(), name="actor-list"), + path("genres/", GenreList.as_view(), name="genre-list"), + path("genres//", GenreDetail.as_view(), name="genre-detail"), + path("actors/", ActorList.as_view(), name="actor-list"), path("actors//", - ActorGenericAPIView.as_view(), + ActorDetail.as_view(), name="actor-detail"), path("cinema_halls/", cinema_hall_list, diff --git a/cinema/views.py b/cinema/views.py index bafdc91a..698d147d 100644 --- a/cinema/views.py +++ b/cinema/views.py @@ -12,7 +12,7 @@ CinemaHallSerializer) -class GenreAPIView(APIView): +class GenreList(APIView): def get(self, request): genres = Genre.objects.all() serializer = GenreSerializer(genres, many=True) @@ -25,9 +25,16 @@ def post(self, request): return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + +class GenreDetail(APIView): def get_object(self, pk): return get_object_or_404(Genre, pk=pk) + def get(self, request, pk): + genre = self.get_object(pk) + serializer = GenreSerializer(genre) + return Response(serializer.data, status=status.HTTP_200_OK) + def put(self, request, pk): genre = self.get_object(pk) serializer = GenreSerializer(genre, data=request.data) @@ -50,47 +57,37 @@ def delete(self, request, pk): return Response(status=status.HTTP_204_NO_CONTENT) -class ActorGenericAPIView(generics.GenericAPIView): +class ActorList(mixins.ListModelMixin, + mixins.CreateModelMixin, + generics.GenericAPIView): queryset = Actor.objects.all() serializer_class = ActorSerializer - def get(self, request, pk=None): - if pk is None: - actors = self.get_queryset() - serializer = self.get_serializer(actors, many=True) - return Response(serializer.data, status=status.HTTP_200_OK) - else: - actor = self.get_object() - serializer = self.get_serializer(actor) - return Response(serializer.data, status=status.HTTP_200_OK) + def get(self, request, *args, **kwargs): + return self.list(request, *args, **kwargs) - def post(self, request): - serializer = self.get_serializer(data=request.data) - if serializer.is_valid(): - serializer.save() - return Response(serializer.data, status=status.HTTP_201_CREATED) - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + def post(self, request, *args, **kwargs): + return self.create(request, *args, **kwargs) - def put(self, request): - actor = self.get_object() - serializer = self.get_serializer(actor, data=request.data) - if serializer.is_valid(): - serializer.save() - return Response(serializer.data, status=status.HTTP_200_OK) - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) - def patch(self, request): - actor = self.get_object() - serializer = self.get_serializer(actor, data=request.data) - if serializer.is_valid(): - serializer.save() - return Response(serializer.data, status=status.HTTP_200_OK) - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) +class ActorDetail(mixins.RetrieveModelMixin, + mixins.UpdateModelMixin, + mixins.DestroyModelMixin, + generics.GenericAPIView): + queryset = Actor.objects.all() + serializer_class = ActorSerializer - def delete(self, request): - actor = self.get_object() - actor.delete() - return Response(status=status.HTTP_204_NO_CONTENT) + 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) class CinemaHallViewSet( @@ -105,6 +102,6 @@ class CinemaHallViewSet( serializer_class = CinemaHallSerializer -class MovieModelViewSet(viewsets.ModelViewSet): +class MovieViewSet(viewsets.ModelViewSet): queryset = Movie.objects.all() serializer_class = MovieSerializer