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

solution #707

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cinema/permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from rest_framework import permissions


class IsAdminOrIfAuthenticatedReadOnly(permissions.BasePermission):
def has_permission(self, request, view):
return request.user.is_authenticated \
if request.method in permissions.SAFE_METHODS\
else request.user.is_staff
Comment on lines +6 to +8

Choose a reason for hiding this comment

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

The line continuation character \ is not needed here. Python automatically joins lines inside parentheses, brackets, and braces. You can remove the backslashes and enclose the entire expression in parentheses for better readability.

46 changes: 23 additions & 23 deletions cinema/urls.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
from django.urls import path, include
from rest_framework import routers

from cinema.views import (
GenreViewSet,
ActorViewSet,
CinemaHallViewSet,
MovieViewSet,
MovieSessionViewSet,
OrderViewSet,
)

router = routers.DefaultRouter()
router.register("genres", GenreViewSet)
router.register("actors", ActorViewSet)
router.register("cinema_halls", CinemaHallViewSet)
router.register("movies", MovieViewSet)
router.register("movie_sessions", MovieSessionViewSet)
router.register("orders", OrderViewSet)

urlpatterns = [path("", include(router.urls))]

app_name = "cinema"
from django.urls import path, include
from rest_framework import routers
from cinema.views import (
GenreViewSet,
ActorViewSet,
CinemaHallViewSet,
MovieViewSet,
MovieSessionViewSet,
OrderViewSet,
)
router = routers.DefaultRouter()
router.register("genres", GenreViewSet)
router.register("actors", ActorViewSet)
router.register("cinema_halls", CinemaHallViewSet)
router.register("movies", MovieViewSet)
router.register("movie_sessions", MovieSessionViewSet)
router.register("orders", OrderViewSet)
urlpatterns = [path("", include(router.urls))]
app_name = "cinema"
302 changes: 164 additions & 138 deletions cinema/views.py
Original file line number Diff line number Diff line change
@@ -1,138 +1,164 @@
from datetime import datetime

from django.db.models import F, Count
from rest_framework import viewsets
from rest_framework.pagination import PageNumberPagination

from cinema.models import Genre, Actor, CinemaHall, Movie, MovieSession, Order

from cinema.serializers import (
GenreSerializer,
ActorSerializer,
CinemaHallSerializer,
MovieSerializer,
MovieSessionSerializer,
MovieSessionListSerializer,
MovieDetailSerializer,
MovieSessionDetailSerializer,
MovieListSerializer,
OrderSerializer,
OrderListSerializer,
)


class GenreViewSet(viewsets.ModelViewSet):
queryset = Genre.objects.all()
serializer_class = GenreSerializer


class ActorViewSet(viewsets.ModelViewSet):
queryset = Actor.objects.all()
serializer_class = ActorSerializer


class CinemaHallViewSet(viewsets.ModelViewSet):
queryset = CinemaHall.objects.all()
serializer_class = CinemaHallSerializer


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

@staticmethod
def _params_to_ints(qs):
"""Converts a list of string IDs to a list of integers"""
return [int(str_id) for str_id in qs.split(",")]

def get_queryset(self):
"""Retrieve the movies with filters"""
title = self.request.query_params.get("title")
genres = self.request.query_params.get("genres")
actors = self.request.query_params.get("actors")

queryset = self.queryset

if title:
queryset = queryset.filter(title__icontains=title)

if genres:
genres_ids = self._params_to_ints(genres)
queryset = queryset.filter(genres__id__in=genres_ids)

if actors:
actors_ids = self._params_to_ints(actors)
queryset = queryset.filter(actors__id__in=actors_ids)

return queryset.distinct()

def get_serializer_class(self):
if self.action == "list":
return MovieListSerializer

if self.action == "retrieve":
return MovieDetailSerializer

return MovieSerializer


class MovieSessionViewSet(viewsets.ModelViewSet):
queryset = (
MovieSession.objects.all()
.select_related("movie", "cinema_hall")
.annotate(
tickets_available=F("cinema_hall__rows")
* F("cinema_hall__seats_in_row")
- Count("tickets")
)
)
serializer_class = MovieSessionSerializer

def get_queryset(self):
date = self.request.query_params.get("date")
movie_id_str = self.request.query_params.get("movie")

queryset = self.queryset

if date:
date = datetime.strptime(date, "%Y-%m-%d").date()
queryset = queryset.filter(show_time__date=date)

if movie_id_str:
queryset = queryset.filter(movie_id=int(movie_id_str))

return queryset

def get_serializer_class(self):
if self.action == "list":
return MovieSessionListSerializer

if self.action == "retrieve":
return MovieSessionDetailSerializer

return MovieSessionSerializer


class OrderPagination(PageNumberPagination):
page_size = 10
max_page_size = 100


class OrderViewSet(viewsets.ModelViewSet):
queryset = Order.objects.prefetch_related(
"tickets__movie_session__movie", "tickets__movie_session__cinema_hall"
)
serializer_class = OrderSerializer
pagination_class = OrderPagination

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

def get_serializer_class(self):
if self.action == "list":
return OrderListSerializer

return OrderSerializer

def perform_create(self, serializer):
serializer.save(user=self.request.user)
from datetime import datetime

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

from cinema.models import Genre, Actor, CinemaHall, Movie, MovieSession, Order

from cinema.serializers import (
GenreSerializer,
ActorSerializer,
CinemaHallSerializer,
MovieSerializer,
MovieSessionSerializer,
MovieSessionListSerializer,
MovieDetailSerializer,
MovieSessionDetailSerializer,
MovieListSerializer,
OrderSerializer,
OrderListSerializer,
)


class GenreViewSet(mixins.CreateModelMixin,
mixins.ListModelMixin,
viewsets.GenericViewSet):
queryset = Genre.objects.all()
serializer_class = GenreSerializer
authentication_classes = [TokenAuthentication]
permission_classes = [IsAdminOrIfAuthenticatedReadOnly]


class ActorViewSet(mixins.CreateModelMixin,
mixins.ListModelMixin,
viewsets.GenericViewSet):
queryset = Actor.objects.all()
serializer_class = ActorSerializer
authentication_classes = [TokenAuthentication]
permission_classes = [IsAdminOrIfAuthenticatedReadOnly]


class CinemaHallViewSet(mixins.CreateModelMixin,
mixins.ListModelMixin,
viewsets.GenericViewSet):
queryset = CinemaHall.objects.all()
serializer_class = CinemaHallSerializer
authentication_classes = [TokenAuthentication]
permission_classes = [IsAdminOrIfAuthenticatedReadOnly]


class MovieViewSet(mixins.CreateModelMixin,
mixins.ListModelMixin,
mixins.RetrieveModelMixin,
viewsets.GenericViewSet):
queryset = Movie.objects.prefetch_related("genres", "actors")
serializer_class = MovieSerializer
authentication_classes = [TokenAuthentication]
permission_classes = [IsAdminOrIfAuthenticatedReadOnly]

@staticmethod
def _params_to_ints(qs):
"""Converts a list of string IDs to a list of integers"""
return [int(str_id) for str_id in qs.split(",")]

def get_queryset(self):
"""Retrieve the movies with filters"""
title = self.request.query_params.get("title")
genres = self.request.query_params.get("genres")
actors = self.request.query_params.get("actors")

queryset = self.queryset

if title:
queryset = queryset.filter(title__icontains=title)

if genres:
genres_ids = self._params_to_ints(genres)
queryset = queryset.filter(genres__id__in=genres_ids)

if actors:
actors_ids = self._params_to_ints(actors)
queryset = queryset.filter(actors__id__in=actors_ids)

return queryset.distinct()

def get_serializer_class(self):
if self.action == "list":
return MovieListSerializer

if self.action == "retrieve":
return MovieDetailSerializer

return MovieSerializer


class MovieSessionViewSet(viewsets.ModelViewSet):
queryset = (
MovieSession.objects.all()
.select_related("movie", "cinema_hall")
.annotate(
tickets_available=F("cinema_hall__rows")
* F("cinema_hall__seats_in_row")
- Count("tickets")
)
)
serializer_class = MovieSessionSerializer
authentication_classes = [TokenAuthentication]
permission_classes = [IsAdminOrIfAuthenticatedReadOnly]

def get_queryset(self):
date = self.request.query_params.get("date")
movie_id_str = self.request.query_params.get("movie")

queryset = self.queryset

if date:
date = datetime.strptime(date, "%Y-%m-%d").date()
queryset = queryset.filter(show_time__date=date)

if movie_id_str:
queryset = queryset.filter(movie_id=int(movie_id_str))

return queryset

def get_serializer_class(self):
if self.action == "list":
return MovieSessionListSerializer

if self.action == "retrieve":
return MovieSessionDetailSerializer

return MovieSessionSerializer


class OrderPagination(PageNumberPagination):
page_size = 10
max_page_size = 100


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)

Comment on lines +154 to +155

Choose a reason for hiding this comment

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

The get_queryset method in OrderViewSet redefines the queryset with Order.objects.filter(user=self.request.user). Since the queryset attribute is already defined, you can simply filter it instead of redefining it. Consider using self.queryset.filter(user=self.request.user) to maintain consistency and avoid redundancy.

def get_serializer_class(self):
if self.action == "list":
return OrderListSerializer

return OrderSerializer

def perform_create(self, serializer):
serializer.save(user=self.request.user)
17 changes: 9 additions & 8 deletions cinema_service/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
path("admin/", admin.site.urls),
path("api/cinema/", include("cinema.urls", namespace="cinema")),
path("__debug__/", include("debug_toolbar.urls")),
]
from django.contrib import admin
from django.urls import path, include

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")),
]
Loading
Loading