diff --git a/cinema/models.py b/cinema/models.py index 3ec1232a..0d2f7096 100644 --- a/cinema/models.py +++ b/cinema/models.py @@ -52,7 +52,8 @@ def __str__(self): class MovieSession(models.Model): show_time = models.DateTimeField() movie = models.ForeignKey(Movie, on_delete=models.CASCADE) - cinema_hall = models.ForeignKey(CinemaHall, on_delete=models.CASCADE) + cinema_hall = models.ForeignKey(CinemaHall, + on_delete=models.CASCADE) class Meta: ordering = ["-show_time"] @@ -63,7 +64,8 @@ def __str__(self): class Order(models.Model): created_at = models.DateTimeField(auto_now_add=True) - user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) + user = models.ForeignKey(settings.AUTH_USER_MODEL, + on_delete=models.CASCADE) def __str__(self): return str(self.created_at) @@ -74,15 +76,22 @@ class Meta: class Ticket(models.Model): movie_session = models.ForeignKey( - MovieSession, on_delete=models.CASCADE, related_name="tickets" + MovieSession, on_delete=models.CASCADE, + related_name="tickets" ) - order = models.ForeignKey(Order, on_delete=models.CASCADE, related_name="tickets") + order = models.ForeignKey(Order, on_delete=models.CASCADE, + related_name="tickets") row = models.IntegerField() seat = models.IntegerField() @staticmethod - def validate_ticket(row, seat, cinema_hall, error_to_raise): - for ticket_attr_value, ticket_attr_name, cinema_hall_attr_name in [ + def validate_ticket(row, + seat, + cinema_hall, + error_to_raise): + for (ticket_attr_value, + ticket_attr_name, + cinema_hall_attr_name) in [ (row, "row", "rows"), (seat, "seat", "seats_in_row"), ]: @@ -118,7 +127,8 @@ def save( ) def __str__(self): - return f"{str(self.movie_session)} " f"(row: {self.row}, seat: {self.seat})" + return f"{str(self.movie_session)} " \ + f"(row: {self.row}, seat: {self.seat})" class Meta: unique_together = ("movie_session", "row", "seat") diff --git a/cinema/serializers.py b/cinema/serializers.py index 1de1c78f..4565410e 100644 --- a/cinema/serializers.py +++ b/cinema/serializers.py @@ -37,7 +37,10 @@ class Meta: class MovieListSerializer(MovieSerializer): - genres = serializers.SlugRelatedField(many=True, read_only=True, slug_field="name") + genres = (serializers.SlugRelatedField + (many=True, + read_only=True, + slug_field="name")) actors = serializers.SlugRelatedField( many=True, read_only=True, slug_field="full_name" ) @@ -49,7 +52,12 @@ class MovieDetailSerializer(MovieSerializer): class Meta: model = Movie - fields = ("id", "title", "description", "duration", "genres", "actors") + fields = ("id", + "title", + "description", + "duration", + "genres", + "actors") class MovieSessionSerializer(serializers.ModelSerializer): @@ -59,8 +67,11 @@ class Meta: class MovieSessionListSerializer(MovieSessionSerializer): - movie_title = serializers.CharField(source="movie.title", read_only=True) - cinema_hall_name = serializers.CharField(source="cinema_hall.name", read_only=True) + movie_title = serializers.CharField(source="movie.title", + read_only=True) + cinema_hall_name = serializers.CharField( + source="cinema_hall.name", + read_only=True) cinema_hall_capacity = serializers.IntegerField( source="cinema_hall.capacity", read_only=True ) @@ -81,7 +92,9 @@ class Meta: class TicketSerializer(serializers.ModelSerializer): def validate(self, attrs): data = super(TicketSerializer, self).validate(attrs=attrs) - Ticket.validate_ticket(attrs["row"], attrs["seat"], attrs["movie_session"]) + Ticket.validate_ticket(attrs["row"], + attrs["seat"], + attrs["movie_session"]) return data class Meta: @@ -90,7 +103,8 @@ class Meta: class TicketListSerializer(TicketSerializer): - movie_session = MovieSessionListSerializer(many=False, read_only=True) + movie_session = MovieSessionListSerializer(many=False, + read_only=True) class TicketSeatsSerializer(TicketSerializer): @@ -100,17 +114,27 @@ class Meta: class MovieSessionDetailSerializer(MovieSessionSerializer): - movie = MovieListSerializer(many=False, read_only=True) - cinema_hall = CinemaHallSerializer(many=False, read_only=True) - taken_places = TicketSeatsSerializer(source="tickets", many=True, read_only=True) + movie = MovieListSerializer(many=False, + read_only=True) + cinema_hall = CinemaHallSerializer(many=False, + read_only=True) + taken_places = TicketSeatsSerializer(source="tickets", + many=True, + read_only=True) class Meta: model = MovieSession - fields = ("id", "show_time", "movie", "cinema_hall", "taken_places") + fields = ("id", + "show_time", + "movie", + "cinema_hall", + "taken_places") class OrderSerializer(serializers.ModelSerializer): - tickets = TicketSerializer(many=True, read_only=False, allow_empty=False) + tickets = TicketSerializer(many=True, + read_only=False, + allow_empty=False) class Meta: model = Order @@ -126,4 +150,5 @@ def create(self, validated_data): class OrderListSerializer(OrderSerializer): - tickets = TicketListSerializer(many=True, read_only=True) + tickets = TicketListSerializer(many=True, + read_only=True) diff --git a/cinema/views.py b/cinema/views.py index 6ae66d03..dfed5a59 100644 --- a/cinema/views.py +++ b/cinema/views.py @@ -4,6 +4,7 @@ from rest_framework import viewsets, mixins from rest_framework.authentication import TokenAuthentication from rest_framework.pagination import PageNumberPagination +from rest_framework.permissions import IsAuthenticated from cinema.models import Genre, Actor, CinemaHall, Movie, MovieSession, Order from cinema.permissions import IsAdminOrIfAuthenticatedReadOnly @@ -92,7 +93,8 @@ class MovieSessionViewSet(viewsets.ModelViewSet): MovieSession.objects.all() .select_related("movie", "cinema_hall") .annotate( - tickets_available=F("cinema_hall__rows") * F("cinema_hall__seats_in_row") + tickets_available=F("cinema_hall__rows") + * F("cinema_hall__seats_in_row") - Count("tickets") ) ) diff --git a/cinema_service/settings.py b/cinema_service/settings.py index 8852ca0d..ff508ac3 100644 --- a/cinema_service/settings.py +++ b/cinema_service/settings.py @@ -9,7 +9,7 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/4.0/ref/settings/ """ - +import os from pathlib import Path # Build paths inside the project like this: BASE_DIR / 'subdir'. @@ -20,7 +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 = os.getenv("DJANGO_SECRET_KEY") # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True