-
Notifications
You must be signed in to change notification settings - Fork 720
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
172 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,86 @@ | ||
# write serializers here | ||
from rest_framework import serializers | ||
|
||
from cinema.models import Genre, CinemaHall, Actor, Movie, MovieSession | ||
|
||
|
||
class GenreSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Genre | ||
fields = ("id", "name") | ||
|
||
|
||
class CinemaHallSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = CinemaHall | ||
fields = ("id", "name", "rows", "seats_in_row", "capacity") | ||
|
||
|
||
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.StringRelatedField(many=True, read_only=True) | ||
actors = serializers.StringRelatedField(many=True, read_only=True) | ||
|
||
|
||
class MovieRetrieveSerializer(MovieSerializer): | ||
genres = GenreSerializer(many=True, read_only=True) | ||
actors = ActorSerializer(many=True, read_only=True) | ||
|
||
|
||
class MovieSessionSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = MovieSession | ||
fields = "__all__" | ||
|
||
|
||
class MovieSessionListSerializer(serializers.ModelSerializer): | ||
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" | ||
) | ||
|
||
|
||
class MovieSessionRetrieveSerializer(MovieSessionSerializer): | ||
movie = MovieListSerializer() | ||
cinema_hall = CinemaHallSerializer() | ||
|
||
class Meta: | ||
model = MovieSession | ||
fields = ( | ||
"id", | ||
"show_time", | ||
"movie", | ||
"cinema_hall" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,23 @@ | ||
# write urls here | ||
from django.urls import path, include | ||
from rest_framework import routers | ||
|
||
from cinema.views import ( | ||
GenreViewSet, | ||
ActorViewSet, | ||
MovieViewSet, | ||
MovieSessionViewSet, | ||
CinemaHallViewSet, | ||
) | ||
|
||
app_name = "cinema" | ||
|
||
router = routers.DefaultRouter() | ||
router.register("genres", GenreViewSet) | ||
router.register("actors", ActorViewSet) | ||
router.register("movies", MovieViewSet) | ||
router.register("movie_sessions", MovieSessionViewSet) | ||
router.register("cinema_halls", CinemaHallViewSet) | ||
|
||
urlpatterns = [ | ||
path("api/cinema/", include(router.urls)), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,56 @@ | ||
# write views here | ||
from rest_framework import viewsets | ||
from cinema.models import ( | ||
Genre, | ||
Actor, | ||
Movie, | ||
MovieSession, | ||
CinemaHall, | ||
) | ||
from cinema.serializers import ( | ||
GenreSerializer, | ||
ActorSerializer, | ||
MovieSerializer, | ||
MovieListSerializer, | ||
MovieRetrieveSerializer, | ||
MovieSessionSerializer, | ||
MovieSessionListSerializer, | ||
MovieSessionRetrieveSerializer, | ||
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() | ||
|
||
def get_serializer_class(self): | ||
if self.action == "list": | ||
return MovieListSerializer | ||
elif self.action == "retrieve": | ||
return MovieRetrieveSerializer | ||
return MovieSerializer | ||
|
||
|
||
class MovieSessionViewSet(viewsets.ModelViewSet): | ||
queryset = MovieSession.objects.all() | ||
|
||
def get_serializer_class(self): | ||
if self.action == "list": | ||
return MovieSessionListSerializer | ||
elif self.action == "retrieve": | ||
return MovieSessionRetrieveSerializer | ||
return MovieSessionSerializer | ||
|
||
|
||
class CinemaHallViewSet(viewsets.ModelViewSet): | ||
queryset = CinemaHall.objects.all() | ||
serializer_class = CinemaHallSerializer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("", include("cinema.urls", namespace="cinema")), | ||
] |