Skip to content

Commit

Permalink
add .env file and reformat code by flake8 require
Browse files Browse the repository at this point in the history
  • Loading branch information
r0manvynnytskyi committed Dec 11, 2024
1 parent 784b2e3 commit 36f5476
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 22 deletions.
24 changes: 17 additions & 7 deletions cinema/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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)
Expand All @@ -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"),
]:
Expand Down Expand Up @@ -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")
Expand Down
49 changes: 37 additions & 12 deletions cinema/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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):
Expand All @@ -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
)
Expand All @@ -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:
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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)
4 changes: 3 additions & 1 deletion cinema/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
)
)
Expand Down
4 changes: 2 additions & 2 deletions cinema_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand All @@ -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
Expand Down

0 comments on commit 36f5476

Please sign in to comment.