-
Notifications
You must be signed in to change notification settings - Fork 707
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 #734
Open
Bondzik-S
wants to merge
5
commits into
mate-academy:master
Choose a base branch
from
Bondzik-S:develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Solution #734
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,4 @@ venv/ | |
.pytest_cache/ | ||
**__pycache__/ | ||
*.pyc | ||
app/db.sqlite3 | ||
db.sqlite3 |
23 changes: 23 additions & 0 deletions
23
cinema/migrations/0005_alter_ticket_unique_together_ticket_unique_ticket.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Generated by Django 4.1 on 2024-12-11 23:32 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("cinema", "0004_alter_genre_name"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterUniqueTogether( | ||
name="ticket", | ||
unique_together=set(), | ||
), | ||
migrations.AddConstraint( | ||
model_name="ticket", | ||
constraint=models.UniqueConstraint( | ||
fields=("row", "seat", "movie_session"), name="unique_ticket" | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
from django.core.serializers import serialize | ||
from django.db.models import F, Count | ||
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 | ||
|
||
from cinema.serializers import ( | ||
GenreSerializer, | ||
|
@@ -12,6 +15,8 @@ | |
MovieDetailSerializer, | ||
MovieSessionDetailSerializer, | ||
MovieListSerializer, | ||
OrderSerializer, | ||
OrderListSerializer, | ||
) | ||
|
||
|
||
|
@@ -34,6 +39,28 @@ class MovieViewSet(viewsets.ModelViewSet): | |
queryset = Movie.objects.all() | ||
serializer_class = 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() | ||
|
||
def get_serializer_class(self): | ||
if self.action == "list": | ||
return MovieListSerializer | ||
|
@@ -56,3 +83,66 @@ def get_serializer_class(self): | |
return MovieSessionDetailSerializer | ||
|
||
return MovieSessionSerializer | ||
|
||
def get_queryset(self): | ||
queryset = self.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 date: | ||
queryset = queryset.filter(show_time__date=date) | ||
if movie: | ||
queryset = queryset.filter(movie__id=movie) | ||
if self.action in ("list", "retrieve"): | ||
queryset = queryset.select_related("movie", "cinema_hall") | ||
Comment on lines
+109
to
+110
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. The |
||
|
||
return queryset.distinct() | ||
|
||
|
||
class OrderSetPagination(PageNumberPagination): | ||
page_size = 1 | ||
page_size_query_param = "page_size" | ||
max_page_size = 20 | ||
|
||
|
||
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__cinema_hall", | ||
"tickets__movie_session__movie" | ||
) | ||
elif self.action == "retrieve": | ||
queryset = queryset.prefetch_related("tickets__movie_session") | ||
|
||
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The
read_only_fields
attribute is redundant here because all fields inTicketInfoSeatsSerializer
are already marked as read-only by default. You can remove this line to clean up the code.