-
Notifications
You must be signed in to change notification settings - Fork 707
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 #745
base: master
Are you sure you want to change the base?
Develop #745
Changes from 5 commits
2bb7697
5a2a991
28fe5be
c711f53
c5801fe
efaf20e
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 |
---|---|---|
@@ -1,6 +1,16 @@ | ||
from django.db import transaction | ||
from rest_framework import serializers | ||
from rest_framework.generics import get_object_or_404 | ||
|
||
from cinema.models import Genre, Actor, CinemaHall, Movie, MovieSession | ||
from cinema.models import ( | ||
Genre, | ||
Actor, | ||
CinemaHall, | ||
Movie, | ||
MovieSession, | ||
Order, | ||
Ticket | ||
) | ||
|
||
|
||
class GenreSerializer(serializers.ModelSerializer): | ||
|
@@ -59,6 +69,7 @@ class MovieSessionListSerializer(MovieSessionSerializer): | |
cinema_hall_capacity = serializers.IntegerField( | ||
source="cinema_hall.capacity", read_only=True | ||
) | ||
tickets_available = serializers.IntegerField(read_only=True) | ||
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 |
||
|
||
class Meta: | ||
model = MovieSession | ||
|
@@ -68,13 +79,63 @@ class Meta: | |
"movie_title", | ||
"cinema_hall_name", | ||
"cinema_hall_capacity", | ||
"tickets_available", | ||
) | ||
|
||
|
||
class TicketTakenSeatsSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Ticket | ||
fields = ["row", "seat"] | ||
|
||
|
||
class MovieSessionDetailSerializer(MovieSessionSerializer): | ||
movie = MovieListSerializer(many=False, read_only=True) | ||
cinema_hall = CinemaHallSerializer(many=False, read_only=True) | ||
taken_places = TicketTakenSeatsSerializer( | ||
source="tickets", | ||
many=True, | ||
read_only=True | ||
) | ||
|
||
class Meta: | ||
model = MovieSession | ||
fields = ("id", "show_time", "movie", "cinema_hall") | ||
fields = ("id", "show_time", "movie", "cinema_hall", "taken_places") | ||
|
||
|
||
class TicketListSerializer(serializers.ModelSerializer): | ||
movie_session = MovieSessionListSerializer(read_only=True) | ||
|
||
class Meta: | ||
model = Ticket | ||
fields = ["id", "row", "seat", "movie_session"] | ||
|
||
|
||
class OrderListSerializer(serializers.ModelSerializer): | ||
tickets = TicketListSerializer(many=True) | ||
|
||
class Meta: | ||
model = Order | ||
fields = ["tickets", "created_at"] | ||
|
||
|
||
class TicketCreateSerializer(TicketListSerializer): | ||
movie_session = serializers.PrimaryKeyRelatedField( | ||
queryset=MovieSession.objects.all() | ||
) | ||
|
||
|
||
class OrderCreateSerializer(OrderListSerializer): | ||
tickets = TicketCreateSerializer(many=True) | ||
|
||
@transaction.atomic | ||
def create(self, validated_data): | ||
tickets_data = validated_data.pop("tickets") | ||
|
||
order = Order.objects.create(**validated_data) | ||
tickets = [ | ||
Ticket(order=order, **ticket_data) | ||
for ticket_data in tickets_data | ||
] | ||
Ticket.objects.bulk_create(tickets) | ||
return order | ||
Comment on lines
+131
to
+141
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. Ensure that the ticket creation logic within the |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
from django.db.models import F, Count | ||
from rest_framework import viewsets | ||
|
||
from cinema.models import Genre, Actor, CinemaHall, Movie, MovieSession | ||
from rest_framework.pagination import PageNumberPagination | ||
|
||
from cinema.models import ( | ||
Genre, | ||
Actor, | ||
CinemaHall, | ||
Movie, | ||
MovieSession, | ||
Order | ||
) | ||
|
||
from cinema.serializers import ( | ||
GenreSerializer, | ||
|
@@ -12,6 +21,7 @@ | |
MovieDetailSerializer, | ||
MovieSessionDetailSerializer, | ||
MovieListSerializer, | ||
OrderListSerializer, OrderCreateSerializer, | ||
) | ||
|
||
|
||
|
@@ -34,6 +44,24 @@ class MovieViewSet(viewsets.ModelViewSet): | |
queryset = Movie.objects.all() | ||
serializer_class = MovieSerializer | ||
|
||
def get_queryset(self): | ||
queryset = self.queryset | ||
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 = Movie.objects.filter(actors__id__in=actors_ids) | ||
elif genres: | ||
genres_ids = [int(str_id) for str_id in genres.split(",")] | ||
queryset = Movie.objects.filter(genres__id__in=genres_ids) | ||
elif title: | ||
queryset = Movie.objects.filter(title__contains=title) | ||
Comment on lines
+54
to
+59
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 current implementation of |
||
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 = Movie.objects.prefetch_related("genres", "actors") | ||
return queryset | ||
return queryset.prefetch_related("genres", "actors") | ||
|
||
def get_serializer_class(self): | ||
if self.action == "list": | ||
return MovieListSerializer | ||
|
@@ -48,6 +76,26 @@ class MovieSessionViewSet(viewsets.ModelViewSet): | |
queryset = MovieSession.objects.all() | ||
serializer_class = MovieSessionSerializer | ||
|
||
def get_queryset(self): | ||
queryset = self.queryset | ||
date = self.request.query_params.get("date") | ||
movie = self.request.query_params.get("movie") | ||
if date: | ||
queryset = queryset.filter(show_time__date=date) | ||
if movie: | ||
queryset = queryset.filter(movie_id=movie) | ||
if self.action == "list": | ||
queryset = ( | ||
queryset | ||
.select_related("movie", "cinema_hall") | ||
.annotate( | ||
tickets_available=F("cinema_hall__rows") | ||
* F("cinema_hall__seats_in_row") | ||
- Count("tickets")) | ||
).order_by("id") | ||
return queryset | ||
return queryset | ||
|
||
def get_serializer_class(self): | ||
if self.action == "list": | ||
return MovieSessionListSerializer | ||
|
@@ -56,3 +104,31 @@ def get_serializer_class(self): | |
return MovieSessionDetailSerializer | ||
|
||
return MovieSessionSerializer | ||
|
||
|
||
class OrderSetPagination(PageNumberPagination): | ||
page_size = 1 | ||
page_size_query_param = "page_size" | ||
max_page_size = 1 | ||
Comment on lines
+109
to
+111
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 pagination settings are very restrictive with a
Comment on lines
+109
to
+111
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 pagination settings in |
||
|
||
|
||
class OrderViewSet(viewsets.ModelViewSet): | ||
queryset = Order.objects.all() | ||
pagination_class = OrderSetPagination | ||
|
||
def get_queryset(self): | ||
queryset = self.queryset.filter(user=self.request.user) | ||
if self.action == "list": | ||
return queryset.prefetch_related( | ||
"tickets__movie_session__movie", | ||
"tickets__movie_session__cinema_hall" | ||
) | ||
return queryset | ||
|
||
def perform_create(self, serializer): | ||
serializer.save(user=self.request.user) | ||
|
||
def get_serializer_class(self): | ||
if self.action == "create": | ||
return OrderCreateSerializer | ||
return OrderListSerializer |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,7 +124,7 @@ | |
|
||
USE_I18N = True | ||
|
||
USE_TZ = False | ||
USE_TZ = True | ||
|
||
|
||
# Static files (CSS, JavaScript, Images) | ||
|
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
tickets_available
field is marked as read-only but does not have a source or method to calculate its value. Ensure that there is a method or property in theMovieSession
model to provide this data.