diff --git a/cinema/admin.py b/cinema/admin.py index 0b288331..38bb0138 100644 --- a/cinema/admin.py +++ b/cinema/admin.py @@ -10,10 +10,20 @@ Ticket, ) + +class TicketInLine(admin.TabularInline): + model = Ticket + extra = 1 + + +@admin.register(Order) +class OrderAdmin(admin.ModelAdmin): + inlines = (TicketInLine,) + + admin.site.register(CinemaHall) admin.site.register(Genre) admin.site.register(Actor) admin.site.register(Movie) admin.site.register(MovieSession) -admin.site.register(Order) admin.site.register(Ticket) diff --git a/cinema/models.py b/cinema/models.py index c42d2a3d..3fe16827 100644 --- a/cinema/models.py +++ b/cinema/models.py @@ -27,6 +27,10 @@ class Actor(models.Model): first_name = models.CharField(max_length=255) last_name = models.CharField(max_length=255) + @property + def full_name(self) -> str: + return f"{self.first_name} {self.last_name}" + def __str__(self): return self.first_name + " " + self.last_name diff --git a/cinema/serializers.py b/cinema/serializers.py index 612ca7e2..3f1ca2ef 100644 --- a/cinema/serializers.py +++ b/cinema/serializers.py @@ -1 +1,107 @@ -# write serializers here +from rest_framework import serializers + +from cinema.models import ( + Movie, + MovieSession, + Genre, + CinemaHall, + Actor +) + + +class ActorSerializer(serializers.ModelSerializer): + + class Meta: + model = Actor + fields = ("id", "first_name", "last_name", "full_name") + read_only_fields = ("id",) + + +class CinemaHallSerializer(serializers.ModelSerializer): + + class Meta: + model = CinemaHall + fields = ("id", "name", "rows", "seats_in_row", "capacity") + read_only_fields = ("id",) + + +class GenreSerializer(serializers.ModelSerializer): + + class Meta: + model = Genre + fields = ("id", "name") + read_only_fields = ("id",) + + +class MovieSerializer(serializers.ModelSerializer): + + class Meta: + model = Movie + fields = ("id", "title", "description", "duration", "genres", "actors") + read_only_fields = ("id",) + + +class MovieListSerializer(MovieSerializer): + + genres = serializers.SlugRelatedField( + many=True, + read_only=True, + slug_field="name" + ) + actors = serializers.SlugRelatedField( + many=True, + read_only=True, + slug_field="full_name" + ) + + +class MovieRetrieveSerializer(MovieSerializer): + + genres = GenreSerializer(many=True) + actors = ActorSerializer(many=True) + + +class MovieSessionSerializer(serializers.ModelSerializer): + class Meta: + model = MovieSession + fields = ("id", "show_time", "movie", "cinema_hall") + + +class MovieSessionListSerializer(MovieSessionSerializer): + + movie_title = serializers.CharField( + source="movie.title", + read_only=True + ) + cinema_hall_name = serializers.CharField( + source="cinema_hall.name", + read_only=True + ) + cinema_hall_capacity = serializers.IntegerField( + source="cinema_hall.capacity", + read_only=True + ) + + class Meta: + model = MovieSession + fields = ( + "id", + "show_time", + "movie_title", + "cinema_hall_name", + "cinema_hall_capacity" + ) + read_only_fields = ("id",) + + +class MovieSessionRetrieveSerializer( + MovieSessionSerializer +): + + cinema_hall = CinemaHallSerializer(read_only=True) + movie = MovieListSerializer() + + class Meta: + model = MovieSession + fields = ("id", "show_time", "movie", "cinema_hall") + read_only_fields = ("id",) diff --git a/cinema/urls.py b/cinema/urls.py index 420f8e8c..fd56de1f 100644 --- a/cinema/urls.py +++ b/cinema/urls.py @@ -1 +1,24 @@ -# write urls here +from django.urls import path, include +from rest_framework import routers + +from cinema.views import ( + MovieViewSet, + MovieSessionViewSet, + GenreViewSet, + ActorViewSet, + CinemaHallViewSet +) + + +router = routers.DefaultRouter() +router.register("actors", ActorViewSet) +router.register("genres", GenreViewSet) +router.register("cinema_halls", CinemaHallViewSet) +router.register("movies", MovieViewSet) +router.register("movie_sessions", MovieSessionViewSet) + +urlpatterns = [ + path("", include(router.urls)) +] + +app_name = "cinema" diff --git a/cinema/views.py b/cinema/views.py index ae87bfde..f633046b 100644 --- a/cinema/views.py +++ b/cinema/views.py @@ -1 +1,57 @@ -# write views here +from rest_framework import viewsets + +from cinema.serializers import ( + MovieSerializer, + MovieListSerializer, + MovieRetrieveSerializer, + MovieSessionSerializer, + MovieSessionListSerializer, + MovieSessionRetrieveSerializer, + ActorSerializer, + GenreSerializer, + CinemaHallSerializer +) +from cinema.models import ( + Movie, + MovieSession, + Actor, + Genre, + CinemaHall +) + + +class MovieViewSet(viewsets.ModelViewSet): + queryset = Movie.objects.prefetch_related("genres", "actors") + + def get_serializer_class(self): + if self.action == "list": + return MovieListSerializer + if self.action == "retrieve": + return MovieRetrieveSerializer + return MovieSerializer + + +class MovieSessionViewSet(viewsets.ModelViewSet): + queryset = MovieSession.objects.select_related("cinema_hall", "movie") + + def get_serializer_class(self): + if self.action == "list": + return MovieSessionListSerializer + if self.action == "retrieve": + return MovieSessionRetrieveSerializer + return MovieSessionSerializer + + +class ActorViewSet(viewsets.ModelViewSet): + queryset = Actor.objects.all() + serializer_class = ActorSerializer + + +class GenreViewSet(viewsets.ModelViewSet): + queryset = Genre.objects.all() + serializer_class = GenreSerializer + + +class CinemaHallViewSet(viewsets.ModelViewSet): + queryset = CinemaHall.objects.all() + serializer_class = CinemaHallSerializer diff --git a/cinema_service/urls.py b/cinema_service/urls.py index 083932c6..e126f35c 100644 --- a/cinema_service/urls.py +++ b/cinema_service/urls.py @@ -1,6 +1,7 @@ from django.contrib import admin -from django.urls import path +from django.urls import path, include urlpatterns = [ path("admin/", admin.site.urls), + path("api/cinema/", include("cinema.urls", namespace="cinema")) ] diff --git a/db.sqlite3 b/db.sqlite3 new file mode 100644 index 00000000..26c1c3f3 Binary files /dev/null and b/db.sqlite3 differ diff --git a/requirements.txt b/requirements.txt index 108897e9..646a2aae 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,12 @@ +asgiref==3.8.1 Django==4.0.4 djangorestframework==3.13.1 flake8==5.0.4 flake8-quotes==3.3.1 flake8-variables-names==0.0.5 -pep8-naming==0.13.2 \ No newline at end of file +mccabe==0.7.0 +pep8-naming==0.13.2 +pycodestyle==2.9.1 +pyflakes==2.5.0 +pytz==2024.2 +sqlparse==0.5.2