-
Notifications
You must be signed in to change notification settings - Fork 722
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
72 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,116 +1,121 @@ | ||
from rest_framework.response import Response | ||
from rest_framework import status, viewsets, mixins | ||
from django.http import Http404 | ||
from rest_framework.views import APIView | ||
from rest_framework.generics import GenericAPIView | ||
from cinema.models import Actor, Genre, CinemaHall, Movie | ||
from cinema.serializers import ( | ||
from rest_framework.viewsets import ModelViewSet, GenericViewSet | ||
from rest_framework.response import Response | ||
from rest_framework import status | ||
from .models import Actor, Genre, CinemaHall, Movie | ||
from .serializers import ( | ||
ActorSerializer, | ||
GenreSerializer, | ||
CinemaHallSerializer, | ||
MovieSerializer, | ||
) | ||
|
||
|
||
class ActorListCreateView(GenericAPIView): | ||
queryset = Actor.objects.all() | ||
serializer_class = ActorSerializer | ||
|
||
def get(self, request, *args, **kwargs): | ||
actors = self.get_queryset() | ||
serializer = self.get_serializer(actors, many=True) | ||
class GenreAPIView(APIView): | ||
def get(self, request): | ||
genres = Genre.objects.all() | ||
serializer = GenreSerializer(genres, many=True) | ||
return Response(serializer.data) | ||
|
||
def post(self, request, *args, **kwargs): | ||
serializer = self.get_serializer(data=request.data) | ||
def post(self, request): | ||
serializer = GenreSerializer(data=request.data) | ||
serializer.is_valid(raise_exception=True) | ||
serializer.save() | ||
return Response(serializer.data, status=status.HTTP_201_CREATED) | ||
|
||
|
||
class ActorRetrieveUpdateDestroyView(GenericAPIView): | ||
queryset = Actor.objects.all() | ||
serializer_class = ActorSerializer | ||
|
||
def get(self, request, pk, *args, **kwargs): | ||
actor = self.get_object() | ||
serializer = self.get_serializer(actor) | ||
class GenreDetailAPIView(APIView): | ||
def get_object(self, pk): | ||
try: | ||
return Genre.objects.get(pk=pk) | ||
except Genre.DoesNotExist: | ||
raise Http404 | ||
|
||
def get(self, request, pk): | ||
genre = self.get_object(pk) | ||
serializer = GenreSerializer(genre) | ||
return Response(serializer.data) | ||
|
||
def put(self, request, pk, *args, **kwargs): | ||
actor = self.get_object() | ||
serializer = self.get_serializer(actor, data=request.data) | ||
def put(self, request, pk): | ||
genre = self.get_object(pk) | ||
serializer = GenreSerializer(genre, data=request.data) | ||
serializer.is_valid(raise_exception=True) | ||
serializer.save() | ||
return Response(serializer.data) | ||
|
||
def patch(self, request, pk, *args, **kwargs): | ||
actor = self.get_object() | ||
serializer = self.get_serializer( | ||
actor, | ||
data=request.data, | ||
partial=True | ||
) | ||
def patch(self, request, pk): | ||
genre = self.get_object(pk) | ||
serializer = GenreSerializer(genre, data=request.data, partial=True) | ||
serializer.is_valid(raise_exception=True) | ||
serializer.save() | ||
return Response(serializer.data) | ||
|
||
def delete(self, request, pk, *args, **kwargs): | ||
actor = self.get_object() | ||
actor.delete() | ||
def delete(self, request, pk): | ||
genre = self.get_object(pk) | ||
genre.delete() | ||
return Response(status=status.HTTP_204_NO_CONTENT) | ||
|
||
|
||
class GenreAPIView(APIView): | ||
def get(self, request, *args, **kwargs): | ||
genres = Genre.objects.all() | ||
serializer = GenreSerializer(genres, many=True) | ||
class ActorListCreateView(GenericAPIView): | ||
serializer_class = ActorSerializer | ||
|
||
def get(self, request): | ||
actors = Actor.objects.all() | ||
serializer = self.get_serializer(actors, many=True) | ||
return Response(serializer.data) | ||
|
||
def post(self, request, *args, **kwargs): | ||
serializer = GenreSerializer(data=request.data) | ||
def post(self, request): | ||
serializer = self.get_serializer(data=request.data) | ||
serializer.is_valid(raise_exception=True) | ||
serializer.save() | ||
return Response(serializer.data, status=status.HTTP_201_CREATED) | ||
|
||
|
||
class GenreDetailAPIView(APIView): | ||
def get(self, request, pk, *args, **kwargs): | ||
genre = Genre.objects.get(pk=pk) | ||
serializer = GenreSerializer(genre) | ||
class ActorRetrieveUpdateDestroyView(GenericAPIView): | ||
serializer_class = ActorSerializer | ||
|
||
def get_object(self, pk): | ||
try: | ||
return Actor.objects.get(pk=pk) | ||
except Actor.DoesNotExist: | ||
raise Http404 | ||
|
||
def get(self, request, pk): | ||
actor = self.get_object(pk) | ||
serializer = self.get_serializer(actor) | ||
return Response(serializer.data) | ||
|
||
def put(self, request, pk, *args, **kwargs): | ||
genre = Genre.objects.get(pk=pk) | ||
serializer = GenreSerializer(genre, data=request.data) | ||
def put(self, request, pk): | ||
actor = self.get_object(pk) | ||
serializer = self.get_serializer(actor, data=request.data) | ||
serializer.is_valid(raise_exception=True) | ||
serializer.save() | ||
return Response(serializer.data) | ||
|
||
def patch(self, request, pk, *args, **kwargs): | ||
genre = Genre.objects.get(pk=pk) | ||
serializer = GenreSerializer(genre, data=request.data, partial=True) | ||
def patch(self, request, pk): | ||
actor = self.get_object(pk) | ||
serializer = self.get_serializer( | ||
actor, | ||
data=request.data, | ||
partial=True | ||
) | ||
serializer.is_valid(raise_exception=True) | ||
serializer.save() | ||
return Response(serializer.data) | ||
|
||
def delete(self, request, pk, *args, **kwargs): | ||
genre = Genre.objects.get(pk=pk) | ||
genre.delete() | ||
def delete(self, request, pk): | ||
actor = self.get_object(pk) | ||
actor.delete() | ||
return Response(status=status.HTTP_204_NO_CONTENT) | ||
|
||
|
||
class CinemaHallViewSet( | ||
mixins.ListModelMixin, | ||
mixins.CreateModelMixin, | ||
mixins.RetrieveModelMixin, | ||
mixins.UpdateModelMixin, | ||
mixins.DestroyModelMixin, | ||
viewsets.GenericViewSet, | ||
): | ||
queryset = CinemaHall.objects.all() | ||
class CinemaHallViewSet(GenericViewSet): | ||
serializer_class = CinemaHallSerializer | ||
queryset = CinemaHall.objects.all() | ||
|
||
|
||
class MovieViewSet(viewsets.ModelViewSet): | ||
queryset = Movie.objects.all() | ||
class MovieViewSet(ModelViewSet): | ||
serializer_class = MovieSerializer | ||
queryset = Movie.objects.all() |