Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 85 additions & 1 deletion cinema/serializers.py
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}"

Comment on lines +12 to +13

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.


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method get_actors is missing type annotations. Adding type annotations will enhance code readability and maintainability, as required by the checklist.


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
)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using SlugRelatedField for cinema_hall_capacity might not be appropriate if capacity is not a unique field. Consider using a different serializer field, such as ReadOnlyField, if capacity is not unique.

class Meta:
model = MovieSession
fields = (
"id",
"show_time",
"movie_title",
"cinema_hall_name",
"cinema_hall_capacity",
)


class MovieSessionRetrieveSerializer(MovieSessionSerializer):
movie = MovieListSerializer()
cinema_hall = CinemaHallSerializer()
4 changes: 4 additions & 0 deletions cinema/tests/test_cinema_hall_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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)
Expand Down
28 changes: 27 additions & 1 deletion cinema/urls.py
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"
66 changes: 65 additions & 1 deletion cinema/views.py
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method get_serializer_class is missing type annotations. According to the checklist, type annotations should be added to all class methods to improve code readability and maintainability.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method get_queryset is missing type annotations. Adding type annotations will enhance code readability and maintainability, as required by the checklist.


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method get_serializer_class in MovieSessionViewSet is missing type annotations. Ensure to add type annotations to all class methods as per the checklist requirements.

def get_queryset(self):
queryset = self.queryset
if self.action == "list":
return queryset.select_related("movie", "cinema_hall")
return queryset
6 changes: 6 additions & 0 deletions cinema_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@
"django.contrib.messages",
"django.contrib.staticfiles",
"rest_framework",
"debug_toolbar",

Choose a reason for hiding this comment

The 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",

Choose a reason for hiding this comment

The 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",
Expand Down Expand Up @@ -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",
]
10 changes: 9 additions & 1 deletion cinema_service/urls.py
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)),
]
Loading