From a9f9c7bf54b813c52356a42ce4b42a20767538f0 Mon Sep 17 00:00:00 2001 From: "artem.puhach" Date: Fri, 22 Nov 2024 13:02:54 +0200 Subject: [PATCH 1/5] created API's for Genre GET api/cinema// - should return a list of the all entity items POST api/cinema// - should create a new entity based on passed data GET api/cinema/// - should return an entity with given id PUT api/cinema/// - should update the entity with given id based on passed data --- ...emahall_genre_movie_actors_movie_genres.py | 47 +++++++++++++++++++ cinema/models.py | 22 +++++++++ cinema/serializers.py | 39 ++++++++++++++- cinema/urls.py | 4 +- cinema/views.py | 40 +++++++++++++++- 5 files changed, 148 insertions(+), 4 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..70fa31b8d --- /dev/null +++ b/cinema/migrations/0002_actor_cinemahall_genre_movie_actors_movie_genres.py @@ -0,0 +1,47 @@ +# Generated by Django 4.1 on 2024-11-22 10:06 + +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/models.py b/cinema/models.py index cc477513f..e06c60ba1 100644 --- a/cinema/models.py +++ b/cinema/models.py @@ -1,10 +1,32 @@ from django.db import models + + + +class Actor(models.Model): + first_name = models.CharField(max_length=255) + last_name = models.CharField(max_length=255) + + +class Genre(models.Model): + name = models.CharField(max_length=255, unique=True) + + +class CinemaHall(models.Model): + name = models.CharField(max_length=255) + rows = models.IntegerField() + seats_in_row = models.IntegerField() + + class Movie(models.Model): title = models.CharField(max_length=255) description = models.TextField() + actors = models.ManyToManyField(Actor) + genres = models.ManyToManyField(Genre) duration = models.IntegerField() def __str__(self): return self.title + + diff --git a/cinema/serializers.py b/cinema/serializers.py index 050db5771..6317994e7 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 class MovieSerializer(serializers.Serializer): @@ -9,6 +9,7 @@ class MovieSerializer(serializers.Serializer): description = serializers.CharField() duration = serializers.IntegerField() + def create(self, validated_data): return Movie.objects.create(**validated_data) @@ -22,3 +23,39 @@ def update(self, instance, validated_data): instance.save() return instance + + + +class ActorSerializer(serializers.Serializer): + id = serializers.IntegerField(read_only=True) + first_name = serializers.CharField(max_length=255) + last_name = serializers.CharField(max_length=255) + + +# class GenreSerializer(serializers.Serializer): +# id = serializers.IntegerField(read_only=True) +# name = serializers.CharField(max_length=255) +# +# def create(self, validated_data): +# return Genre.objects.create(**validated_data) +# +# def update(self, instance, validated_data): +# instance.name = validated_data.get("name", instance.name) +# instance.save() +# return instance + + +class GenreSerializer(serializers.ModelSerializer): + class Meta: + model = Genre + fields = "__all__" + + +class CinemaHallSerializer(serializers.Serializer): + id = serializers.IntegerField(read_only=True) + name = serializers.CharField(max_length=255) + rows = serializers.IntegerField() + seats_in_row = serializers.IntegerField() + + + diff --git a/cinema/urls.py b/cinema/urls.py index 1ae7d5cb0..921dc60cc 100644 --- a/cinema/urls.py +++ b/cinema/urls.py @@ -1,10 +1,12 @@ from django.urls import path -from cinema.views import movie_list, movie_detail +from cinema.views import movie_list, movie_detail, GenreList, GenreDetail urlpatterns = [ path("movies/", movie_list, name="movie-list"), path("movies//", movie_detail, name="movie-detail"), + path("genres/", GenreList.as_view(), name="genre-list"), + path("genres//", GenreDetail.as_view(), name="genre-detail"), ] app_name = "cinema" diff --git a/cinema/views.py b/cinema/views.py index 78ba8a79c..6ac35cf1c 100644 --- a/cinema/views.py +++ b/cinema/views.py @@ -3,9 +3,10 @@ from rest_framework import status from django.shortcuts import get_object_or_404 +from rest_framework.views import APIView -from cinema.models import Movie -from cinema.serializers import MovieSerializer +from cinema.models import Movie, Genre +from cinema.serializers import MovieSerializer, GenreSerializer @api_view(["GET", "POST"]) @@ -43,3 +44,38 @@ def movie_detail(request, pk): if request.method == "DELETE": movie.delete() return Response(status=status.HTTP_204_NO_CONTENT) + + +class GenreList(APIView): + def get_object(self, pk): + return get_object_or_404(Genre, pk=pk) + + def get(self, request, format=None): + genres = Genre.objects.all() + serializer = GenreSerializer(genres, many=True) + return Response(serializer.data, status=status.HTTP_200_OK) + + def post(self, request, format=None): + serializer = GenreSerializer(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) + + +class GenreDetail(APIView): + def get_object(self, pk): + return get_object_or_404(Genre, pk=pk) + + def get(self, request, pk, format=None): + genre = self.get_object(pk) + serializer = GenreSerializer(genre) + return Response(serializer.data, status=status.HTTP_200_OK) + + def put(self, request, pk, format=None): + genre = self.get_object(pk) + serializer = GenreSerializer(genre, data=request.data) + if serializer.is_valid(): + serializer.save() + return Response(serializer.data, status=status.HTTP_200_OK) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) \ No newline at end of file From 16163f70bf79c8d48b160f196dac1287a76f06f5 Mon Sep 17 00:00:00 2001 From: "artem.puhach" Date: Fri, 22 Nov 2024 17:47:12 +0200 Subject: [PATCH 2/5] PATCH api/cinema/// - should partially update the entity with given id based on passed data --- cinema/views.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/cinema/views.py b/cinema/views.py index 6ac35cf1c..64236ef63 100644 --- a/cinema/views.py +++ b/cinema/views.py @@ -73,6 +73,19 @@ def get(self, request, pk, format=None): return Response(serializer.data, status=status.HTTP_200_OK) def put(self, request, pk, format=None): + genre = self.get_object(pk) + serializer = GenreSerializer(genre, data=request.data) + if serializer.is_valid(): + serializer.save() + return Response(serializer.data, status=status.HTTP_200_OK) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + + def delete(self, request, pk, format=None): + genre = self.get_object(pk) + genre.delete() + return Response(status=status.HTTP_204_NO_CONTENT) + + def patch(self, request, pk, format=None): genre = self.get_object(pk) serializer = GenreSerializer(genre, data=request.data) if serializer.is_valid(): From 36d822f66ebaa8728165b707c376f4d5888b2c41 Mon Sep 17 00:00:00 2001 From: "artem.puhach" Date: Sat, 23 Nov 2024 12:15:10 +0200 Subject: [PATCH 3/5] For the CinemaHall model use a GenericViewSet --- cinema/serializers.py | 32 +++++++---------------- cinema/urls.py | 21 ++++++++++++++- cinema/views.py | 59 +++++++++++++++++++++++++++++++++++++++---- 3 files changed, 83 insertions(+), 29 deletions(-) diff --git a/cinema/serializers.py b/cinema/serializers.py index 6317994e7..18dee78d0 100644 --- a/cinema/serializers.py +++ b/cinema/serializers.py @@ -1,6 +1,6 @@ from rest_framework import serializers -from cinema.models import Movie, Genre +from cinema.models import Movie, Genre, Actor, CinemaHall class MovieSerializer(serializers.Serializer): @@ -26,23 +26,10 @@ def update(self, instance, validated_data): -class ActorSerializer(serializers.Serializer): - id = serializers.IntegerField(read_only=True) - first_name = serializers.CharField(max_length=255) - last_name = serializers.CharField(max_length=255) - - -# class GenreSerializer(serializers.Serializer): -# id = serializers.IntegerField(read_only=True) -# name = serializers.CharField(max_length=255) -# -# def create(self, validated_data): -# return Genre.objects.create(**validated_data) -# -# def update(self, instance, validated_data): -# instance.name = validated_data.get("name", instance.name) -# instance.save() -# return instance +class ActorSerializer(serializers.ModelSerializer): + class Meta: + model = Actor + fields = "__all__" class GenreSerializer(serializers.ModelSerializer): @@ -51,11 +38,10 @@ class Meta: fields = "__all__" -class CinemaHallSerializer(serializers.Serializer): - id = serializers.IntegerField(read_only=True) - name = serializers.CharField(max_length=255) - rows = serializers.IntegerField() - seats_in_row = serializers.IntegerField() +class CinemaHallSerializer(serializers.ModelSerializer): + class Meta: + model = CinemaHall + fields = "__all__" diff --git a/cinema/urls.py b/cinema/urls.py index 921dc60cc..9df592885 100644 --- a/cinema/urls.py +++ b/cinema/urls.py @@ -1,12 +1,31 @@ from django.urls import path -from cinema.views import movie_list, movie_detail, GenreList, GenreDetail +from cinema.views import movie_list, movie_detail, GenreList, GenreDetail, ActorList, ActorDetail, CinemaHallViewSet + +cinema_hall_list = CinemaHallViewSet.as_view(actions={ + 'get': 'list', + 'post': 'create' +}) + +cinema_hall_detail = CinemaHallViewSet.as_view(actions={ + 'get': 'retrieve', + 'put': 'update', + 'delete': 'destroy', + 'patch': 'partial_update' +}) urlpatterns = [ path("movies/", movie_list, name="movie-list"), path("movies//", movie_detail, name="movie-detail"), 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/", cinema_hall_list, name="cinema-hall-list"), + path("cinema-halls//", cinema_hall_detail, name="cinema-hall-detail"), ] app_name = "cinema" diff --git a/cinema/views.py b/cinema/views.py index 64236ef63..241642368 100644 --- a/cinema/views.py +++ b/cinema/views.py @@ -1,12 +1,12 @@ from rest_framework.decorators import api_view from rest_framework.response import Response -from rest_framework import status +from rest_framework import status, generics, mixins, viewsets from django.shortcuts import get_object_or_404 from rest_framework.views import APIView -from cinema.models import Movie, Genre -from cinema.serializers import MovieSerializer, GenreSerializer +from cinema.models import Movie, Genre, Actor, CinemaHall +from cinema.serializers import MovieSerializer, GenreSerializer, ActorSerializer, CinemaHallSerializer @api_view(["GET", "POST"]) @@ -87,8 +87,57 @@ def delete(self, request, pk, format=None): def patch(self, request, pk, format=None): genre = self.get_object(pk) - serializer = GenreSerializer(genre, data=request.data) + serializer = GenreSerializer(genre, data=request.data, partial=True) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) \ No newline at end of file + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + + +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) + + def patch(self, request, *args, **kwargs): + return self.partial_update(request, *args, **kwargs) + + +class CinemaHallViewSet( + viewsets.GenericViewSet, + mixins.ListModelMixin, + mixins.CreateModelMixin, + mixins.RetrieveModelMixin, + mixins.UpdateModelMixin, + mixins.DestroyModelMixin, +): + queryset = CinemaHall.objects.all() + serializer_class = CinemaHallSerializer \ No newline at end of file From 1e35dc13bca70a767b9d349ccce0543843dcc59a Mon Sep 17 00:00:00 2001 From: "artem.puhach" Date: Sat, 23 Nov 2024 14:15:46 +0200 Subject: [PATCH 4/5] ready solution! --- ...3_alter_movie_actors_alter_movie_genres.py | 23 +++++++++ cinema/models.py | 4 +- cinema/serializers.py | 22 ++------- cinema/urls.py | 26 +++++++--- cinema/views.py | 49 +++++-------------- 5 files changed, 59 insertions(+), 65 deletions(-) create mode 100644 cinema/migrations/0003_alter_movie_actors_alter_movie_genres.py diff --git a/cinema/migrations/0003_alter_movie_actors_alter_movie_genres.py b/cinema/migrations/0003_alter_movie_actors_alter_movie_genres.py new file mode 100644 index 000000000..856d383de --- /dev/null +++ b/cinema/migrations/0003_alter_movie_actors_alter_movie_genres.py @@ -0,0 +1,23 @@ +# Generated by Django 4.1 on 2024-11-23 12:08 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('cinema', '0002_actor_cinemahall_genre_movie_actors_movie_genres'), + ] + + operations = [ + migrations.AlterField( + model_name='movie', + name='actors', + field=models.ManyToManyField(related_name='movies', to='cinema.actor'), + ), + migrations.AlterField( + model_name='movie', + name='genres', + field=models.ManyToManyField(related_name='movies', to='cinema.genre'), + ), + ] diff --git a/cinema/models.py b/cinema/models.py index e06c60ba1..e24ce233c 100644 --- a/cinema/models.py +++ b/cinema/models.py @@ -22,8 +22,8 @@ class CinemaHall(models.Model): class Movie(models.Model): title = models.CharField(max_length=255) description = models.TextField() - actors = models.ManyToManyField(Actor) - genres = models.ManyToManyField(Genre) + actors = models.ManyToManyField(Actor, related_name='movies') + genres = models.ManyToManyField(Genre, related_name='movies') duration = models.IntegerField() def __str__(self): diff --git a/cinema/serializers.py b/cinema/serializers.py index 18dee78d0..0b701817b 100644 --- a/cinema/serializers.py +++ b/cinema/serializers.py @@ -3,26 +3,12 @@ from cinema.models import Movie, Genre, Actor, CinemaHall -class MovieSerializer(serializers.Serializer): - id = serializers.IntegerField(read_only=True) - title = serializers.CharField(max_length=255) - description = serializers.CharField() - duration = serializers.IntegerField() - - - def create(self, validated_data): - return Movie.objects.create(**validated_data) - - 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) +class MovieSerializer(serializers.ModelSerializer): + class Meta: + model = Movie + fields = ("id", "title", "description", "duration") - instance.save() - return instance diff --git a/cinema/urls.py b/cinema/urls.py index 9df592885..c4bd892c9 100644 --- a/cinema/urls.py +++ b/cinema/urls.py @@ -1,6 +1,14 @@ -from django.urls import path - -from cinema.views import movie_list, movie_detail, GenreList, GenreDetail, ActorList, ActorDetail, CinemaHallViewSet +from django.urls import path, include +from rest_framework import routers + +from cinema.views import ( + GenreList, + GenreDetail, + ActorList, + ActorDetail, + CinemaHallViewSet, + MovieViewSet +) cinema_hall_list = CinemaHallViewSet.as_view(actions={ 'get': 'list', @@ -14,9 +22,13 @@ 'patch': 'partial_update' }) + +router = routers.DefaultRouter() +router.register("movies", MovieViewSet) + urlpatterns = [ - path("movies/", movie_list, name="movie-list"), - path("movies//", movie_detail, name="movie-detail"), + + path("", include(router.urls)), path("genres/", GenreList.as_view(), name="genre-list"), path("genres//", GenreDetail.as_view(), name="genre-detail"), @@ -24,8 +36,8 @@ 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_list, name="cinema-hall-list"), + path("cinema_halls//", cinema_hall_detail, name="cinema-hall-detail"), ] app_name = "cinema" diff --git a/cinema/views.py b/cinema/views.py index 241642368..488e817c2 100644 --- a/cinema/views.py +++ b/cinema/views.py @@ -6,44 +6,17 @@ from rest_framework.views import APIView from cinema.models import Movie, Genre, Actor, CinemaHall -from cinema.serializers import MovieSerializer, GenreSerializer, ActorSerializer, CinemaHallSerializer +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) - 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) - - -@api_view(["GET", "PUT", "DELETE"]) -def movie_detail(request, pk): - movie = get_object_or_404(Movie, pk=pk) - - if request.method == "GET": - serializer = MovieSerializer(movie) - 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) - - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) - - if request.method == "DELETE": - movie.delete() - return Response(status=status.HTTP_204_NO_CONTENT) +class MovieViewSet(viewsets.ModelViewSet): + queryset = Movie.objects.all() + serializer_class = MovieSerializer class GenreList(APIView): @@ -132,12 +105,12 @@ def patch(self, request, *args, **kwargs): class CinemaHallViewSet( - viewsets.GenericViewSet, mixins.ListModelMixin, mixins.CreateModelMixin, mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, + viewsets.GenericViewSet, ): queryset = CinemaHall.objects.all() - serializer_class = CinemaHallSerializer \ No newline at end of file + serializer_class = CinemaHallSerializer From 5ee3bace488b7d525032cd38eb6e1d5b9a27693a Mon Sep 17 00:00:00 2001 From: "artem.puhach" Date: Sat, 23 Nov 2024 14:31:55 +0200 Subject: [PATCH 5/5] flake8 fix --- ...emahall_genre_movie_actors_movie_genres.py | 62 +++++++++++++------ ...3_alter_movie_actors_alter_movie_genres.py | 14 ++--- cinema/models.py | 9 +-- cinema/serializers.py | 6 -- cinema/urls.py | 36 ++++++----- cinema/views.py | 6 +- 6 files changed, 74 insertions(+), 59 deletions(-) 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 70fa31b8d..31ef9ef50 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 @@ -6,42 +6,66 @@ class Migration(migrations.Migration): dependencies = [ - ('cinema', '0001_initial'), + ("cinema", "0001_initial"), ] operations = [ migrations.CreateModel( - name='Actor', + 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)), + ( + "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', + 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()), + ( + "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', + 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)), + ( + "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'), + model_name="movie", + name="actors", + field=models.ManyToManyField(to="cinema.actor"), ), migrations.AddField( - model_name='movie', - name='genres', - field=models.ManyToManyField(to='cinema.genre'), + model_name="movie", + name="genres", + field=models.ManyToManyField(to="cinema.genre"), ), ] diff --git a/cinema/migrations/0003_alter_movie_actors_alter_movie_genres.py b/cinema/migrations/0003_alter_movie_actors_alter_movie_genres.py index 856d383de..70102d31e 100644 --- a/cinema/migrations/0003_alter_movie_actors_alter_movie_genres.py +++ b/cinema/migrations/0003_alter_movie_actors_alter_movie_genres.py @@ -6,18 +6,18 @@ class Migration(migrations.Migration): dependencies = [ - ('cinema', '0002_actor_cinemahall_genre_movie_actors_movie_genres'), + ("cinema", "0002_actor_cinemahall_genre_movie_actors_movie_genres"), ] operations = [ migrations.AlterField( - model_name='movie', - name='actors', - field=models.ManyToManyField(related_name='movies', to='cinema.actor'), + model_name="movie", + name="actors", + field=models.ManyToManyField(related_name="movies", to="cinema.actor"), ), migrations.AlterField( - model_name='movie', - name='genres', - field=models.ManyToManyField(related_name='movies', to='cinema.genre'), + model_name="movie", + name="genres", + field=models.ManyToManyField(related_name="movies", to="cinema.genre"), ), ] diff --git a/cinema/models.py b/cinema/models.py index e24ce233c..b0993e696 100644 --- a/cinema/models.py +++ b/cinema/models.py @@ -1,9 +1,6 @@ from django.db import models - - - class Actor(models.Model): first_name = models.CharField(max_length=255) last_name = models.CharField(max_length=255) @@ -22,11 +19,9 @@ class CinemaHall(models.Model): class Movie(models.Model): title = models.CharField(max_length=255) description = models.TextField() - 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") duration = models.IntegerField() def __str__(self): return self.title - - diff --git a/cinema/serializers.py b/cinema/serializers.py index 0b701817b..51e741fea 100644 --- a/cinema/serializers.py +++ b/cinema/serializers.py @@ -9,9 +9,6 @@ class Meta: fields = ("id", "title", "description", "duration") - - - class ActorSerializer(serializers.ModelSerializer): class Meta: model = Actor @@ -28,6 +25,3 @@ class CinemaHallSerializer(serializers.ModelSerializer): class Meta: model = CinemaHall fields = "__all__" - - - diff --git a/cinema/urls.py b/cinema/urls.py index c4bd892c9..d7e8c5e71 100644 --- a/cinema/urls.py +++ b/cinema/urls.py @@ -7,37 +7,41 @@ ActorList, ActorDetail, CinemaHallViewSet, - MovieViewSet + MovieViewSet, ) -cinema_hall_list = CinemaHallViewSet.as_view(actions={ - 'get': 'list', - 'post': 'create' -}) +cinema_hall_list = CinemaHallViewSet.as_view( + actions={ + "get": "list", + "post": "create" + } +) -cinema_hall_detail = CinemaHallViewSet.as_view(actions={ - 'get': 'retrieve', - 'put': 'update', - 'delete': 'destroy', - 'patch': 'partial_update' -}) +cinema_hall_detail = CinemaHallViewSet.as_view( + actions={ + "get": "retrieve", + "put": "update", + "delete": "destroy", + "patch": "partial_update", + } +) router = routers.DefaultRouter() router.register("movies", MovieViewSet) urlpatterns = [ - path("", include(router.urls)), 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/", 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 488e817c2..7aff7f5fd 100644 --- a/cinema/views.py +++ b/cinema/views.py @@ -68,9 +68,7 @@ def patch(self, request, pk, format=None): class ActorList( - mixins.ListModelMixin, - mixins.CreateModelMixin, - generics.GenericAPIView + mixins.ListModelMixin, mixins.CreateModelMixin, generics.GenericAPIView ): queryset = Actor.objects.all() serializer_class = ActorSerializer @@ -86,7 +84,7 @@ class ActorDetail( mixins.RetrieveModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, - generics.GenericAPIView + generics.GenericAPIView, ): queryset = Actor.objects.all() serializer_class = ActorSerializer