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

Develop #745

Open
wants to merge 6 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
65 changes: 63 additions & 2 deletions 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.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):
Expand Down Expand Up @@ -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)

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 the MovieSession model to provide this data.

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 there is no source or method provided to compute its value. Ensure that this field is properly calculated, possibly by adding a method in the serializer to compute the available tickets based on the cinema hall capacity and the number of tickets already sold.


class Meta:
model = MovieSession
Expand All @@ -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

Choose a reason for hiding this comment

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

Ensure that the ticket creation logic within the create method of OrderCreateSerializer is correctly handling the transaction. The transaction.atomic decorator is used, which is good, but make sure that any potential exceptions during ticket creation are handled to maintain data integrity.

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, basename="order")

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

Expand Down
80 changes: 78 additions & 2 deletions cinema/views.py
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,
Expand All @@ -12,6 +21,7 @@
MovieDetailSerializer,
MovieSessionDetailSerializer,
MovieListSerializer,
OrderListSerializer, OrderCreateSerializer,
)


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

Choose a reason for hiding this comment

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

The current implementation of get_queryset in MovieViewSet uses elif statements, which means only one filter can be applied at a time. Consider using if statements instead to allow filtering by multiple parameters simultaneously, such as actors, genres, and title.

if self.action == ("list", "retrieve"):

Choose a reason for hiding this comment

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

The condition if self.action == ("list", "retrieve"): is incorrect. You should check for each action separately, like if self.action in ["list", "retrieve"]:.

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

Choose a reason for hiding this comment

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

The pagination settings are very restrictive with a page_size and max_page_size of 1. Ensure this is intentional, as it might not be suitable for all scenarios.

Comment on lines +109 to +111

Choose a reason for hiding this comment

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

The pagination settings in OrderSetPagination are very restrictive, with both page_size and max_page_size set to 1. Consider increasing these values to provide a more user-friendly experience, allowing users to view more items per page.



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
2 changes: 1 addition & 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 Down
Binary file added db.sqlite3
Binary file not shown.
Loading