Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
CaCtUs1k committed Oct 10, 2023
1 parent cb65ce5 commit ce87ee6
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 29 deletions.
1 change: 0 additions & 1 deletion cinema/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class Migration(migrations.Migration):

initial = True

dependencies = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
),
),
]
24 changes: 15 additions & 9 deletions cinema/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
24 changes: 14 additions & 10 deletions cinema/urls.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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",
}
)

Expand All @@ -33,7 +35,9 @@
path("actors/", ActorList.as_view(), name="actor-list"),
path("actors/<int:pk>/", ActorDetail.as_view(), name="actor-detail"),
path("cinema_halls/", cinema_hall_list, name="cinema-hall-list"),
path("cinema_halls/<int:pk>/", cinema_hall_detail, name="cinema-hall-detail"),
path(
"cinema_halls/<int:pk>/", cinema_hall_detail, name="cinema-hall-detail"
),
]

app_name = "cinema"
17 changes: 10 additions & 7 deletions cinema/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -100,7 +103,7 @@ class CinemaHallViewSet(
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
mixins.ListModelMixin,
GenericViewSet
GenericViewSet,
):
queryset = CinemaHall.objects.all()
serializer_class = CinemaHallSerializer
Expand Down

0 comments on commit ce87ee6

Please sign in to comment.