From 7d10089162c82830e6e789fd35d8544b04806d9f Mon Sep 17 00:00:00 2001 From: FursykIvan Date: Wed, 18 Dec 2024 07:41:16 +0200 Subject: [PATCH] Solution --- cinema/views.py | 48 ++++++++++++++++++++++++++++++------- cinema_service/settings.py | 5 +++- cinema_service/urls.py | 1 + requirements.txt | Bin 152 -> 552 bytes user/permissions.py | 13 ++++++++++ user/serializers.py | 25 ++++++++++++++++++- user/urls.py | 12 +++++++++- user/views.py | 25 ++++++++++++++++++- 8 files changed, 117 insertions(+), 12 deletions(-) create mode 100644 user/permissions.py diff --git a/cinema/views.py b/cinema/views.py index a191bf5f..eb152642 100644 --- a/cinema/views.py +++ b/cinema/views.py @@ -1,10 +1,19 @@ from datetime import datetime from django.db.models import F, Count -from rest_framework import viewsets +from rest_framework import viewsets, mixins +from rest_framework.authentication import TokenAuthentication from rest_framework.pagination import PageNumberPagination - -from cinema.models import Genre, Actor, CinemaHall, Movie, MovieSession, Order +from rest_framework.permissions import IsAuthenticated + +from cinema.models import ( + Genre, + Actor, + CinemaHall, + Movie, + MovieSession, + Order +) from cinema.serializers import ( GenreSerializer, @@ -19,26 +28,43 @@ OrderSerializer, OrderListSerializer, ) +from user.permissions import IsAdminOrIfAuthenticatedReadOnly -class GenreViewSet(viewsets.ModelViewSet): +class GenreViewSet(viewsets.GenericViewSet, + mixins.ListModelMixin, + mixins.CreateModelMixin): queryset = Genre.objects.all() serializer_class = GenreSerializer + authentication_classes = (TokenAuthentication,) + permission_classes = (IsAdminOrIfAuthenticatedReadOnly,) -class ActorViewSet(viewsets.ModelViewSet): +class ActorViewSet(viewsets.GenericViewSet, + mixins.ListModelMixin, + mixins.CreateModelMixin): queryset = Actor.objects.all() serializer_class = ActorSerializer + authentication_classes = (TokenAuthentication,) + permission_classes = (IsAdminOrIfAuthenticatedReadOnly,) -class CinemaHallViewSet(viewsets.ModelViewSet): +class CinemaHallViewSet(viewsets.GenericViewSet, + mixins.ListModelMixin, + mixins.CreateModelMixin): queryset = CinemaHall.objects.all() serializer_class = CinemaHallSerializer + authentication_classes = (TokenAuthentication,) + permission_classes = (IsAdminOrIfAuthenticatedReadOnly,) -class MovieViewSet(viewsets.ModelViewSet): +class MovieViewSet(viewsets.GenericViewSet, + mixins.ListModelMixin, + mixins.CreateModelMixin): queryset = Movie.objects.prefetch_related("genres", "actors") serializer_class = MovieSerializer + authentication_classes = (TokenAuthentication,) + permission_classes = (IsAdminOrIfAuthenticatedReadOnly,) @staticmethod def _params_to_ints(qs): @@ -87,6 +113,8 @@ class MovieSessionViewSet(viewsets.ModelViewSet): ) ) serializer_class = MovieSessionSerializer + authentication_classes = (TokenAuthentication,) + permission_classes = (IsAdminOrIfAuthenticatedReadOnly,) def get_queryset(self): date = self.request.query_params.get("date") @@ -118,12 +146,16 @@ class OrderPagination(PageNumberPagination): max_page_size = 100 -class OrderViewSet(viewsets.ModelViewSet): +class OrderViewSet(viewsets.GenericViewSet, + mixins.ListModelMixin, + mixins.CreateModelMixin): queryset = Order.objects.prefetch_related( "tickets__movie_session__movie", "tickets__movie_session__cinema_hall" ) serializer_class = OrderSerializer pagination_class = OrderPagination + authentication_classes = (TokenAuthentication,) + permission_classes = (IsAuthenticated,) def get_queryset(self): return Order.objects.filter(user=self.request.user) diff --git a/cinema_service/settings.py b/cinema_service/settings.py index 29ea7dea..9030126c 100644 --- a/cinema_service/settings.py +++ b/cinema_service/settings.py @@ -12,6 +12,9 @@ from pathlib import Path +from django.urls import reverse_lazy + + # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -125,7 +128,7 @@ USE_I18N = True -USE_TZ = False +USE_TZ = True # Static files (CSS, JavaScript, Images) diff --git a/cinema_service/urls.py b/cinema_service/urls.py index bf903c00..a282f874 100644 --- a/cinema_service/urls.py +++ b/cinema_service/urls.py @@ -4,5 +4,6 @@ urlpatterns = [ path("admin/", admin.site.urls), path("api/cinema/", include("cinema.urls", namespace="cinema")), + path("api/user/", include("user.urls", namespace="user")), path("__debug__/", include("debug_toolbar.urls")), ] diff --git a/requirements.txt b/requirements.txt index 56e1355473bd442e9781dbeb838fd90a63b869bf..ec476ab23a55b36c9ae78a541ae402770ed8cf70 100644 GIT binary patch literal 552 zcmY*WNe+TQ5UjI_Pr=Ay)PpxaG71>nKtT;YUajiM5DXcTp_Z;Lz8{Ac2UNJDK#2{X z6f>-NVoWi?7DqT-$*B#^1|%OL5pwExc#s#NBe%wxcSqmgnNXRT=UAz^Efpo14j0~D z)ZTF#bTJlGCZ;Uu>WCFgC|@D-!FP;CxG_ylXZeQ=+Rg9v&wX0bitimKND*;opU0~; zMQ33-F4*%CyD)DdOJk_BhF=>d3?7yBrwro7*0A5FK~JCB+EWX?O&3#bt&Qy+do~ny r_ld;t`v*pcx1~uqCt=s2wU%9+nMi-59VM1JwSA<_iFZ7H`oF^u2c%N1 literal 152 zcmX|)O%8(~5QX>L1tOw}n>ox7Mq;Im1#GWhMw>3*``%A@V2%th8LQTWJ1qdS8O-!k z`gL-n3gC?UNAw%ZhCWf%8J|LjUYXTF$0E*_qgd9NQHAS32l8jsjX8NNvNraMn-!{U NEq&+1Wf#