Skip to content

Commit

Permalink
fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriZhok committed Sep 25, 2024
1 parent 3fc9d7f commit cc2f22a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 68 deletions.
9 changes: 4 additions & 5 deletions cinema/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@
name="actor-detail",
),
path("genres/", GenreAPIView.as_view(), name="genre-list-create"),
path(
"genres/<int:pk>/",
GenreDetailAPIView.as_view(),
name="genre-detail"
),
path("genres/<int:pk>/",
GenreDetailAPIView.as_view(),
name="genre-detail"
),
path("", include(router.urls)),
]
131 changes: 68 additions & 63 deletions cinema/views.py
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()

0 comments on commit cc2f22a

Please sign in to comment.