Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
VladimirDolhyi committed Dec 4, 2024
1 parent 1f225be commit 5be6bbd
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ select = B,C,E,F,W,T4,B9,Q0,N8,VNE
exclude =
**migrations
venv
tests
tests
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
venv/
.pytest_cache/
**__pycache__/
db.sqlite3
4 changes: 4 additions & 0 deletions cinema/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class Actor(models.Model):
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)

@property
def full_name(self) -> str:
return f"{self.first_name} {self.last_name}"

def __str__(self):
return self.first_name + " " + self.last_name

Expand Down
72 changes: 71 additions & 1 deletion cinema/serializers.py
Original file line number Diff line number Diff line change
@@ -1 +1,71 @@
# write serializers here
from rest_framework import serializers

from cinema.models import CinemaHall, Genre, Actor, Movie, MovieSession


class CinemaHallSerializer(serializers.ModelSerializer):

class Meta:
model = CinemaHall
fields = ["id", "name", "rows", "seats_in_row", "capacity"]


class GenreSerializer(serializers.ModelSerializer):

class Meta:
model = Genre
fields = "__all__"


class ActorSerializer(serializers.ModelSerializer):

class Meta:
model = Actor
fields = ["id", "first_name", "last_name", "full_name"]


class MovieSerializer(serializers.ModelSerializer):

class Meta:
model = Movie
fields = "__all__"


class MovieListSerializer(MovieSerializer):
genres = serializers.SlugRelatedField(
many=True,
read_only=True,
slug_field="name",
)
actors = serializers.SlugRelatedField(
many=True,
read_only=True,
slug_field="full_name",
)


class MovieRetrieveSerializer(MovieSerializer):
genres = GenreSerializer(many=True, read_only=True)
actors = ActorSerializer(many=True, read_only=True)


class MovieSessionSerializer(serializers.ModelSerializer):

class Meta:
model = MovieSession
fields = "__all__"


class MovieSessionListSerializer(serializers.ModelSerializer):
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)

class Meta:
model = MovieSession
fields = ["id", "show_time", "movie_title", "cinema_hall_name", "cinema_hall_capacity"]


class MovieSessionRetrieveSerializer(MovieSessionSerializer):
movie = MovieListSerializer()
cinema_hall = CinemaHallSerializer()
22 changes: 21 additions & 1 deletion cinema/urls.py
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
# write urls here
from django.urls import include, path
from rest_framework import routers

from cinema.views import (
CinemaHallViewSet,
GenreViewSet,
MovieViewSet,
ActorViewSet,
MovieSessionViewSet
)

app_name = "cinema"

router = routers.DefaultRouter()
router.register("cinema_halls", CinemaHallViewSet)
router.register("genres", GenreViewSet)
router.register("actors", ActorViewSet)
router.register("movies", MovieViewSet)
router.register("movie_sessions", MovieSessionViewSet)

urlpatterns = [path("", include(router.urls))]
64 changes: 63 additions & 1 deletion cinema/views.py
Original file line number Diff line number Diff line change
@@ -1 +1,63 @@
# write views here
from rest_framework import viewsets

from cinema.models import Movie, CinemaHall, Genre, Actor, MovieSession
from cinema.serializers import (
MovieSerializer,
CinemaHallSerializer,
GenreSerializer,
ActorSerializer,
MovieSessionSerializer,
MovieSessionListSerializer,
MovieListSerializer,
MovieRetrieveSerializer,
MovieSessionRetrieveSerializer
)


class CinemaHallViewSet(viewsets.ModelViewSet):
queryset = CinemaHall.objects.all()
serializer_class = CinemaHallSerializer


class GenreViewSet(viewsets.ModelViewSet):
queryset = Genre.objects.all()
serializer_class = GenreSerializer


class ActorViewSet(viewsets.ModelViewSet):
queryset = Actor.objects.all()
serializer_class = ActorSerializer


class MovieViewSet(viewsets.ModelViewSet):
queryset = Movie.objects.all()

def get_serializer_class(self):
if self.action == "list":
return MovieListSerializer
elif self.action == "retrieve":
return MovieRetrieveSerializer
return MovieSerializer

def get_queryset(self):
queryset = self.queryset
if self.action in ("list", "retrieve"):
return queryset.prefetch_related("genres", "actors")
return queryset


class MovieSessionViewSet(viewsets.ModelViewSet):
queryset = MovieSession.objects.all()

def get_serializer_class(self):
if self.action == "list":
return MovieSessionListSerializer
elif self.action == "retrieve":
return MovieSessionRetrieveSerializer
return MovieSessionSerializer

def get_queryset(self):
queryset = self.queryset
if self.action in ("list", "retrieve"):
return queryset.select_related("movie", "cinema_hall")
return queryset
3 changes: 2 additions & 1 deletion cinema_service/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib import admin
from django.urls import path
from django.urls import path, include

urlpatterns = [
path("admin/", admin.site.urls),
path("api/cinema/", include("cinema.urls", namespace="cinema")),
]
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ djangorestframework==3.13.1
flake8==5.0.4
flake8-quotes==3.3.1
flake8-variables-names==0.0.5
pep8-naming==0.13.2
pep8-naming==0.13.2

0 comments on commit 5be6bbd

Please sign in to comment.