From f63dcea2900a32ef81758cef12f02959e40d7cb9 Mon Sep 17 00:00:00 2001 From: rivrxsq Date: Sun, 12 Jan 2025 01:13:29 +0200 Subject: [PATCH 1/7] add solution for task --- cinema/models.py | 25 +++++++++ cinema/serializers.py | 19 ++++++- cinema/urls.py | 12 +++-- cinema/views.py | 118 +++++++++++++++++++++++++++++++----------- 4 files changed, 139 insertions(+), 35 deletions(-) diff --git a/cinema/models.py b/cinema/models.py index cc477513f..ce05da6f6 100644 --- a/cinema/models.py +++ b/cinema/models.py @@ -5,6 +5,31 @@ class Movie(models.Model): title = models.CharField(max_length=255) description = models.TextField() duration = models.IntegerField() + actors = models.ManyToManyField('Actor', related_name='movies') + genres = models.ManyToManyField('Genre', related_name='movies') def __str__(self): return self.title + + +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}" + +class Genre(models.Model): + name = models.CharField(max_length=255, unique=True) + + def __str__(self): + return self.name + + +class CinemaHall(models.Model): + name = models.CharField(max_length=255) + rows = models.IntegerField() + seats_in_row = models.IntegerField() + + def __str__(self): + return self.name diff --git a/cinema/serializers.py b/cinema/serializers.py index 050db5771..ed1d4671b 100644 --- a/cinema/serializers.py +++ b/cinema/serializers.py @@ -1,6 +1,6 @@ from rest_framework import serializers -from cinema.models import Movie +from cinema.models import Movie, Genre, Actor, CinemaHall class MovieSerializer(serializers.Serializer): @@ -22,3 +22,20 @@ def update(self, instance, validated_data): instance.save() return instance + +class GenreSerializer(serializers.ModelSerializer): + class Meta: + model = Genre + fields = ["id", "name"] + + +class ActorSerializer(serializers.ModelSerializer): + class Meta: + model = Actor + fields = ["id", "first_name", "last_name"] + + +class CinemaHallSerializer(serializers.ModelSerializer): + class Meta: + model = CinemaHall + fields = ["id", "name", "rows", "seats_in_row"] diff --git a/cinema/urls.py b/cinema/urls.py index 1ae7d5cb0..809bc087c 100644 --- a/cinema/urls.py +++ b/cinema/urls.py @@ -1,10 +1,14 @@ -from django.urls import path +from django.urls import path, include +from rest_framework import routers -from cinema.views import movie_list, movie_detail +from cinema.views import MovieViewSet + +router = routers.DefaultRouter() +router.register("buses", MovieViewSet) urlpatterns = [ - path("movies/", movie_list, name="movie-list"), - path("movies//", movie_detail, name="movie-detail"), + path("", include(router.urls)) ] + app_name = "cinema" diff --git a/cinema/views.py b/cinema/views.py index 78ba8a79c..a75d1223a 100644 --- a/cinema/views.py +++ b/cinema/views.py @@ -1,45 +1,103 @@ -from rest_framework.decorators import api_view +from rest_framework import status, mixins, generics, viewsets from rest_framework.response import Response -from rest_framework import status +from rest_framework.views import APIView from django.shortcuts import get_object_or_404 -from cinema.models import Movie -from cinema.serializers import MovieSerializer +from cinema.models import Movie, Genre, Actor, CinemaHall +from cinema.serializers import ( + MovieSerializer, + GenreSerializer, + ActorSerializer, + CinemaHallSerializer, +) - -@api_view(["GET", "POST"]) -def movie_list(request): - if request.method == "GET": - movies = Movie.objects.all() - serializer = MovieSerializer(movies, many=True) +class GenreList(APIView): + def get(self, request): + genre = Genre.objects.all() + serializer = GenreSerializer(genre, many=True) return Response(serializer.data, status=status.HTTP_200_OK) - if request.method == "POST": - serializer = MovieSerializer(data=request.data) - if serializer.is_valid(): - serializer.save() - return Response(serializer.data, status=status.HTTP_201_CREATED) - - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + def post(self, request): + serializer = GenreSerializer(data=request.data) + serializer.is_valid(raise_exception=True) + serializer.save() + return Response(serializer.data, status=status.HTTP_201_CREATED) -@api_view(["GET", "PUT", "DELETE"]) -def movie_detail(request, pk): - movie = get_object_or_404(Movie, pk=pk) +class GenreDetail(APIView): + def get_object(self, pk): + return get_object_or_404(Genre, pk=pk) - if request.method == "GET": - serializer = MovieSerializer(movie) + def get(self, request, pk): + genre = self.get_object(pk) + serializer = GenreSerializer(genre) return Response(serializer.data, status=status.HTTP_200_OK) - if request.method == "PUT": - serializer = MovieSerializer(movie, data=request.data) - if serializer.is_valid(): - serializer.save() - return Response(serializer.data, status=status.HTTP_200_OK) + def put(self, request, pk): + genre = self.get_object(pk) + serializer = GenreSerializer(genre, data=request.data) + serializer.is_valid(raise_exception=True) + serializer.save() + return Response(serializer.data, status=status.HTTP_200_OK) - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + def patch(self, request, pk): + genre = self.get_object(pk) + serializer = GenreSerializer(genre, data=request.data, partial=True) + serializer.is_valid(raise_exception=True) + serializer.save() + return Response(serializer.data, status=status.HTTP_200_OK) - if request.method == "DELETE": - movie.delete() + def delete(self, request, pk): + genre = self.get_object(pk) + genre.delete() return Response(status=status.HTTP_204_NO_CONTENT) + + +class ActorList( + mixins.ListModelMixin, + mixins.CreateModelMixin, + generics.GenericAPIView +): + queryset = Actor.objects.all() + serializer_class = ActorSerializer + + def get(self, request, *args, **kwargs): + return self.list(request, *args, **kwargs) + + def post(self, request, *args, **kwargs): + return self.create(request, *args, **kwargs) + + +class ActorDetail( + mixins.RetrieveModelMixin, + mixins.UpdateModelMixin, + mixins.DestroyModelMixin, + generics.GenericAPIView +): + queryset = Actor.objects.all() + serializer_class = ActorSerializer + + def get(self, request, *args, **kwargs): + return self.retrieve(request, *args, **kwargs) + + def put(self, request, *args, **kwargs): + return self.update(request, *args, **kwargs) + + def delete(self, request, *args, **kwargs): + return self.destroy(request, *args, **kwargs) + + +class CinemaHallList(generics.ListCreateAPIView): + queryset = CinemaHall.objects.all() + serializer_class = CinemaHallSerializer + + +class CinemaHallDetail(generics.RetrieveUpdateDestroyAPIView): + queryset = CinemaHall.objects.all() + serializer_class = CinemaHallSerializer + + +class MovieViewSet(viewsets.ModelViewSet): + queryset = Movie.objects.all() + serializer_class = MovieSerializer From c3d09f60544c819d3b7bcca255a78c9d3b6497fe Mon Sep 17 00:00:00 2001 From: rivrxsq Date: Sun, 12 Jan 2025 01:18:43 +0200 Subject: [PATCH 2/7] fix flake8 errors --- cinema/models.py | 7 ++++--- cinema/serializers.py | 1 + cinema/urls.py | 3 +-- cinema/views.py | 1 + 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/cinema/models.py b/cinema/models.py index ce05da6f6..b3a479128 100644 --- a/cinema/models.py +++ b/cinema/models.py @@ -5,8 +5,8 @@ class Movie(models.Model): title = models.CharField(max_length=255) description = models.TextField() duration = models.IntegerField() - actors = models.ManyToManyField('Actor', related_name='movies') - genres = models.ManyToManyField('Genre', related_name='movies') + actors = models.ManyToManyField("Actor", related_name="movies") + genres = models.ManyToManyField("Genre", related_name="movies") def __str__(self): return self.title @@ -19,6 +19,7 @@ class Actor(models.Model): def __str__(self): return f"{self.first_name} {self.last_name}" + class Genre(models.Model): name = models.CharField(max_length=255, unique=True) @@ -32,4 +33,4 @@ class CinemaHall(models.Model): seats_in_row = models.IntegerField() def __str__(self): - return self.name + return self.name \ No newline at end of file diff --git a/cinema/serializers.py b/cinema/serializers.py index ed1d4671b..a0be696e4 100644 --- a/cinema/serializers.py +++ b/cinema/serializers.py @@ -23,6 +23,7 @@ def update(self, instance, validated_data): return instance + class GenreSerializer(serializers.ModelSerializer): class Meta: model = Genre diff --git a/cinema/urls.py b/cinema/urls.py index 809bc087c..74e81ba34 100644 --- a/cinema/urls.py +++ b/cinema/urls.py @@ -7,8 +7,7 @@ router.register("buses", MovieViewSet) urlpatterns = [ - path("", include(router.urls)) + path("", include(router.urls)), ] - app_name = "cinema" diff --git a/cinema/views.py b/cinema/views.py index a75d1223a..6f3f87791 100644 --- a/cinema/views.py +++ b/cinema/views.py @@ -12,6 +12,7 @@ CinemaHallSerializer, ) + class GenreList(APIView): def get(self, request): genre = Genre.objects.all() From 7ee71e529314b2d30996d59ff97cb34cdac3bf1d Mon Sep 17 00:00:00 2001 From: rivrxsq Date: Sun, 12 Jan 2025 01:20:23 +0200 Subject: [PATCH 3/7] add new line at end of models.py --- cinema/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cinema/models.py b/cinema/models.py index b3a479128..5e45fa1b5 100644 --- a/cinema/models.py +++ b/cinema/models.py @@ -33,4 +33,4 @@ class CinemaHall(models.Model): seats_in_row = models.IntegerField() def __str__(self): - return self.name \ No newline at end of file + return self.name From 19ec03f44d6ef1bf4458c7b7c172c15b789eaa66 Mon Sep 17 00:00:00 2001 From: rivrxsq Date: Sun, 12 Jan 2025 01:27:49 +0200 Subject: [PATCH 4/7] add urls --- ...emahall_genre_movie_actors_movie_genres.py | 47 +++++++++++++++++++ cinema/urls.py | 30 ++++++++---- 2 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 cinema/migrations/0002_actor_cinemahall_genre_movie_actors_movie_genres.py diff --git a/cinema/migrations/0002_actor_cinemahall_genre_movie_actors_movie_genres.py b/cinema/migrations/0002_actor_cinemahall_genre_movie_actors_movie_genres.py new file mode 100644 index 000000000..cd35e55f0 --- /dev/null +++ b/cinema/migrations/0002_actor_cinemahall_genre_movie_actors_movie_genres.py @@ -0,0 +1,47 @@ +# Generated by Django 4.1 on 2025-01-11 22:56 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('cinema', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='Actor', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('first_name', models.CharField(max_length=255)), + ('last_name', models.CharField(max_length=255)), + ], + ), + migrations.CreateModel( + name='CinemaHall', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=255)), + ('rows', models.IntegerField()), + ('seats_in_row', models.IntegerField()), + ], + ), + migrations.CreateModel( + name='Genre', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=255, unique=True)), + ], + ), + migrations.AddField( + model_name='movie', + name='actors', + field=models.ManyToManyField(to='cinema.actor'), + ), + migrations.AddField( + model_name='movie', + name='genres', + field=models.ManyToManyField(to='cinema.genre'), + ), + ] diff --git a/cinema/urls.py b/cinema/urls.py index 74e81ba34..453f524e6 100644 --- a/cinema/urls.py +++ b/cinema/urls.py @@ -1,13 +1,25 @@ -from django.urls import path, include -from rest_framework import routers +from django.urls import path +from rest_framework.routers import DefaultRouter +from cinema.views import ( + MovieViewSet, + GenreList, + GenreDetail, + CinemaHallList, + CinemaHallDetail, + ActorList, + ActorDetail +) -from cinema.views import MovieViewSet +router = DefaultRouter() +router.register("movies", MovieViewSet, basename="movie") -router = routers.DefaultRouter() -router.register("buses", MovieViewSet) +app_name = "cinema" urlpatterns = [ - path("", include(router.urls)), -] - -app_name = "cinema" + path("genres/", GenreList.as_view(), name="genre-list"), + path("genres//", GenreDetail.as_view(), name="genre-detail"), + path("actors/", ActorList.as_view(), name="actor-list"), + path("actors//", ActorDetail.as_view(), name="actor-detail"), + path("cinema_halls/", CinemaHallList.as_view(), name="actor-list"), + path("cinema_halls//", CinemaHallDetail.as_view(), name="actor-detail"), +] + router.urls \ No newline at end of file From 65378d44e27118a680668cc36654ccf080ffada0 Mon Sep 17 00:00:00 2001 From: rivrxsq Date: Sun, 12 Jan 2025 01:29:20 +0200 Subject: [PATCH 5/7] fix flake8 --- cinema/urls.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cinema/urls.py b/cinema/urls.py index 453f524e6..20216c84d 100644 --- a/cinema/urls.py +++ b/cinema/urls.py @@ -21,5 +21,9 @@ path("actors/", ActorList.as_view(), name="actor-list"), path("actors//", ActorDetail.as_view(), name="actor-detail"), path("cinema_halls/", CinemaHallList.as_view(), name="actor-list"), - path("cinema_halls//", CinemaHallDetail.as_view(), name="actor-detail"), -] + router.urls \ No newline at end of file + path( + "cinema_halls//", + CinemaHallDetail.as_view(), + name="actor-detail" + ), +] + router.urls From d72b2418d981893d1544a3afa2c574f35d0ca40d Mon Sep 17 00:00:00 2001 From: rivrxsq Date: Sun, 12 Jan 2025 01:37:38 +0200 Subject: [PATCH 6/7] fix errors --- cinema/urls.py | 18 ++++-------------- cinema/views.py | 49 ++++++++++++++++--------------------------------- 2 files changed, 20 insertions(+), 47 deletions(-) diff --git a/cinema/urls.py b/cinema/urls.py index 20216c84d..325654661 100644 --- a/cinema/urls.py +++ b/cinema/urls.py @@ -1,17 +1,13 @@ from django.urls import path from rest_framework.routers import DefaultRouter from cinema.views import ( - MovieViewSet, - GenreList, - GenreDetail, - CinemaHallList, - CinemaHallDetail, - ActorList, - ActorDetail + MovieViewSet, GenreList, GenreDetail, + CinemaHallViewSet, ActorList, ActorDetail ) router = DefaultRouter() router.register("movies", MovieViewSet, basename="movie") +router.register("cinema_halls", CinemaHallViewSet, basename="cinema_hall") app_name = "cinema" @@ -20,10 +16,4 @@ path("genres//", GenreDetail.as_view(), name="genre-detail"), path("actors/", ActorList.as_view(), name="actor-list"), path("actors//", ActorDetail.as_view(), name="actor-detail"), - path("cinema_halls/", CinemaHallList.as_view(), name="actor-list"), - path( - "cinema_halls//", - CinemaHallDetail.as_view(), - name="actor-detail" - ), -] + router.urls +] + router.urls \ No newline at end of file diff --git a/cinema/views.py b/cinema/views.py index 6f3f87791..7de7065fc 100644 --- a/cinema/views.py +++ b/cinema/views.py @@ -1,8 +1,17 @@ -from rest_framework import status, mixins, generics, viewsets +from rest_framework import status, viewsets +from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView +from rest_framework.mixins import ( + ListModelMixin, + CreateModelMixin, + RetrieveModelMixin, + UpdateModelMixin, + DestroyModelMixin +) from rest_framework.response import Response from rest_framework.views import APIView from django.shortcuts import get_object_or_404 +from rest_framework.viewsets import GenericViewSet from cinema.models import Movie, Genre, Actor, CinemaHall from cinema.serializers import ( @@ -55,46 +64,20 @@ def delete(self, request, pk): return Response(status=status.HTTP_204_NO_CONTENT) -class ActorList( - mixins.ListModelMixin, - mixins.CreateModelMixin, - generics.GenericAPIView -): +class ActorList(ListCreateAPIView): queryset = Actor.objects.all() serializer_class = ActorSerializer - def get(self, request, *args, **kwargs): - return self.list(request, *args, **kwargs) - - def post(self, request, *args, **kwargs): - return self.create(request, *args, **kwargs) - -class ActorDetail( - mixins.RetrieveModelMixin, - mixins.UpdateModelMixin, - mixins.DestroyModelMixin, - generics.GenericAPIView -): +class ActorDetail(RetrieveUpdateDestroyAPIView): queryset = Actor.objects.all() serializer_class = ActorSerializer - def get(self, request, *args, **kwargs): - return self.retrieve(request, *args, **kwargs) - - def put(self, request, *args, **kwargs): - return self.update(request, *args, **kwargs) - - def delete(self, request, *args, **kwargs): - return self.destroy(request, *args, **kwargs) - -class CinemaHallList(generics.ListCreateAPIView): - queryset = CinemaHall.objects.all() - serializer_class = CinemaHallSerializer - - -class CinemaHallDetail(generics.RetrieveUpdateDestroyAPIView): +class CinemaHallViewSet( + GenericViewSet, ListModelMixin, CreateModelMixin, + RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin +): queryset = CinemaHall.objects.all() serializer_class = CinemaHallSerializer From 59f590077cc3aa36d1768358ecbd7ff2c418e49c Mon Sep 17 00:00:00 2001 From: rivrxsq Date: Sun, 12 Jan 2025 01:39:36 +0200 Subject: [PATCH 7/7] fix flake8 --- cinema/urls.py | 2 +- cinema/views.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/cinema/urls.py b/cinema/urls.py index 325654661..f538cff14 100644 --- a/cinema/urls.py +++ b/cinema/urls.py @@ -16,4 +16,4 @@ path("genres//", GenreDetail.as_view(), name="genre-detail"), path("actors/", ActorList.as_view(), name="actor-list"), path("actors//", ActorDetail.as_view(), name="actor-detail"), -] + router.urls \ No newline at end of file +] + router.urls diff --git a/cinema/views.py b/cinema/views.py index 7de7065fc..d8068dc53 100644 --- a/cinema/views.py +++ b/cinema/views.py @@ -1,5 +1,8 @@ from rest_framework import status, viewsets -from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView +from rest_framework.generics import ( + ListCreateAPIView, + RetrieveUpdateDestroyAPIView +) from rest_framework.mixins import ( ListModelMixin, CreateModelMixin,