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 #726

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
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
4 changes: 4 additions & 0 deletions cinema/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
return f"{self.first_name} {self.last_name}"

def __str__(self):
return self.first_name + " " + self.last_name

Expand Down
84 changes: 83 additions & 1 deletion cinema/serializers.py
Original file line number Diff line number Diff line change
@@ -1 +1,83 @@
# write serializers here
from rest_framework import serializers
from cinema.models import CinemaHall, Genre, Actor, Movie, MovieSession


class CinemaHallSerializer(serializers.ModelSerializer):
class Meta:
model = CinemaHall
fields = ("id", "name", "rows", "seats_in_row", "capacity")


class GenreSerializer(serializers.ModelSerializer):
class Meta:
model = Genre
fields = ("id", "name")


class ActorSerializer(serializers.ModelSerializer):
class Meta:
model = Actor
fields = ("id", "first_name", "last_name", "full_name")


class MovieSerializer(serializers.ModelSerializer):
class Meta:
model = Movie
fields = ("id", "title", "description", "duration", "genres", "actors")


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 MovieDetailSerializer(MovieSerializer):
genres = GenreSerializer(many=True, read_only=True)
actors = ActorSerializer(many=True, read_only=True)


class MovieSessionSerializer(serializers.ModelSerializer):
movie = serializers.PrimaryKeyRelatedField(queryset=Movie.objects.all())
cinema_hall = (serializers.
PrimaryKeyRelatedField
(queryset=CinemaHall.objects.all()))

class Meta:
model = MovieSession
fields = ("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"
)

Comment on lines +74 to +78

Choose a reason for hiding this comment

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

Consider verifying whether the exclusion of the id field from the MovieSessionListSerializer is intentional. If you need the id field for identification purposes, you should include it in the fields list.


class MovieSessionDetailSerializer(MovieSessionSerializer):
movie = MovieListSerializer(many=False, read_only=True)
cinema_hall = CinemaHallSerializer(many=False, read_only=True)
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 import routers

from cinema.views import (
CinemaHallViewSet,
GenreViewSet,
ActorViewSet,
MovieViewSet,
MovieSessionViewSet
)

router = routers.DefaultRouter()
router.register("cinema_halls", CinemaHallViewSet)
router.register("genres", GenreViewSet)
router.register("actors", ActorViewSet)
router.register("movies", MovieViewSet, basename="movie")
router.register(
"movie_sessions",
MovieSessionViewSet,
basename="movie_session"
)

urlpatterns = [
path("", include(router.urls))
]

app_name = "cinema"
61 changes: 60 additions & 1 deletion cinema/views.py
Original file line number Diff line number Diff line change
@@ -1 +1,60 @@
# write views here
from rest_framework import viewsets
from cinema.models import CinemaHall, Genre, Actor, Movie, MovieSession
from cinema.serializers import (
CinemaHallSerializer, GenreSerializer,
ActorSerializer, MovieSerializer,
MovieSessionSerializer, MovieListSerializer,
MovieDetailSerializer, MovieSessionListSerializer,
MovieSessionDetailSerializer
)


class CinemaHallViewSet(viewsets.ModelViewSet):
queryset = CinemaHall.objects.all()
serializer_class = CinemaHallSerializer


class GenreViewSet(viewsets.ModelViewSet):
queryset = Genre.objects.all()
serializer_class = GenreSerializer


class ActorViewSet(viewsets.ModelViewSet):
queryset = Actor.objects.all()
serializer_class = ActorSerializer


class MovieViewSet(viewsets.ModelViewSet):
queryset = Movie.objects.all()
serializer_class = MovieSerializer

def get_queryset(self):
queryset = self.queryset
if self.action in ("list", "retrieve"):
return queryset.prefetch_related("genres", "actors")
return queryset

def get_serializer_class(self):
if self.action == "list":
return MovieListSerializer
if self.action == "retrieve":
return MovieDetailSerializer
return MovieSerializer


class MovieSessionViewSet(viewsets.ModelViewSet):
queryset = MovieSession.objects.all()
serializer_class = MovieSessionSerializer

def get_queryset(self):
queryset = self.queryset
if self.action == "list":
return queryset.select_related("movie", "cinema_hall")
return queryset

def get_serializer_class(self):
if self.action == "list":
return MovieSessionListSerializer
if self.action == "retrieve":
return MovieSessionDetailSerializer
return MovieSessionSerializer
2 changes: 1 addition & 1 deletion cinema_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@

USE_I18N = True

USE_TZ = True
USE_TZ = False

Choose a reason for hiding this comment

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

The USE_TZ setting is set to False. It's recommended to set USE_TZ = True to enable timezone-aware datetimes, which helps prevent potential issues with time zone handling.



# Static files (CSS, JavaScript, Images)
Expand Down
3 changes: 2 additions & 1 deletion cinema_service/urls.py
Original file line number Diff line number Diff line change
@@ -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")),
]
Loading