-
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.
imprement solution serializers folowing readme.md
- Loading branch information
Showing
6 changed files
with
194 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,7 @@ | |
.env | ||
.DS_Store | ||
venv/ | ||
.venv/ | ||
.pytest_cache/ | ||
**__pycache__/ | ||
db.sqlite3 |
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,85 @@ | ||
# write serializers here | ||
from rest_framework import serializers | ||
from cinema.models import CinemaHall, Genre, Actor, Movie, MovieSession, Order, Ticket | ||
|
||
|
||
class CinemaHallSerializer(serializers.ModelSerializer): | ||
capacity = serializers.IntegerField(read_only=True) | ||
|
||
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): | ||
full_name = serializers.SerializerMethodField(read_only=True) | ||
|
||
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 ActorListSerializer(ActorSerializer): | ||
|
||
|
||
class Meta(ActorSerializer.Meta): | ||
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( | ||
slug_field="name", | ||
many=True, | ||
read_only=True, | ||
) | ||
actors = serializers.StringRelatedField(many=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 = ["id", "show_time", "cinema_hall", "movie"] | ||
|
||
|
||
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(MovieSessionSerializer.Meta): | ||
fields = ["id", "show_time", "movie_title", "cinema_hall_name", "cinema_hall_capacity"] | ||
|
||
|
||
class MovieSessionRetrieveSerializer(MovieSessionSerializer): | ||
cinema_hall = CinemaHallSerializer(read_only=True) | ||
movie = MovieListSerializer(read_only=True) | ||
|
||
|
||
class OrderSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Order | ||
fields = ["id", "created_at", "user", "tickets"] | ||
|
||
|
||
class TicketSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Ticket | ||
fields = ["id", "movie_session", "order", "row", "seat"] |
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,16 @@ | ||
# write urls here | ||
from django.urls import path, include | ||
from cinema import views | ||
from rest_framework import routers | ||
|
||
router = routers.DefaultRouter() | ||
router.register("cinema_halls", views.CinemaHallSet, basename="cinema_halls") | ||
router.register("genres", views.GenreSet, basename="genres") | ||
router.register("actors", views.ActorSet, basename="actors") | ||
router.register("movies", views.MovieSet, basename="movies") | ||
router.register("movie_sessions", views.MovieSessionSet, basename="movie_sessions") | ||
router.register("orders", views.OrderSet, basename="orders") | ||
router.register("tickets", views.TicketSet, basename="tickets") | ||
|
||
urlpatterns = [ | ||
path("", 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,85 @@ | ||
# write views here | ||
from rest_framework import viewsets | ||
from cinema.models import ( | ||
CinemaHall, | ||
Genre, | ||
Actor, | ||
Movie, | ||
MovieSession, | ||
Order, | ||
Ticket, | ||
) | ||
from cinema.serializers import ( | ||
ActorSerializer, | ||
ActorListSerializer, | ||
CinemaHallSerializer, | ||
GenreSerializer, | ||
MovieSerializer, | ||
MovieListSerializer, | ||
MovieRetrieveSerializer, | ||
MovieSessionSerializer, | ||
MovieSessionListSerializer, | ||
MovieSessionRetrieveSerializer, | ||
OrderSerializer, | ||
TicketSerializer, | ||
) | ||
|
||
|
||
class CinemaHallSet(viewsets.ModelViewSet): | ||
queryset = CinemaHall.objects.all() | ||
serializer_class = CinemaHallSerializer | ||
|
||
|
||
class GenreSet(viewsets.ModelViewSet): | ||
queryset = Genre.objects.all() | ||
serializer_class = GenreSerializer | ||
|
||
|
||
class ActorSet(viewsets.ModelViewSet): | ||
queryset = Actor.objects.all() | ||
serializer_class = ActorSerializer | ||
|
||
def get_serializer_class(self): | ||
if self.action == "list": | ||
return ActorListSerializer | ||
return ActorSerializer | ||
|
||
|
||
class MovieSet(viewsets.ModelViewSet): | ||
|
||
def get_queryset(self): | ||
queryset = Movie.objects.all() | ||
if self.action in ["list", "retrieve"]: | ||
queryset = queryset.prefetch_related("genres", "actors") | ||
return queryset | ||
|
||
def get_serializer_class(self): | ||
if self.action == "list": | ||
return MovieListSerializer | ||
if self.action == "retrieve": | ||
return MovieRetrieveSerializer | ||
return MovieSerializer | ||
|
||
|
||
class MovieSessionSet(viewsets.ModelViewSet): | ||
def get_queryset(self): | ||
queryset = MovieSession.objects.all() | ||
if self.action in ["list", "retrieve"]: | ||
queryset = queryset.select_related("movie", "cinema_hall") | ||
return queryset | ||
|
||
def get_serializer_class(self): | ||
if self.action == "list": | ||
return MovieSessionListSerializer | ||
if self.action == "retrieve": | ||
return MovieSessionRetrieveSerializer | ||
return MovieSessionSerializer | ||
|
||
|
||
class OrderSet(viewsets.ModelViewSet): | ||
queryset = Order.objects.all() | ||
serializer_class = OrderSerializer | ||
|
||
|
||
class TicketSet(viewsets.ModelViewSet): | ||
queryset = Ticket.objects.all() | ||
serializer_class = TicketSerializer |
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,8 @@ | ||
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")), | ||
] |