-
Notifications
You must be signed in to change notification settings - Fork 637
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
implement py-tickets-orders #650
base: master
Are you sure you want to change the base?
Changes from 4 commits
4547e1b
89a321b
1cf6814
4cce13d
194084a
ae1a5a3
d96a857
e57aa5a
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,24 @@ | ||
# Generated by Django 4.1 on 2024-10-01 14:32 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('cinema', '0004_alter_genre_name'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='moviesession', | ||
name='cinema_hall', | ||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='movie_sessions', to='cinema.cinemahall'), | ||
), | ||
migrations.AlterField( | ||
model_name='moviesession', | ||
name='movie', | ||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='movie_sessions', to='cinema.movie'), | ||
), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from rest_framework.pagination import PageNumberPagination | ||
|
||
|
||
class OrderSetPagination(PageNumberPagination): | ||
page_size = 1 | ||
page_size_query_param = "page_size" | ||
max_page_size = 10 | ||
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.fields import SerializerMethodField | ||
|
||
from cinema.models import Genre, Actor, CinemaHall, Movie, MovieSession | ||
from cinema.models import ( | ||
Genre, | ||
Actor, | ||
CinemaHall, | ||
Movie, | ||
MovieSession, | ||
Order, | ||
Ticket | ||
) | ||
|
||
|
||
class GenreSerializer(serializers.ModelSerializer): | ||
|
@@ -48,7 +58,7 @@ class Meta: | |
class MovieSessionSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = MovieSession | ||
fields = ("id", "show_time", "movie", "cinema_hall") | ||
fields = ("id", "show_time", "movie", "cinema_hall", ) | ||
|
||
|
||
class MovieSessionListSerializer(MovieSessionSerializer): | ||
|
@@ -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) | ||
|
||
class Meta: | ||
model = MovieSession | ||
|
@@ -68,13 +79,61 @@ class Meta: | |
"movie_title", | ||
"cinema_hall_name", | ||
"cinema_hall_capacity", | ||
"tickets_available" | ||
) | ||
|
||
|
||
class TicketSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Ticket | ||
fields = ("id", "row", "seat", "movie_session") | ||
|
||
def validate(self, attrs): | ||
Ticket.validate_seat_and_row( | ||
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. better call super() here and save the result, then after validation return result 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. This to be 100% sure that you return the same thing that is in the parent class |
||
attrs["seat"], | ||
attrs["movie_session"].cinema_hall.seats_in_row, | ||
attrs["row"], | ||
attrs["movie_session"].cinema_hall.rows, | ||
serializers.ValidationError, | ||
) | ||
return attrs | ||
|
||
|
||
class MovieSessionDetailSerializer(MovieSessionSerializer): | ||
movie = MovieListSerializer(many=False, read_only=True) | ||
cinema_hall = CinemaHallSerializer(many=False, read_only=True) | ||
taken_places = SerializerMethodField() | ||
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. You can use nested serializer here - create TicketSeatsSerializer with row / seat fields and pass it to taken_places |
||
|
||
class Meta: | ||
model = MovieSession | ||
fields = ("id", "show_time", "movie", "cinema_hall") | ||
fields = ("id", "show_time", "movie", "cinema_hall", "taken_places") | ||
|
||
def get_taken_places(self, movie_session): | ||
return [ | ||
{"row": ticket.row, "seat": ticket.seat} | ||
for ticket in movie_session.tickets.all() | ||
] | ||
|
||
|
||
class TicketRetrieveSerializer(TicketSerializer): | ||
movie_session = MovieSessionListSerializer(many=False, 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. many is False be default |
||
|
||
|
||
class OrderSerializer(serializers.ModelSerializer): | ||
tickets = TicketSerializer(many=True, read_only=False, allow_empty=False) | ||
|
||
class Meta: | ||
model = Order | ||
fields = ("id", "tickets", "created_at") | ||
|
||
def create(self, validated_data): | ||
with transaction.atomic(): | ||
tickets_data = validated_data.pop("tickets") | ||
order = Order.objects.create(**validated_data) | ||
for ticket_data in tickets_data: | ||
Ticket.objects.create(order=order, **ticket_data) | ||
return order | ||
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. In create/update/delete fucntion you don't need return anything |
||
|
||
|
||
class OrderListSerializer(OrderSerializer): | ||
tickets = TicketRetrieveSerializer(many=True, read_only=True) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
from django.db.models import Count, F | ||
from rest_framework import viewsets | ||
|
||
from cinema.models import Genre, Actor, CinemaHall, Movie, MovieSession | ||
|
||
from cinema.models import Genre, Actor, CinemaHall, Movie, MovieSession, Order | ||
from cinema.paginators import OrderSetPagination | ||
from cinema.serializers import ( | ||
GenreSerializer, | ||
ActorSerializer, | ||
|
@@ -11,7 +12,9 @@ | |
MovieSessionListSerializer, | ||
MovieDetailSerializer, | ||
MovieSessionDetailSerializer, | ||
MovieListSerializer, | ||
MovieListSerializer, OrderSerializer, | ||
OrderListSerializer, | ||
|
||
) | ||
|
||
|
||
|
@@ -34,6 +37,10 @@ class MovieViewSet(viewsets.ModelViewSet): | |
queryset = Movie.objects.all() | ||
serializer_class = MovieSerializer | ||
|
||
@staticmethod | ||
def _params_to_ints(query_string): | ||
return [int(str_id) for str_id in query_string.split(",")] | ||
|
||
def get_serializer_class(self): | ||
if self.action == "list": | ||
return MovieListSerializer | ||
|
@@ -43,6 +50,31 @@ def get_serializer_class(self): | |
|
||
return MovieSerializer | ||
|
||
def get_queryset(self): | ||
queryset = self.queryset.prefetch_related("genres", "actors") | ||
|
||
if self.action == "list": | ||
|
||
genres = self.request.query_params.get("genres") | ||
actors = self.request.query_params.get("actors") | ||
title = self.request.query_params.get("title") | ||
|
||
if actors: | ||
actors = self._params_to_ints(actors) | ||
queryset = queryset.filter(actors__id__in=actors) | ||
|
||
if genres: | ||
genres = self._params_to_ints(genres) | ||
queryset = queryset.filter(genres__id__in=genres) | ||
|
||
if title: | ||
queryset = queryset.filter(title__icontains=title) | ||
|
||
elif self.action == "retrieve": | ||
queryset = queryset.prefetch_related("genres", "actors") | ||
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. you already prefetch above - no need to do it twice |
||
|
||
return queryset.distinct() | ||
|
||
|
||
class MovieSessionViewSet(viewsets.ModelViewSet): | ||
queryset = MovieSession.objects.all() | ||
|
@@ -56,3 +88,50 @@ def get_serializer_class(self): | |
return MovieSessionDetailSerializer | ||
|
||
return MovieSessionSerializer | ||
|
||
def get_queryset(self): | ||
queryset = self.queryset | ||
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. call super method instead of accesing this attribute directly |
||
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") | ||
) | ||
) | ||
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) | ||
|
||
if self.action in "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. should be |
||
queryset = queryset.select_related("movie", "cinema_hall") | ||
|
||
return queryset.distinct() | ||
|
||
|
||
class OrdersViewSet(viewsets.ModelViewSet): | ||
queryset = Order.objects.all() | ||
serializer_class = OrderSerializer | ||
pagination_class = OrderSetPagination | ||
|
||
def get_queryset(self): | ||
queryset = self.queryset.filter(user=self.request.user) | ||
if self.action == "list": | ||
queryset = queryset.prefetch_related("tickets__movie_session") | ||
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. here would be better to specifically prefetch "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): | ||
serializer = self.serializer_class | ||
if self.action == "list": | ||
serializer = OrderListSerializer | ||
|
||
return serializer |
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.
page_size = 1
I think it's better to give at least 5