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 #748

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
7 changes: 7 additions & 0 deletions cinema/pagination.py
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
56 changes: 55 additions & 1 deletion cinema/serializers.py
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.relations import PrimaryKeyRelatedField

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 @@ -78,3 +88,47 @@ class MovieSessionDetailSerializer(MovieSessionSerializer):
class Meta:
model = MovieSession
fields = ("id", "show_time", "movie", "cinema_hall")


class TicketSerializer(serializers.ModelSerializer):
movie_session = PrimaryKeyRelatedField(
queryset=MovieSession.objects.all()
)

class Meta:
model = Ticket
fields = ("id", "row", "seat", "movie_session")


class TicketDetailSerializer(TicketSerializer):
movie_session = MovieSessionListSerializer(many=False, read_only=True)

def validate(self, data):
data = super(TicketSerializer, self).validate(data)
ticket = Ticket(
movie_session=data.get("movie_session"),
row=data.get("row"),
seat=data.get("seat"),
)
ticket.full_clean()
return data


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


class OrderDetailSerializer(OrderSerializer):
tickets = TicketDetailSerializer(many=True, read_only=True)
8 changes: 6 additions & 2 deletions cinema/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@
CinemaHallViewSet,
MovieViewSet,
MovieSessionViewSet,
OrderViewSet,
TicketViewSet,
)

app_name = "cinema"

router = routers.DefaultRouter()
router.register("genres", GenreViewSet)
router.register("actors", ActorViewSet)
router.register("cinema_halls", CinemaHallViewSet)
router.register("movies", MovieViewSet)
router.register("movie_sessions", MovieSessionViewSet)
router.register("orders", OrderViewSet, basename="orders")
router.register("tickets", TicketViewSet)

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

app_name = "cinema"
88 changes: 87 additions & 1 deletion cinema/views.py
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,
Expand All @@ -12,6 +22,9 @@
MovieDetailSerializer,
MovieSessionDetailSerializer,
MovieListSerializer,
OrderSerializer,
TicketSerializer,
OrderDetailSerializer,
)


Expand Down Expand Up @@ -43,6 +56,28 @@ def get_serializer_class(self):

return MovieSerializer

def get_queryset(self):
queryset = super().get_queryset()
if self.action in ["list", "retrieve"]:
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()
Expand All @@ -56,3 +91,54 @@ 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()

Choose a reason for hiding this comment

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

The TicketViewSet is missing a serializer_class attribute. You should define it to specify which serializer to use for this viewset.

serializer_class = TicketSerializer
8 changes: 7 additions & 1 deletion cinema_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@

USE_I18N = True

USE_TZ = False
USE_TZ = True


# Static files (CSS, JavaScript, Images)
Expand All @@ -136,3 +136,9 @@
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

REST_FRAMEWORK = {
"DEFAULT_PAGINATION_CLASS":
"rest_framework.pagination.LimitOffsetPagination",
"PAGE_SIZE": 10,
}
Binary file added db.sqlite3
Binary file not shown.
Loading