-
Notifications
You must be signed in to change notification settings - Fork 636
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' #663
base: master
Are you sure you want to change the base?
'Solution' #663
Changes from 2 commits
daabca8
aaa0d26
3e34d39
f046d6f
42156cf
84236e9
a36eded
562cd2e
1fa809c
4b5d8b7
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,15 @@ | ||
from django.db import transaction | ||
from rest_framework import serializers | ||
|
||
from cinema.models import Genre, Actor, CinemaHall, Movie, MovieSession | ||
from cinema.models import ( | ||
Genre, | ||
Actor, | ||
CinemaHall, | ||
Movie, | ||
MovieSession, | ||
Order, | ||
Ticket | ||
) | ||
|
||
|
||
class GenreSerializer(serializers.ModelSerializer): | ||
|
@@ -68,6 +77,7 @@ class Meta: | |
"movie_title", | ||
"cinema_hall_name", | ||
"cinema_hall_capacity", | ||
"tickets_available", | ||
) | ||
|
||
|
||
|
@@ -77,4 +87,52 @@ class MovieSessionDetailSerializer(MovieSessionSerializer): | |
|
||
class Meta: | ||
model = MovieSession | ||
fields = ("id", "show_time", "movie", "cinema_hall") | ||
fields = ("id", "show_time", "movie", "cinema_hall", "taken_places") | ||
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 added field taken_places, but model doesn't have this fields. You need to got it from tickets |
||
|
||
|
||
class TicketSerializer(serializers.ModelSerializer): | ||
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 inherit this from TicketPerformSerializer so you don't have to declare the same Meta class twice |
||
class Meta: | ||
model = Ticket | ||
fields = ("id", "row", "seat", "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. we don't need id here |
||
|
||
|
||
class TicketRetrieveSerializer(serializers.ModelSerializer): | ||
movie_session = MovieSessionListSerializer(many=False) | ||
|
||
class Meta: | ||
model = Ticket | ||
fields = [ | ||
"id", | ||
"order", | ||
"row", | ||
"seat", | ||
"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. We don't need order field in this serializer |
||
|
||
|
||
class OrderSerializer(serializers.ModelSerializer): | ||
tickets = TicketRetrieveSerializer( | ||
many=True, | ||
read_only=False, | ||
allow_empty=False | ||
) | ||
|
||
class Meta: | ||
model = Order | ||
fields = [ | ||
"id", | ||
"created_at", | ||
"tickets" | ||
] | ||
|
||
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 | ||
|
||
|
||
class OrderListSerializer(OrderSerializer): | ||
tickets = TicketRetrieveSerializer(many=True, read_only=True) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
from rest_framework import viewsets | ||
from rest_framework.pagination import PageNumberPagination | ||
|
||
from cinema.models import Genre, Actor, CinemaHall, Movie, MovieSession | ||
from cinema.models import Genre, Actor, CinemaHall, Movie, MovieSession, 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. Spread imports on multiple lines |
||
|
||
from cinema.serializers import ( | ||
GenreSerializer, | ||
|
@@ -12,6 +13,8 @@ | |
MovieDetailSerializer, | ||
MovieSessionDetailSerializer, | ||
MovieListSerializer, | ||
OrderSerializer, | ||
OrderListSerializer | ||
) | ||
|
||
|
||
|
@@ -56,3 +59,30 @@ def get_serializer_class(self): | |
return MovieSessionDetailSerializer | ||
|
||
return MovieSessionSerializer | ||
|
||
|
||
class OrderSetPagination(PageNumberPagination): | ||
page_size = 1 | ||
page_size_query_param = "page_size" | ||
max_page_size = 3 | ||
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. it would be better to create separated file for paginator. Do not mix ViewSets & Pagination in one class |
||
|
||
|
||
class OrderViewSet(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. It would be better to prefetch_related("tickets__movie_session__movie", "tickets__movie_session__cinema_hall") instead of tickets__movie_session. Because there are indepent models which have relations with model MovieSession |
||
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 |
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.
You need annotate method to your MovieSessionViewSet, to make this field available