Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #683

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Add authentication and permission classes to viewsets(NOT FINISHED)
  • Loading branch information
Lev2098 committed Dec 9, 2024
commit b58d1a0607411c74b21ee205c09c15d1c5ffb704
19 changes: 19 additions & 0 deletions cinema/views.py
Original file line number Diff line number Diff line change
@@ -2,9 +2,14 @@

from django.db.models import F, Count
from rest_framework import viewsets
from rest_framework.authentication import TokenAuthentication
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import IsAdminUser, IsAuthenticated

from cinema.models import Genre, Actor, CinemaHall, Movie, MovieSession, Order
from cinema.permissions import (
IsAdminOrIfAuthenticatedReadOnly
)

from cinema.serializers import (
GenreSerializer,
@@ -24,21 +29,29 @@
class GenreViewSet(viewsets.ModelViewSet):
queryset = Genre.objects.all()
serializer_class = GenreSerializer
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAdminOrIfAuthenticatedReadOnly, IsAuthenticated)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The permission_classes for GenreViewSet includes both IsAdminOrIfAuthenticatedReadOnly and IsAuthenticated. This is redundant because IsAdminOrIfAuthenticatedReadOnly already handles both admin and authenticated user permissions. You can remove IsAuthenticated from this tuple.



class ActorViewSet(viewsets.ModelViewSet):
queryset = Actor.objects.all()
serializer_class = ActorSerializer
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAdminOrIfAuthenticatedReadOnly,)


class CinemaHallViewSet(viewsets.ModelViewSet):
queryset = CinemaHall.objects.all()
serializer_class = CinemaHallSerializer
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAdminOrIfAuthenticatedReadOnly,)


class MovieViewSet(viewsets.ModelViewSet):
queryset = Movie.objects.prefetch_related("genres", "actors")
serializer_class = MovieSerializer
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAdminOrIfAuthenticatedReadOnly,)

@staticmethod
def _params_to_ints(qs):
@@ -87,6 +100,9 @@ class MovieSessionViewSet(viewsets.ModelViewSet):
)
)
serializer_class = MovieSessionSerializer
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAdminOrIfAuthenticatedReadOnly,)


def get_queryset(self):
date = self.request.query_params.get("date")
@@ -124,6 +140,9 @@ class OrderViewSet(viewsets.ModelViewSet):
)
serializer_class = OrderSerializer
pagination_class = OrderPagination
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAdminOrIfAuthenticatedReadOnly,)


def get_queryset(self):
return Order.objects.filter(user=self.request.user)