-
Notifications
You must be signed in to change notification settings - Fork 702
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 #748
base: master
Are you sure you want to change the base?
Solution #748
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from rest_framework.pagination import PageNumberPagination | ||
|
||
|
||
class OrdersPagination(PageNumberPagination): | ||
page_size = 10 | ||
page_size_query_param = "page_size" | ||
max_page_size = 100 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,16 @@ | ||
from django.db.models import F, Count | ||
from rest_framework import viewsets | ||
|
||
from cinema.models import Genre, Actor, CinemaHall, Movie, MovieSession | ||
from cinema.models import ( | ||
Genre, | ||
Actor, | ||
CinemaHall, | ||
Movie, | ||
MovieSession, | ||
Order, | ||
Ticket, | ||
) | ||
from cinema.pagination import OrdersPagination | ||
|
||
from cinema.serializers import ( | ||
GenreSerializer, | ||
|
@@ -12,6 +22,10 @@ | |
MovieDetailSerializer, | ||
MovieSessionDetailSerializer, | ||
MovieListSerializer, | ||
OrderSerializer, | ||
TicketSerializer, | ||
TicketDetailSerializer, | ||
OrderDetailSerializer, | ||
) | ||
|
||
|
||
|
@@ -43,6 +57,28 @@ def get_serializer_class(self): | |
|
||
return MovieSerializer | ||
|
||
def get_queryset(self): | ||
queryset = super().get_queryset() | ||
if self.action == ("list", "retrieve"): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The condition |
||
queryset = self.queryset.prefetch_related("actors", "genres") | ||
|
||
actors = self.request.query_params.get("actors") | ||
genres = self.request.query_params.get("genres") | ||
title = self.request.query_params.get("title") | ||
|
||
if actors: | ||
actors_ids = [int(str_id) for str_id in actors.split(",")] | ||
queryset = queryset.filter(actors__id__in=actors_ids) | ||
|
||
if genres: | ||
genres_ids = [int(str_id) for str_id in genres.split(",")] | ||
queryset = queryset.filter(genres__id__in=genres_ids) | ||
|
||
if title: | ||
queryset = queryset.filter(title__icontains=title) | ||
|
||
return queryset.distinct() | ||
|
||
|
||
class MovieSessionViewSet(viewsets.ModelViewSet): | ||
queryset = MovieSession.objects.all() | ||
|
@@ -56,3 +92,52 @@ def get_serializer_class(self): | |
return MovieSessionDetailSerializer | ||
|
||
return MovieSessionSerializer | ||
|
||
def get_queryset(self): | ||
queryset = super().get_queryset() | ||
|
||
if self.action == "retrieve": | ||
queryset = queryset.select_related("movie") | ||
|
||
elif self.action == "list": | ||
queryset = ( | ||
queryset | ||
.select_related("cinema_hall") | ||
.annotate( | ||
tickets_available=( | ||
F("cinema_hall__rows") | ||
* F("cinema_hall__seats_in_row") | ||
- Count("tickets") | ||
) | ||
) | ||
).order_by("id") | ||
|
||
movie = self.request.query_params.get("movie") | ||
date = self.request.query_params.get("date") | ||
|
||
if movie: | ||
queryset = queryset.filter(movie__id=movie) | ||
|
||
if date: | ||
queryset = queryset.filter(show_time__date=date) | ||
|
||
return queryset.distinct() | ||
|
||
|
||
class OrderViewSet(viewsets.ModelViewSet): | ||
queryset = Order.objects.all() | ||
pagination_class = OrdersPagination | ||
|
||
def get_serializer_class(self): | ||
if self.action in ["create", "update", "partial_update"]: | ||
return OrderSerializer | ||
return OrderDetailSerializer | ||
|
||
def get_queryset(self): | ||
return Order.objects.filter(user=self.request.user) | ||
|
||
def perform_create(self, serializer): | ||
serializer.save(user=self.request.user) | ||
|
||
class TicketViewSet(viewsets.ModelViewSet): | ||
queryset = Ticket.objects.all() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
data.get
method should use parentheses instead of square brackets. Correct it todata.get('movie_session')
,data.get('row')
, anddata.get('seat')
.