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' #663

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
62 changes: 60 additions & 2 deletions cinema/serializers.py
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):
Expand Down Expand Up @@ -68,6 +77,7 @@ class Meta:
"movie_title",
"cinema_hall_name",
"cinema_hall_capacity",
"tickets_available",

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

)


Expand All @@ -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")

Choose a reason for hiding this comment

The 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):

Choose a reason for hiding this comment

The 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")

Choose a reason for hiding this comment

The 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"
]

Choose a reason for hiding this comment

The 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)
2 changes: 2 additions & 0 deletions cinema/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
CinemaHallViewSet,
MovieViewSet,
MovieSessionViewSet,
OrderViewSet,
)

router = routers.DefaultRouter()
Expand All @@ -15,6 +16,7 @@
router.register("cinema_halls", CinemaHallViewSet)
router.register("movies", MovieViewSet)
router.register("movie_sessions", MovieSessionViewSet)
router.register("orders", OrderViewSet)

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

Expand Down
32 changes: 31 additions & 1 deletion cinema/views.py
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

Choose a reason for hiding this comment

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

Spread imports on multiple lines


from cinema.serializers import (
GenreSerializer,
Expand All @@ -12,6 +13,8 @@
MovieDetailSerializer,
MovieSessionDetailSerializer,
MovieListSerializer,
OrderSerializer,
OrderListSerializer
)


Expand Down Expand Up @@ -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

Choose a reason for hiding this comment

The 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")

Choose a reason for hiding this comment

The 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
Loading