Skip to content

Commit

Permalink
Refactor Genre and Actor views, update URL patterns.
Browse files Browse the repository at this point in the history
Replaced `GenreAPIView` with `GenreList` and added `GenreDetail` for better REST structure. Simplified `ActorDetail` and `ActorList` by using generic views. Updated URL patterns and serializers to align with these changes.
  • Loading branch information
frezworx committed Dec 19, 2024
1 parent 4923b60 commit 02041dc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
1 change: 1 addition & 0 deletions cinema/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,4 @@ def update(self, instance, validated_data):
instance.seats_in_row
)
instance.save()
return instance
14 changes: 7 additions & 7 deletions cinema/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@
from rest_framework.routers import DefaultRouter

from cinema.views import (
GenreAPIView,
GenreList,
CinemaHallViewSet,
MovieViewSet,
ActorList,
ActorDetail,
ActorList, ActorDetail, GenreDetail,
# ActorDetail,
)

router = DefaultRouter()
router.register("cinema-halls", CinemaHallViewSet, basename="cinema-hall")
router.register("movies", MovieViewSet, basename="movies")
router.register("cinema_halls", CinemaHallViewSet, basename="cinema_hall")
router.register("movies", MovieViewSet, basename="movie")

urlpatterns = [
path("genres/", GenreAPIView.as_view(), name="genre-list"),
path("genres/", GenreList.as_view(), name="genre-list"),
path("genres/<int:pk>/", GenreDetail.as_view(), name="genre-detail"),
path("actors/", ActorList.as_view(), name="actor-list"),
path("actors/<int:pk>/", ActorDetail.as_view(), name="actor-detail"),
path("", include(router.urls)),
path("", include(router.urls))
]

app_name = "cinema"
41 changes: 23 additions & 18 deletions cinema/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.http import Http404
from rest_framework.mixins import (
ListModelMixin,
CreateModelMixin,
Expand All @@ -6,7 +7,8 @@
DestroyModelMixin,
)
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView
from rest_framework.generics import GenericAPIView, RetrieveAPIView, \
UpdateAPIView, CreateAPIView, DestroyAPIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework.viewsets import GenericViewSet, ModelViewSet
Expand All @@ -20,7 +22,7 @@
)


class GenreAPIView(APIView):
class GenreList(APIView):
def get(self, request):
genres = Genre.objects.all()
serializer = GenreSerializer(genres, many=True)
Expand All @@ -34,27 +36,28 @@ def post(self, request):
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


class GenreDetail(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)


class ActorDetail(
GenericAPIView,
RetrieveModelMixin,
UpdateModelMixin,
DestroyModelMixin,
CreateAPIView,
DestroyAPIView,
UpdateAPIView,
RetrieveAPIView,
):
queryset = Actor.objects.all()
serializer_class = ActorSerializer

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 ActorList(
GenericAPIView,
Expand All @@ -68,6 +71,8 @@ class ActorList(
serializer_class = ActorSerializer

def get(self, request, *args, **kwargs):
if "pk" in kwargs:
return self.retrieve(request, *args, **kwargs)
return self.list(request, *args, **kwargs)

def post(self, request, *args, **kwargs):
Expand Down

0 comments on commit 02041dc

Please sign in to comment.