From ce87ee6033023d0ab57c3c00b580f9a57d1f2d61 Mon Sep 17 00:00:00 2001 From: CaCtUs1k Date: Tue, 10 Oct 2023 21:00:58 +0300 Subject: [PATCH] Solution --- cinema/migrations/0001_initial.py | 1 - ...emahall_genre_movie_actors_movie_genres.py | 8 +++++-- cinema/serializers.py | 24 ++++++++++++------- cinema/urls.py | 24 +++++++++++-------- cinema/views.py | 17 +++++++------ 5 files changed, 45 insertions(+), 29 deletions(-) diff --git a/cinema/migrations/0001_initial.py b/cinema/migrations/0001_initial.py index 514947a64..8f0b180c1 100644 --- a/cinema/migrations/0001_initial.py +++ b/cinema/migrations/0001_initial.py @@ -4,7 +4,6 @@ class Migration(migrations.Migration): - initial = True dependencies = [] 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 index b3d3cdb66..be8735aaf 100644 --- a/cinema/migrations/0002_actor_cinemahall_genre_movie_actors_movie_genres.py +++ b/cinema/migrations/0002_actor_cinemahall_genre_movie_actors_movie_genres.py @@ -60,11 +60,15 @@ class Migration(migrations.Migration): migrations.AddField( model_name="movie", name="actors", - field=models.ManyToManyField(related_name="movies", to="cinema.actor"), + field=models.ManyToManyField( + related_name="movies", to="cinema.actor" + ), ), migrations.AddField( model_name="movie", name="genres", - field=models.ManyToManyField(related_name="movies", to="cinema.genre"), + field=models.ManyToManyField( + related_name="movies", to="cinema.genre" + ), ), ] diff --git a/cinema/serializers.py b/cinema/serializers.py index 0f08cfdac..b044fe020 100644 --- a/cinema/serializers.py +++ b/cinema/serializers.py @@ -6,28 +6,34 @@ class MovieSerializer(serializers.Serializer): title = serializers.CharField(max_length=255) description = serializers.CharField() - actors = serializers.PrimaryKeyRelatedField(required=False, queryset=Actor.objects.all(), many=True) - genres = serializers.PrimaryKeyRelatedField(required=False, queryset=Genre.objects.all(), many=True) + actors = serializers.PrimaryKeyRelatedField( + required=False, queryset=Actor.objects.all(), many=True + ) + genres = serializers.PrimaryKeyRelatedField( + required=False, queryset=Genre.objects.all(), many=True + ) duration = serializers.IntegerField(min_value=1) def create(self, validated_data): - actors_data = validated_data.pop('actors', []) - genres_data = validated_data.pop('genres', []) + actors_data = validated_data.pop("actors", []) + genres_data = validated_data.pop("genres", []) movie = Movie.objects.create(**validated_data) movie.actors.set(actors_data) movie.genres.set(genres_data) return movie def update(self, instance, validated_data): - instance.title = validated_data.get('title', instance.title) - instance.description = validated_data.get('description', instance.description) - instance.duration = validated_data.get('duration', instance.duration) + instance.title = validated_data.get("title", instance.title) + instance.description = validated_data.get( + "description", instance.description + ) + instance.duration = validated_data.get("duration", instance.duration) - actors_data = validated_data.get('actors') + actors_data = validated_data.get("actors") if actors_data is not None: instance.actors.set(actors_data) - genres_data = validated_data.get('genres') + genres_data = validated_data.get("genres") if genres_data is not None: instance.genres.set(genres_data) diff --git a/cinema/urls.py b/cinema/urls.py index b03859848..e0f165832 100644 --- a/cinema/urls.py +++ b/cinema/urls.py @@ -1,6 +1,13 @@ from django.urls import path, include -from cinema.views import GenreList, GenreDetail, ActorDetail, ActorList, CinemaHallViewSet, MovieViewSet +from cinema.views import ( + GenreList, + GenreDetail, + ActorDetail, + ActorList, + CinemaHallViewSet, + MovieViewSet, +) from rest_framework import routers router = routers.DefaultRouter() @@ -9,20 +16,15 @@ router.register("movies", MovieViewSet) cinema_hall_list = CinemaHallViewSet.as_view( - actions= - { - "get": "list", - "post": "create" - } + actions={"get": "list", "post": "create"} ) cinema_hall_detail = CinemaHallViewSet.as_view( - actions= - { + actions={ "get": "retrieve", "put": "update", "patch": "partial_update", - "delete": "destroy" + "delete": "destroy", } ) @@ -33,7 +35,9 @@ path("actors/", ActorList.as_view(), name="actor-list"), path("actors//", ActorDetail.as_view(), name="actor-detail"), path("cinema_halls/", cinema_hall_list, name="cinema-hall-list"), - path("cinema_halls//", cinema_hall_detail, name="cinema-hall-detail"), + path( + "cinema_halls//", cinema_hall_detail, name="cinema-hall-detail" + ), ] app_name = "cinema" diff --git a/cinema/views.py b/cinema/views.py index f3b1b7a84..3c553a085 100644 --- a/cinema/views.py +++ b/cinema/views.py @@ -9,7 +9,12 @@ from rest_framework.viewsets import GenericViewSet, ModelViewSet from cinema.models import Movie, Genre, Actor, CinemaHall -from cinema.serializers import MovieSerializer, GenreSerializer, ActorSerializer, CinemaHallSerializer +from cinema.serializers import ( + MovieSerializer, + GenreSerializer, + ActorSerializer, + CinemaHallSerializer, +) class GenreList(APIView): @@ -28,7 +33,6 @@ def post(self, request): class GenreDetail(APIView): - def get(self, request, pk): genre = get_object_or_404(Genre, pk=pk) serializer = GenreSerializer(genre) @@ -59,9 +63,7 @@ def delete(self, request, pk): class ActorList( - mixins.ListModelMixin, - mixins.CreateModelMixin, - GenericAPIView + mixins.ListModelMixin, mixins.CreateModelMixin, GenericAPIView ): queryset = Actor.objects.all() serializer_class = ActorSerializer @@ -77,10 +79,11 @@ class ActorDetail( mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, - GenericAPIView + GenericAPIView, ): queryset = Actor.objects.all() serializer_class = ActorSerializer + def get(self, request, *args, **kwargs): return self.retrieve(request, *args, **kwargs) @@ -100,7 +103,7 @@ class CinemaHallViewSet( mixins.UpdateModelMixin, mixins.DestroyModelMixin, mixins.ListModelMixin, - GenericViewSet + GenericViewSet, ): queryset = CinemaHall.objects.all() serializer_class = CinemaHallSerializer