From 2fe9818e493e23e3b2e09244be54a16c47347ba4 Mon Sep 17 00:00:00 2001 From: popeye88 Date: Mon, 25 Nov 2024 23:06:35 +0200 Subject: [PATCH 1/2] solution --- cinema/admin.py | 17 +++++- cinema/models.py | 32 ++++++++++- cinema/serializers.py | 35 ++++++------ cinema/urls.py | 62 +++++++++++++++++++-- cinema/views.py | 124 +++++++++++++++++++++++++++++++----------- requirements.txt | 20 ++++++- 6 files changed, 233 insertions(+), 57 deletions(-) diff --git a/cinema/admin.py b/cinema/admin.py index 06a2c4616..cf23dbfba 100644 --- a/cinema/admin.py +++ b/cinema/admin.py @@ -1,8 +1,23 @@ from django.contrib import admin -from cinema.models import Movie +from cinema.models import Actor, CinemaHall, Genre, Movie @admin.register(Movie) class MovieAdmin(admin.ModelAdmin): pass + + +@admin.register(Actor) +class ActorAdmin(admin.ModelAdmin): + pass + + +@admin.register(Genre) +class GenreAdmin(admin.ModelAdmin): + pass + + +@admin.register(CinemaHall) +class CinemaHallAdmin(admin.ModelAdmin): + pass diff --git a/cinema/models.py b/cinema/models.py index cc477513f..c089b9aeb 100644 --- a/cinema/models.py +++ b/cinema/models.py @@ -1,10 +1,38 @@ from django.db import models +CHAR_FIELD_MAX_LENGTH = 255 + + +class CinemaHall(models.Model): + name = models.CharField(max_length=CHAR_FIELD_MAX_LENGTH) + rows = models.PositiveIntegerField() + seats_in_row = models.PositiveIntegerField() + + def __str__(self): + return self.name + + +class Genre(models.Model): + name = models.CharField(max_length=CHAR_FIELD_MAX_LENGTH, unique=True) + + def __str__(self): + return self.name + + +class Actor(models.Model): + first_name = models.CharField(max_length=CHAR_FIELD_MAX_LENGTH) + last_name = models.CharField(max_length=CHAR_FIELD_MAX_LENGTH) + + def __str__(self): + return f"{self.first_name} {self.last_name}" + class Movie(models.Model): - title = models.CharField(max_length=255) + title = models.CharField(max_length=CHAR_FIELD_MAX_LENGTH) description = models.TextField() - duration = models.IntegerField() + duration = models.PositiveIntegerField() # Duration in minutes + genres = models.ManyToManyField(Genre, related_name="movies") + actors = models.ManyToManyField(Actor, related_name="movies") def __str__(self): return self.title diff --git a/cinema/serializers.py b/cinema/serializers.py index 050db5771..2a75087a9 100644 --- a/cinema/serializers.py +++ b/cinema/serializers.py @@ -1,24 +1,27 @@ from rest_framework import serializers -from cinema.models import Movie +from cinema.models import Actor, CinemaHall, Genre, Movie -class MovieSerializer(serializers.Serializer): - id = serializers.IntegerField(read_only=True) - title = serializers.CharField(max_length=255) - description = serializers.CharField() - duration = serializers.IntegerField() +class GenreSerializer(serializers.ModelSerializer): + class Meta: + model = Genre + fields = "__all__" - 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 ActorSerializer(serializers.ModelSerializer): + class Meta: + model = Actor + fields = "__all__" - instance.save() - return instance +class CinemaHallSerializer(serializers.ModelSerializer): + class Meta: + model = CinemaHall + fields = "__all__" + + +class MovieSerializer(serializers.ModelSerializer): + class Meta: + model = Movie + exclude = ["genres", "actors"] diff --git a/cinema/urls.py b/cinema/urls.py index 1ae7d5cb0..35c0c753a 100644 --- a/cinema/urls.py +++ b/cinema/urls.py @@ -1,10 +1,64 @@ -from django.urls import path +from django.urls import include, path +from rest_framework import routers -from cinema.views import movie_list, movie_detail +from cinema.views import ( + ActorDetail, + ActorList, + CinemaHallViewSet, + GenreDetail, + GenreList, + MovieViewSet, +) +router = routers.DefaultRouter() +router.register("movies", MovieViewSet) + +cinema_halls = CinemaHallViewSet.as_view( + actions={ + "get": "list", + "post": "create", + } +) +cinema_hall_detail = CinemaHallViewSet.as_view( + actions={ + "get": "retrieve", + "put": "update", + "patch": "partial_update", + "delete": "destroy", + } +) urlpatterns = [ - path("movies/", movie_list, name="movie-list"), - path("movies//", movie_detail, name="movie-detail"), + path( + "genres/", + GenreList.as_view(), + name="genres", + ), + path( + "genres//", + GenreDetail.as_view(), + name="genre-detail", + ), + path( + "actors/", + ActorList.as_view(), + name="actors", + ), + path( + "actors//", + ActorDetail.as_view(), + name="actor-detail", + ), + path( + "cinema_halls/", + cinema_halls, + name="cinema_hall-list", + ), + path( + "cinema_halls//", + cinema_hall_detail, + name="cinema_hall-detail", + ), + path("", include(router.urls)), ] app_name = "cinema" diff --git a/cinema/views.py b/cinema/views.py index 78ba8a79c..dd7a4d2f6 100644 --- a/cinema/views.py +++ b/cinema/views.py @@ -1,45 +1,107 @@ -from rest_framework.decorators import api_view +from rest_framework import generics, mixins, status, viewsets +from rest_framework.generics import get_object_or_404 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 Actor, CinemaHall, Genre, Movie +from cinema.serializers import ( + ActorSerializer, + CinemaHallSerializer, + GenreSerializer, + MovieSerializer, +) -from cinema.models import Movie -from cinema.serializers import MovieSerializer - -@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) + def post(self, request): + serializer = GenreSerializer(data=request.data) + 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) -@api_view(["GET", "PUT", "DELETE"]) -def movie_detail(request, pk): - movie = get_object_or_404(Movie, pk=pk) + def get(self, request, pk: int) -> Response: + serializer = GenreSerializer(self.get_object(pk)) + return Response(serializer.data) - if request.method == "GET": - serializer = MovieSerializer(movie) + def put(self, request, pk: int) -> Response: + serializer = GenreSerializer(self.get_object(pk), data=request.data) + serializer.is_valid(raise_exception=True) + serializer.save() 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) + def patch(self, request, pk: int) -> Response: + serializer = GenreSerializer( + self.get_object(pk), 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: int) -> Response: + genre = self.get_object(pk) + genre.delete() return Response(status=status.HTTP_204_NO_CONTENT) + + +class ActorList( + generics.GenericAPIView, + mixins.ListModelMixin, + mixins.CreateModelMixin, +): + 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( + generics.RetrieveUpdateDestroyAPIView, + mixins.RetrieveModelMixin, + mixins.UpdateModelMixin, + mixins.DestroyModelMixin, +): + 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 patch(self, request, *args, **kwargs): + return self.partial_update(request, *args, **kwargs) + + def delete(self, request, *args, **kwargs): + return self.destroy(request, *args, **kwargs) + + +class CinemaHallViewSet( + viewsets.GenericViewSet, + mixins.ListModelMixin, + mixins.CreateModelMixin, + mixins.RetrieveModelMixin, + mixins.UpdateModelMixin, + mixins.DestroyModelMixin, +): + queryset = CinemaHall.objects.all() + serializer_class = CinemaHallSerializer + + +class MovieViewSet(viewsets.ModelViewSet): + queryset = Movie.objects.all() + serializer_class = MovieSerializer diff --git a/requirements.txt b/requirements.txt index 99938e15f..f1d518deb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,21 @@ -django==4.1 +asgiref==3.8.1 +black==24.10.0 +click==8.1.7 +Django==5.1.2 +django-debug-toolbar==3.2.4 +djangorestframework==3.15.2 flake8==5.0.4 flake8-quotes==3.3.1 flake8-variables-names==0.0.5 +isort==5.13.2 +mccabe==0.7.0 +mypy-extensions==1.0.0 +packaging==24.2 +pathspec==0.12.1 pep8-naming==0.13.2 -django-debug-toolbar==3.2.4 -djangorestframework==3.13.1 +platformdirs==4.3.6 +pycodestyle==2.9.1 +pyflakes==2.5.0 +pytz==2024.2 +six==1.16.0 +sqlparse==0.5.2 From 1b2655647e6a2597325b22e68a25242eea233e2b Mon Sep 17 00:00:00 2001 From: popeye88 Date: Mon, 25 Nov 2024 23:09:43 +0200 Subject: [PATCH 2/2] fix: added migrations --- ...all_genre_alter_movie_duration_and_more.py | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 cinema/migrations/0002_actor_cinemahall_genre_alter_movie_duration_and_more.py diff --git a/cinema/migrations/0002_actor_cinemahall_genre_alter_movie_duration_and_more.py b/cinema/migrations/0002_actor_cinemahall_genre_alter_movie_duration_and_more.py new file mode 100644 index 000000000..a36a9ad42 --- /dev/null +++ b/cinema/migrations/0002_actor_cinemahall_genre_alter_movie_duration_and_more.py @@ -0,0 +1,76 @@ +# Generated by Django 5.1.2 on 2024-11-25 21:09 + +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.PositiveIntegerField()), + ("seats_in_row", models.PositiveIntegerField()), + ], + ), + 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.AlterField( + model_name="movie", + name="duration", + field=models.PositiveIntegerField(), + ), + migrations.AddField( + model_name="movie", + name="actors", + 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"), + ), + ]