-
Notifications
You must be signed in to change notification settings - Fork 702
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" #731
base: master
Are you sure you want to change the base?
"solution" #731
Changes from all commits
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 |
---|---|---|
|
@@ -7,4 +7,5 @@ select = B,C,E,F,W,T4,B9,Q0,N8,VNE | |
exclude = | ||
**migrations | ||
venv | ||
tests | ||
tests | ||
cinema_service |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,16 @@ | ||
from django.db.models import Count, F | ||
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, | ||
Ticket, | ||
) | ||
|
||
from cinema.serializers import ( | ||
GenreSerializer, | ||
|
@@ -12,6 +22,8 @@ | |
MovieDetailSerializer, | ||
MovieSessionDetailSerializer, | ||
MovieListSerializer, | ||
OrderSerializer, | ||
TicketSerializer, | ||
) | ||
|
||
|
||
|
@@ -43,6 +55,28 @@ def get_serializer_class(self): | |
|
||
return MovieSerializer | ||
|
||
def get_queryset(self): | ||
queryset = super().get_queryset() | ||
if self.action == ("list", "retrieve"): | ||
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 condition |
||
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() | ||
|
@@ -56,3 +90,45 @@ 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 TicketViewSet(viewsets.ModelViewSet): | ||
queryset = Ticket.objects.all() | ||
serializer_class = TicketSerializer | ||
|
||
|
||
class OrderPagination(PageNumberPagination): | ||
page_size = 5 | ||
|
||
|
||
class OrderViewSet(viewsets.ModelViewSet): | ||
queryset = Order.objects.all() | ||
serializer_class = OrderSerializer | ||
pagination_class = OrderPagination |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,9 +20,7 @@ | |
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ | ||
|
||
# SECURITY WARNING: keep the secret key used in production secret! | ||
SECRET_KEY = ( | ||
"django-insecure-6vubhk2$++agnctay_4pxy_8cq)mosmn(*-#2b^v4cgsh-^!i3" | ||
) | ||
SECRET_KEY = "django-insecure-6vubhk2$++agnctay_4pxy_8cq)mosmn(*-#2b^v4cgsh-^!i3" | ||
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 |
||
|
||
# SECURITY WARNING: don't run with debug turned on in production! | ||
DEBUG = True | ||
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 |
||
|
@@ -100,16 +98,13 @@ | |
"UserAttributeSimilarityValidator", | ||
}, | ||
{ | ||
"NAME": "django.contrib.auth.password_validation." | ||
"MinimumLengthValidator", | ||
"NAME": "django.contrib.auth.password_validation." "MinimumLengthValidator", | ||
}, | ||
{ | ||
"NAME": "django.contrib.auth.password_validation." | ||
"CommonPasswordValidator", | ||
"NAME": "django.contrib.auth.password_validation." "CommonPasswordValidator", | ||
}, | ||
{ | ||
"NAME": "django.contrib.auth.password_validation." | ||
"NumericPasswordValidator", | ||
"NAME": "django.contrib.auth.password_validation." "NumericPasswordValidator", | ||
}, | ||
] | ||
|
||
|
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
tickets
field is marked asread_only=True
, which means it will not be included in thevalidated_data
during creation. Therefore, attempting topop
it in thecreate
method will raise a KeyError. Consider removing thetickets
field fromvalidated_data
handling or adjusting the logic to accommodate read-only fields.