Skip to content

Commit

Permalink
"solution"
Browse files Browse the repository at this point in the history
  • Loading branch information
Olha Stadnik committed Dec 11, 2024
1 parent 7b4fa8c commit b485b59
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
6 changes: 3 additions & 3 deletions cinema/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ class Actor(models.Model):
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)

def __str__(self):
return f"{self.first_name} {self.last_name}"

@property
def full_name(self):
return self.__str__()

def __str__(self):
return f"{self.first_name} {self.last_name}"


class Movie(models.Model):
title = models.CharField(max_length=255)
Expand Down
18 changes: 13 additions & 5 deletions cinema/serializers.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
from rest_framework import serializers
from .models import Actor, CinemaHall, Genre, Movie, MovieSession, Order, Ticket
from .models import Actor, CinemaHall, Genre, Movie, MovieSession, Order, \
Ticket


class ActorSerializer(serializers.ModelSerializer):
class Meta:
model = Actor
fields = ["id", "first_name", "last_name"]
fields = ["id", "first_name", "last_name", "full_name"]


class GenreSerializer(serializers.ModelSerializer):
class Meta:
model = Genre
fields = ["id", "name"]


class MovieSerializer(serializers.ModelSerializer):
class Meta:
model = Movie
fields = ["id", "title", "description", "duration", "genres", "actors"]


class MovieListSerializer(MovieSerializer):
genres = serializers.SlugRelatedField(
many=True, read_only=True, slug_field="name"
Expand All @@ -25,6 +29,7 @@ class MovieListSerializer(MovieSerializer):
many=True, read_only=True, slug_field="full_name"
)


class MovieDetailSerializer(MovieSerializer):
genres = GenreSerializer(many=True, read_only=True)
actors = ActorSerializer(many=True, read_only=True)
Expand All @@ -33,13 +38,15 @@ class MovieDetailSerializer(MovieSerializer):
class CinemaHallSerializer(serializers.ModelSerializer):
class Meta:
model = CinemaHall
fields = ["id", "name", "rows", "seats_in_row"]
fields = ["id", "name", "rows", "seats_in_row", "capacity"]


class MovieSessionSerializer(serializers.ModelSerializer):
class Meta:
model = MovieSession
fields = ["id", "show_time", "movie", "cinema_hall"]


class MovieSessionListSerializer(MovieSessionSerializer):
movie_title = serializers.CharField(source="movie.title", read_only=True)
cinema_hall_name = serializers.CharField(
Expand All @@ -51,9 +58,10 @@ class MovieSessionListSerializer(MovieSessionSerializer):

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


class MovieSessionDetailSerializer(MovieSessionSerializer):
movie = MovieListSerializer(read_only=True)
cinema_hall = CinemaHallSerializer(read_only=True)
cinema_hall = CinemaHallSerializer(read_only=True)
13 changes: 7 additions & 6 deletions cinema/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
from rest_framework import routers
from rest_framework.urls import app_name

from cinema.views import GenreViewSet, ActorViewSet, CinemaHallViewSet, MovieViewSet, MovieSessionViewSet
from cinema.views import GenreViewSet, ActorViewSet, CinemaHallViewSet, \
MovieViewSet, MovieSessionViewSet

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

urlpatterns = [
path("", include(router.urls)),
Expand Down
4 changes: 4 additions & 0 deletions cinema/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ class GenreViewSet(viewsets.ModelViewSet):
queryset = Genre.objects.all()
serializer_class = GenreSerializer


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


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


class MovieViewSet(viewsets.ModelViewSet):
queryset = Movie.objects.prefetch_related("actors", "genres")

Expand All @@ -28,6 +31,7 @@ def get_serializer_class(self):
return MovieDetailSerializer
return MovieSerializer


class MovieSessionViewSet(viewsets.ModelViewSet):
queryset = MovieSession.objects.select_related("movie", "cinema_hall")

Expand Down

0 comments on commit b485b59

Please sign in to comment.