-
Notifications
You must be signed in to change notification settings - Fork 720
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 1 commit
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.SlugRelatedField( | ||
source="cinema_hall", slug_field="capacity", read_only=True | ||
) | ||
|
||
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. Using |
||
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 |
---|---|---|
|
@@ -116,9 +116,13 @@ def test_patch_cinema_hall(self): | |
self.assertEqual(CinemaHall.objects.get(id=1).name, "Green") | ||
|
||
def test_delete_cinema_hall(self): | ||
print("Before DELETE request:", CinemaHall.objects.all()) | ||
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. Unnecessary print statement. Consider removing it to keep the test output clean. |
||
response = self.client.delete( | ||
"/api/cinema/cinema_halls/1/", | ||
) | ||
print("After DELETE request:", CinemaHall.objects.all()) # Друкуємо всі об'єкти | ||
print("Count of CinemaHall with id=1:", | ||
CinemaHall.objects.filter(id=1).count()) # Друкуємо кількість об'єктів з id=1 | ||
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. Unnecessary print statements. Consider removing them to keep the test output clean. |
||
db_cinema_halls_id_1 = CinemaHall.objects.filter(id=1) | ||
self.assertEqual(db_cinema_halls_id_1.count(), 0) | ||
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) | ||
|
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 |
---|---|---|
|
@@ -40,12 +40,14 @@ | |
"django.contrib.messages", | ||
"django.contrib.staticfiles", | ||
"rest_framework", | ||
"debug_toolbar", | ||
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 'debug_toolbar' is included in INSTALLED_APPS. Ensure it is removed or conditionally included for production environments. |
||
"cinema", | ||
"user", | ||
] | ||
|
||
MIDDLEWARE = [ | ||
"django.middleware.security.SecurityMiddleware", | ||
"debug_toolbar.middleware.DebugToolbarMiddleware", | ||
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 'DebugToolbarMiddleware' is included in MIDDLEWARE. Ensure it is removed or conditionally included for production environments. |
||
"django.contrib.sessions.middleware.SessionMiddleware", | ||
"django.middleware.common.CommonMiddleware", | ||
"django.middleware.csrf.CsrfViewMiddleware", | ||
|
@@ -131,3 +133,7 @@ | |
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field | ||
|
||
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" | ||
|
||
INTERNAL_IPS = [ | ||
"127.0.0.1", | ||
] |
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.