Skip to content

Commit

Permalink
Solution1
Browse files Browse the repository at this point in the history
  • Loading branch information
zakotii committed Jan 8, 2025
1 parent f91b4d9 commit aab7f42
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
10 changes: 8 additions & 2 deletions cinema/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.db.models import OuterRef, Subquery
from django_filters import rest_framework as filters
from cinema.models import MovieSession, Movie, Actor
import logging


class MovieSessionFilter(filters.FilterSet):
Expand All @@ -13,6 +14,9 @@ class Meta:
fields = ["date", "movie"]


logger = logging.getLogger(__name__)


class MovieFilter(filters.FilterSet):
genres = filters.CharFilter(method="filter_by_genre_ids")
title = filters.CharFilter(field_name="title", lookup_expr="icontains")
Expand All @@ -39,6 +43,8 @@ def filter_by_actor_ids(self, queryset, name, value):
if id.strip().isdigit()
]
return queryset.filter(actors__id__in=actor_ids)
except ValueError:
# Если передан некорректный ID
except ValueError as e:
# Логируем ошибку для отладки
logger.warning(f"Invalid actor ID input: '{value}'. Error: {e}")
# Возвращаем пустой queryset, если данные некорректны
return queryset.none()
15 changes: 11 additions & 4 deletions cinema/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,23 @@ class Meta:
fields = ("id", "row", "seat", "movie_session")

def validate(self, data):
movie_session = data["movie_session"]
row = data["row"]
seat = data["seat"]
movie_session = data.get("movie_session")
if not movie_session:
raise serializers.ValidationError(
"Необходимо указать сеанс фильма."
)
row = data.get("row")
seat = data.get("seat")
# Проверка на занятость места
if Ticket.objects.filter(
movie_session=movie_session, row=row, seat=seat
).exists():
raise serializers.ValidationError("Это место уже занято.")
# Проверка допустимости номера ряда
if not (1 <= row <= movie_session.cinema_hall.rows):
raise serializers.ValidationError("Недопустимый номер ряда.")
if not (1 <= seat <= movie_session.cinema_hалл.seats_in_row):
# Проверка допустимости номера места
if not (1 <= seat <= movie_session.cinema_hall.seats_in_row):
raise serializers.ValidationError("Недопустимый номер места.")
return data

Expand Down
4 changes: 0 additions & 4 deletions cinema_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,3 @@
"django_filters.rest_framework.DjangoFilterBackend",
),
}

REST_FRAMEWORK["DEFAULT_FILTER_BACKENDS"] = [
"django_filters.rest_framework.DjangoFilterBackend"
]

0 comments on commit aab7f42

Please sign in to comment.