From aab7f428ce32656e28357e0164ba15f902f18668 Mon Sep 17 00:00:00 2001 From: zakotii Date: Wed, 8 Jan 2025 14:27:16 +0200 Subject: [PATCH] Solution1 --- cinema/filters.py | 10 ++++++++-- cinema/serializers.py | 15 +++++++++++---- cinema_service/settings.py | 4 ---- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/cinema/filters.py b/cinema/filters.py index a0234499..26443786 100644 --- a/cinema/filters.py +++ b/cinema/filters.py @@ -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): @@ -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") @@ -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() diff --git a/cinema/serializers.py b/cinema/serializers.py index 3aeeb810..d13b413f 100644 --- a/cinema/serializers.py +++ b/cinema/serializers.py @@ -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 diff --git a/cinema_service/settings.py b/cinema_service/settings.py index 1f940231..8c7a5c46 100644 --- a/cinema_service/settings.py +++ b/cinema_service/settings.py @@ -144,7 +144,3 @@ "django_filters.rest_framework.DjangoFilterBackend", ), } - -REST_FRAMEWORK["DEFAULT_FILTER_BACKENDS"] = [ - "django_filters.rest_framework.DjangoFilterBackend" -]