From 16c02ce601214b6009a7c30cfe06206cfd3d03c9 Mon Sep 17 00:00:00 2001 From: Olha Stadnik Date: Tue, 10 Dec 2024 21:40:57 +0200 Subject: [PATCH 1/5] "created serializers, views and urls" --- cinema/serializers.py | 30 +++++++++++++++++++++++++++++- cinema/urls.py | 9 ++++++++- cinema/views.py | 24 +++++++++++++++++++++++- 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/cinema/serializers.py b/cinema/serializers.py index 612ca7e2..1931f0ed 100644 --- a/cinema/serializers.py +++ b/cinema/serializers.py @@ -1 +1,29 @@ -# write serializers here +from rest_framework import serializers +from .models import Actor, CinemaHall, Genre, Movie, MovieSession, Order, Ticket + +class ActorSerializer(serializers.ModelSerializer): + class Meta: + model = Actor + fields = ["id", "first_name", "last_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 CinemaHallSerializer(serializers.ModelSerializer): + class Meta: + model = CinemaHall + fields = ["id", "name", "rows", "seats_in_row"] + +class MovieSessionSerializer(serializers.ModelSerializer): + class Meta: + model = MovieSession + fields = ["id", "show_time", "movie", "cinema_hall"] + diff --git a/cinema/urls.py b/cinema/urls.py index 420f8e8c..9252cce4 100644 --- a/cinema/urls.py +++ b/cinema/urls.py @@ -1 +1,8 @@ -# write urls here +from django.contrib import admin +from cinema.models import CinemaHall, Genre, Actor, Movie, MovieSession + +admin.site.register(CinemaHall) +admin.site.register(Genre) +admin.site.register(Actor) +admin.site.register(Movie) +admin.site.register(MovieSession) diff --git a/cinema/views.py b/cinema/views.py index ae87bfde..facaa855 100644 --- a/cinema/views.py +++ b/cinema/views.py @@ -1 +1,23 @@ -# write views here +from rest_framework import viewsets +from cinema.models import Genre, Actor, CinemaHall, Movie, MovieSession +from cinema.serializers import GenreSerializer, ActorSerializer, CinemaHallSerializer, MovieSerializer, MovieSessionSerializer + +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.all() + serializer_class = MovieSerializer + +class MovieSessionViewSet(viewsets.ModelViewSet): + queryset = MovieSession.objects.all() + serializer_class = MovieSessionSerializer From add1b6fa7f741ecd174592490c9ec3b470828e91 Mon Sep 17 00:00:00 2001 From: Olha Stadnik Date: Tue, 10 Dec 2024 22:07:11 +0200 Subject: [PATCH 2/5] "update the urls" --- cinema/urls.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/cinema/urls.py b/cinema/urls.py index 9252cce4..57d884f5 100644 --- a/cinema/urls.py +++ b/cinema/urls.py @@ -1,8 +1,16 @@ -from django.contrib import admin -from cinema.models import CinemaHall, Genre, Actor, Movie, MovieSession +from django.urls import path, include +from rest_framework import routers +from rest_framework.urls import app_name -admin.site.register(CinemaHall) -admin.site.register(Genre) -admin.site.register(Actor) -admin.site.register(Movie) -admin.site.register(MovieSession) +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) + +urlpatterns = [ + path("", include(router.urls)), +] From 7b4fa8c2a25f1df1acee51077697d5b7ccd02be3 Mon Sep 17 00:00:00 2001 From: Olha Stadnik Date: Wed, 11 Dec 2024 18:17:48 +0200 Subject: [PATCH 3/5] "before test" --- cinema/models.py | 6 +++++- cinema/serializers.py | 30 ++++++++++++++++++++++++++++++ cinema/views.py | 26 +++++++++++++++++++++----- cinema_service/urls.py | 3 ++- 4 files changed, 58 insertions(+), 7 deletions(-) diff --git a/cinema/models.py b/cinema/models.py index c42d2a3d..815791ad 100644 --- a/cinema/models.py +++ b/cinema/models.py @@ -28,7 +28,11 @@ class Actor(models.Model): last_name = models.CharField(max_length=255) def __str__(self): - return self.first_name + " " + self.last_name + return f"{self.first_name} {self.last_name}" + + @property + def full_name(self): + return self.__str__() class Movie(models.Model): diff --git a/cinema/serializers.py b/cinema/serializers.py index 1931f0ed..c78cc19e 100644 --- a/cinema/serializers.py +++ b/cinema/serializers.py @@ -17,6 +17,19 @@ 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" + ) + actors = serializers.SlugRelatedField( + 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) + + class CinemaHallSerializer(serializers.ModelSerializer): class Meta: model = CinemaHall @@ -27,3 +40,20 @@ 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( + source="cinema_hall.name", read_only=True + ) + cinema_hall_capacity = serializers.IntegerField( + source="cinema_hall.capacity", read_only=True + ) + + class Meta: + model = MovieSession + exclude = ["movie", "cinema_hall"] + + +class MovieSessionDetailSerializer(MovieSessionSerializer): + movie = MovieListSerializer(read_only=True) + cinema_hall = CinemaHallSerializer(read_only=True) \ No newline at end of file diff --git a/cinema/views.py b/cinema/views.py index facaa855..8168d045 100644 --- a/cinema/views.py +++ b/cinema/views.py @@ -1,6 +1,10 @@ from rest_framework import viewsets from cinema.models import Genre, Actor, CinemaHall, Movie, MovieSession -from cinema.serializers import GenreSerializer, ActorSerializer, CinemaHallSerializer, MovieSerializer, MovieSessionSerializer +from cinema.serializers import GenreSerializer, ActorSerializer, \ + CinemaHallSerializer, MovieSerializer, MovieSessionSerializer, \ + MovieListSerializer, MovieDetailSerializer, MovieSessionListSerializer, \ + MovieSessionDetailSerializer + class GenreViewSet(viewsets.ModelViewSet): queryset = Genre.objects.all() @@ -15,9 +19,21 @@ class CinemaHallViewSet(viewsets.ModelViewSet): serializer_class = CinemaHallSerializer class MovieViewSet(viewsets.ModelViewSet): - queryset = Movie.objects.all() - serializer_class = MovieSerializer + queryset = Movie.objects.prefetch_related("actors", "genres") + + def get_serializer_class(self): + if self.action == "list": + return MovieListSerializer + elif self.action == "retrieve": + return MovieDetailSerializer + return MovieSerializer class MovieSessionViewSet(viewsets.ModelViewSet): - queryset = MovieSession.objects.all() - serializer_class = MovieSessionSerializer + queryset = MovieSession.objects.select_related("movie", "cinema_hall") + + def get_serializer_class(self): + if self.action == "list": + return MovieSessionListSerializer + elif self.action == "retrieve": + return MovieSessionDetailSerializer + return MovieSessionSerializer diff --git a/cinema_service/urls.py b/cinema_service/urls.py index 083932c6..fda5a995 100644 --- a/cinema_service/urls.py +++ b/cinema_service/urls.py @@ -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")), ] From b485b594e7013c126dc7d69cccbc40c0ffc427df Mon Sep 17 00:00:00 2001 From: Olha Stadnik Date: Wed, 11 Dec 2024 18:59:24 +0200 Subject: [PATCH 4/5] "solution" --- cinema/models.py | 6 +++--- cinema/serializers.py | 18 +++++++++++++----- cinema/urls.py | 13 +++++++------ cinema/views.py | 4 ++++ 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/cinema/models.py b/cinema/models.py index 815791ad..724a6e8c 100644 --- a/cinema/models.py +++ b/cinema/models.py @@ -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) diff --git a/cinema/serializers.py b/cinema/serializers.py index c78cc19e..769837a4 100644 --- a/cinema/serializers.py +++ b/cinema/serializers.py @@ -1,10 +1,12 @@ 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): @@ -12,11 +14,13 @@ 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" @@ -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) @@ -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( @@ -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) \ No newline at end of file + cinema_hall = CinemaHallSerializer(read_only=True) diff --git a/cinema/urls.py b/cinema/urls.py index 57d884f5..2a6ac34c 100644 --- a/cinema/urls.py +++ b/cinema/urls.py @@ -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)), diff --git a/cinema/views.py b/cinema/views.py index 8168d045..0fefaa69 100644 --- a/cinema/views.py +++ b/cinema/views.py @@ -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") @@ -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") From 58d08537829b70d5e5a72a53dd3f8862f26468c4 Mon Sep 17 00:00:00 2001 From: Olha Stadnik Date: Sat, 14 Dec 2024 12:31:44 +0200 Subject: [PATCH 5/5] "solution" --- cinema/urls.py | 1 - 1 file changed, 1 deletion(-) diff --git a/cinema/urls.py b/cinema/urls.py index 2a6ac34c..675aa451 100644 --- a/cinema/urls.py +++ b/cinema/urls.py @@ -1,6 +1,5 @@ from django.urls import path, include from rest_framework import routers -from rest_framework.urls import app_name from cinema.views import GenreViewSet, ActorViewSet, CinemaHallViewSet, \ MovieViewSet, MovieSessionViewSet