-
Notifications
You must be signed in to change notification settings - Fork 714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Solution #746
base: master
Are you sure you want to change the base?
Solution #746
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,85 @@ | ||
# write serializers here | ||
from rest_framework import serializers | ||
from cinema.models import Genre, Actor, CinemaHall, Movie, MovieSession | ||
|
||
|
||
class ActorSerializer(serializers.ModelSerializer): | ||
full_name = serializers.SerializerMethodField() | ||
|
||
class Meta: | ||
model = Actor | ||
fields = ("id", "first_name", "last_name", "full_name") | ||
|
||
def get_full_name(self, obj): | ||
return f"{obj.first_name} {obj.last_name}" | ||
|
||
|
||
class GenreSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Genre | ||
fields = ("id", "name") | ||
|
||
|
||
class BaseMovieSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Movie | ||
fields = ("id", "title", "description", "duration", "genres", "actors") | ||
|
||
|
||
class MovieListSerializer(BaseMovieSerializer): | ||
genres = serializers.SlugRelatedField( | ||
many=True, | ||
read_only=True, | ||
slug_field="name" | ||
) | ||
actors = serializers.SerializerMethodField() | ||
|
||
def get_actors(self, obj): | ||
return [ | ||
f"{actor.first_name} {actor.last_name}" | ||
for actor in obj.actors.all() | ||
] | ||
|
||
Comment on lines
+36
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method |
||
|
||
class MovieRetrieveSerializer(BaseMovieSerializer): | ||
genres = GenreSerializer(many=True) | ||
actors = ActorSerializer(many=True) | ||
|
||
|
||
class CinemaHallSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = CinemaHall | ||
fields = ("id", "name", "rows", "seats_in_row", "capacity") | ||
|
||
|
||
class MovieSessionSerializer(serializers.ModelSerializer): | ||
|
||
class Meta: | ||
model = MovieSession | ||
fields = ("id", "show_time", "movie", "cinema_hall") | ||
|
||
|
||
class MovieSessionListSerializer(MovieSessionSerializer): | ||
movie_title = serializers.SlugRelatedField( | ||
source="movie", slug_field="title", read_only=True | ||
) | ||
cinema_hall_name = serializers.SlugRelatedField( | ||
source="cinema_hall", slug_field="name", read_only=True | ||
) | ||
cinema_hall_capacity = serializers.ReadOnlyField( | ||
source="cinema_hall.capacity" | ||
) | ||
|
||
class Meta: | ||
model = MovieSession | ||
fields = ( | ||
"id", | ||
"show_time", | ||
"movie_title", | ||
"cinema_hall_name", | ||
"cinema_hall_capacity", | ||
) | ||
|
||
|
||
class MovieSessionRetrieveSerializer(MovieSessionSerializer): | ||
movie = MovieListSerializer() | ||
cinema_hall = CinemaHallSerializer() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,27 @@ | ||
# write urls here | ||
from django.urls import path, include | ||
from rest_framework.routers import DefaultRouter | ||
from .views import ( | ||
GenreViewSet, | ||
ActorViewSet, | ||
CinemaHallViewSet, | ||
MovieViewSet, | ||
MovieSessionViewSet, | ||
) | ||
|
||
router = DefaultRouter() | ||
router.register(r"genres", GenreViewSet, basename="genre") | ||
router.register("actors", ActorViewSet, basename="actor") | ||
router.register("cinema_halls", CinemaHallViewSet, basename="cinema_hall") | ||
router.register("movies", MovieViewSet, basename="movie") | ||
router.register( | ||
"movie_sessions", | ||
MovieSessionViewSet, | ||
basename="movie_session" | ||
) | ||
|
||
|
||
urlpatterns = [ | ||
path("", include(router.urls)), | ||
] | ||
|
||
app_name = "cinema" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,65 @@ | ||
# write views here | ||
from rest_framework import viewsets | ||
from cinema.models import Genre, Actor, CinemaHall, Movie, MovieSession | ||
from cinema.serializers import ( | ||
GenreSerializer, | ||
ActorSerializer, | ||
CinemaHallSerializer, | ||
MovieSessionSerializer, | ||
MovieSessionListSerializer, | ||
MovieListSerializer, | ||
MovieRetrieveSerializer, | ||
MovieSessionRetrieveSerializer, | ||
BaseMovieSerializer, | ||
) | ||
|
||
|
||
class GenreViewSet(viewsets.ModelViewSet): | ||
queryset = Genre.objects.all() | ||
serializer_class = GenreSerializer | ||
|
||
|
||
class ActorViewSet(viewsets.ModelViewSet): | ||
queryset = Actor.objects.all() | ||
serializer_class = ActorSerializer | ||
|
||
|
||
class CinemaHallViewSet(viewsets.ModelViewSet): | ||
queryset = CinemaHall.objects.all() | ||
serializer_class = CinemaHallSerializer | ||
|
||
|
||
class MovieViewSet(viewsets.ModelViewSet): | ||
queryset = Movie.objects.prefetch_related("genres", "actors") | ||
|
||
def get_serializer_class(self): | ||
if self.action == "list": | ||
return MovieListSerializer | ||
elif self.action == "retrieve": | ||
return MovieRetrieveSerializer | ||
return BaseMovieSerializer | ||
|
||
Comment on lines
+34
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method |
||
def get_queryset(self): | ||
queryset = self.queryset | ||
if self.action == "list": | ||
return queryset.prefetch_related("genres", "actors") | ||
return queryset | ||
|
||
Comment on lines
+41
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method |
||
|
||
class MovieSessionViewSet(viewsets.ModelViewSet): | ||
queryset = MovieSession.objects.all().select_related( | ||
"movie", | ||
"cinema_hall" | ||
) | ||
|
||
def get_serializer_class(self): | ||
if self.action == "list": | ||
return MovieSessionListSerializer | ||
elif self.action == "retrieve": | ||
return MovieSessionRetrieveSerializer | ||
return MovieSessionSerializer | ||
|
||
Comment on lines
+54
to
+59
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method |
||
def get_queryset(self): | ||
queryset = self.queryset | ||
if self.action == "list": | ||
return queryset.select_related("movie", "cinema_hall") | ||
return queryset |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,14 @@ | ||
from django.conf import settings | ||
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")), | ||
] | ||
|
||
if settings.DEBUG: | ||
import debug_toolbar | ||
urlpatterns += [ | ||
path("__debug__/", include(debug_toolbar.urls)), | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method
get_full_name
is missing type annotations. According to the checklist, type annotations should be added to all class methods to improve code readability and maintainability.