-
Notifications
You must be signed in to change notification settings - Fork 722
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
237 additions
and
49 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
cinema/migrations/0002_actor_cinemahall_genre_alter_movie_duration_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Generated by Django 4.1 on 2024-10-29 12:20 | ||
|
||
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=100)), | ||
('last_name', models.CharField(max_length=100)), | ||
], | ||
), | ||
migrations.CreateModel( | ||
name='CinemaHall', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('name', models.CharField(max_length=100)), | ||
('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=100, unique=True)), | ||
], | ||
), | ||
migrations.AlterField( | ||
model_name='movie', | ||
name='duration', | ||
field=models.DurationField(), | ||
), | ||
migrations.AlterField( | ||
model_name='movie', | ||
name='title', | ||
field=models.CharField(max_length=200), | ||
), | ||
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'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,32 @@ | ||
from django.db import models | ||
|
||
class Actor(models.Model): | ||
first_name = models.CharField(max_length=100) | ||
last_name = models.CharField(max_length=100) | ||
|
||
def __str__(self): | ||
return f"{self.first_name} {self.last_name}" | ||
|
||
class Genre(models.Model): | ||
name = models.CharField(max_length=100, unique=True) | ||
|
||
def __str__(self): | ||
return self.name | ||
|
||
class CinemaHall(models.Model): | ||
name = models.CharField(max_length=100) | ||
rows = models.IntegerField() | ||
seats_in_row = models.IntegerField() | ||
|
||
def __str__(self): | ||
return self.name | ||
|
||
class Movie(models.Model): | ||
title = models.CharField(max_length=255) | ||
title = models.CharField(max_length=200) | ||
description = models.TextField() | ||
duration = models.IntegerField() | ||
actors = models.ManyToManyField(Actor) | ||
genres = models.ManyToManyField(Genre) | ||
duration = models.DurationField() | ||
|
||
def __str__(self): | ||
return self.title |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,22 @@ | ||
from rest_framework import serializers | ||
from .models import Actor, Genre, CinemaHall, Movie | ||
|
||
from cinema.models import Movie | ||
class ActorSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Actor | ||
fields = ['id', 'first_name', 'last_name'] | ||
|
||
class GenreSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Genre | ||
fields = ['id', 'name'] | ||
|
||
class MovieSerializer(serializers.Serializer): | ||
id = serializers.IntegerField(read_only=True) | ||
title = serializers.CharField(max_length=255) | ||
description = serializers.CharField() | ||
duration = serializers.IntegerField() | ||
class CinemaHallSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = CinemaHall | ||
fields = ['id', 'name', 'rows', 'seats_in_row'] | ||
|
||
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) | ||
|
||
instance.save() | ||
|
||
return instance | ||
class MovieSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Movie | ||
fields = ['id', 'title', 'description', 'actors', 'genres', 'duration'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
from django.urls import path | ||
from django.urls import path, include | ||
from rest_framework.routers import DefaultRouter | ||
from .views import GenreAPIView, GenreDetailAPIView, ActorAPIView, ActorDetailAPIView, CinemaHallViewSet, MovieViewSet | ||
|
||
from cinema.views import movie_list, movie_detail | ||
router = DefaultRouter() | ||
router.register(r'cinema/cinema_halls', CinemaHallViewSet, basename='cinemahall') | ||
router.register(r'cinema/movies', MovieViewSet, basename='movie') | ||
|
||
urlpatterns = [ | ||
path("movies/", movie_list, name="movie-list"), | ||
path("movies/<int:pk>/", movie_detail, name="movie-detail"), | ||
path('cinema/genres/', GenreAPIView.as_view(), name='genre-list'), | ||
path('cinema/genres/<int:pk>/', GenreDetailAPIView.as_view(), name='genre-detail'), | ||
path('cinema/actors/', ActorAPIView.as_view(), name='actor-list'), | ||
path('cinema/actors/<int:pk>/', ActorDetailAPIView.as_view(), name='actor-detail'), | ||
path('', include(router.urls)), | ||
] | ||
|
||
app_name = "cinema" | ||
app_name = 'cinema' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,149 @@ | ||
from rest_framework.decorators import api_view | ||
from rest_framework.views import APIView | ||
from rest_framework.generics import GenericAPIView | ||
from rest_framework.viewsets import ModelViewSet, GenericViewSet | ||
from rest_framework.response import Response | ||
from rest_framework import status | ||
from .models import Actor, Genre, CinemaHall, Movie | ||
from .serializers import ActorSerializer, GenreSerializer, CinemaHallSerializer, MovieSerializer | ||
|
||
from django.shortcuts import get_object_or_404 | ||
# Genre APIView | ||
class GenreAPIView(APIView): | ||
def get(self, request): | ||
genres = Genre.objects.all() | ||
serializer = GenreSerializer(genres, many=True) | ||
return Response(serializer.data) | ||
|
||
from cinema.models import Movie | ||
from cinema.serializers import MovieSerializer | ||
def post(self, request): | ||
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 GenreDetailAPIView(APIView): | ||
def get(self, request, pk): | ||
try: | ||
genre = Genre.objects.get(pk=pk) | ||
serializer = GenreSerializer(genre) | ||
return Response(serializer.data) | ||
except Genre.DoesNotExist: | ||
return Response(status=status.HTTP_404_NOT_FOUND) | ||
|
||
def put(self, request, pk): | ||
genre = Genre.objects.get(pk=pk) | ||
serializer = GenreSerializer(genre, data=request.data) | ||
if serializer.is_valid(): | ||
serializer.save() | ||
return Response(serializer.data) | ||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
def patch(self, request, pk): | ||
genre = Genre.objects.get(pk=pk) | ||
serializer = GenreSerializer(genre, data=request.data, partial=True) | ||
if serializer.is_valid(): | ||
serializer.save() | ||
return Response(serializer.data) | ||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
def delete(self, request, pk): | ||
genre = Genre.objects.get(pk=pk) | ||
genre.delete() | ||
return Response(status=status.HTTP_204_NO_CONTENT) | ||
|
||
@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) | ||
# Actor GenericAPIView | ||
class ActorAPIView(GenericAPIView): | ||
queryset = Actor.objects.all() | ||
serializer_class = ActorSerializer | ||
|
||
if request.method == "POST": | ||
serializer = MovieSerializer(data=request.data) | ||
def get(self, request): | ||
actors = self.get_queryset() | ||
serializer = self.get_serializer(actors, many=True) | ||
return Response(serializer.data) | ||
|
||
def post(self, request): | ||
serializer = self.get_serializer(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) | ||
|
||
# Actor detail view | ||
class ActorDetailAPIView(GenericAPIView): | ||
queryset = Actor.objects.all() | ||
serializer_class = ActorSerializer | ||
|
||
def get(self, request, pk): | ||
actor = self.get_object() | ||
serializer = self.get_serializer(actor) | ||
return Response(serializer.data) | ||
|
||
def put(self, request, pk): | ||
actor = self.get_object() | ||
serializer = self.get_serializer(actor, data=request.data) | ||
if serializer.is_valid(): | ||
serializer.save() | ||
return Response(serializer.data) | ||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
def patch(self, request, pk): | ||
actor = self.get_object() | ||
serializer = self.get_serializer(actor, data=request.data, partial=True) | ||
if serializer.is_valid(): | ||
serializer.save() | ||
return Response(serializer.data) | ||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
def delete(self, request, pk): | ||
actor = self.get_object() | ||
actor.delete() | ||
return Response(status=status.HTTP_204_NO_CONTENT) | ||
|
||
# CinemaHall GenericViewSet | ||
from rest_framework import viewsets | ||
|
||
class CinemaHallViewSet(viewsets.GenericViewSet): | ||
queryset = CinemaHall.objects.all() | ||
serializer_class = CinemaHallSerializer | ||
|
||
@api_view(["GET", "PUT", "DELETE"]) | ||
def movie_detail(request, pk): | ||
movie = get_object_or_404(Movie, pk=pk) | ||
def list(self, request): | ||
cinema_halls = self.get_queryset() | ||
serializer = self.get_serializer(cinema_halls, many=True) | ||
return Response(serializer.data) | ||
|
||
if request.method == "GET": | ||
serializer = MovieSerializer(movie) | ||
return Response(serializer.data, status=status.HTTP_200_OK) | ||
def create(self, request): | ||
serializer = self.get_serializer(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 retrieve(self, request, pk): | ||
cinema_hall = self.get_object() | ||
serializer = self.get_serializer(cinema_hall) | ||
return Response(serializer.data) | ||
|
||
if request.method == "PUT": | ||
serializer = MovieSerializer(movie, data=request.data) | ||
def update(self, request, pk): | ||
cinema_hall = self.get_object() | ||
serializer = self.get_serializer(cinema_hall, data=request.data) | ||
if serializer.is_valid(): | ||
serializer.save() | ||
return Response(serializer.data, status=status.HTTP_200_OK) | ||
return Response(serializer.data) | ||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
def partial_update(self, request, pk): | ||
cinema_hall = self.get_object() | ||
serializer = self.get_serializer(cinema_hall, data=request.data, partial=True) | ||
if serializer.is_valid(): | ||
serializer.save() | ||
return Response(serializer.data) | ||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
if request.method == "DELETE": | ||
movie.delete() | ||
def destroy(self, request, pk): | ||
cinema_hall = self.get_object() | ||
cinema_hall.delete() | ||
return Response(status=status.HTTP_204_NO_CONTENT) | ||
|
||
# Movie ModelViewSet | ||
class MovieViewSet(viewsets.ModelViewSet): | ||
queryset = Movie.objects.all() | ||
serializer_class = MovieSerializer |