Skip to content

Commit

Permalink
imprement solution serializers folowing readme.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Mxbely committed Dec 7, 2024
1 parent 1f225be commit 7b7b8d0
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
.env
.DS_Store
venv/
.venv/
.pytest_cache/
**__pycache__/
db.sqlite3
3 changes: 3 additions & 0 deletions cinema/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class Actor(models.Model):

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

def get_full_name(self):
return f"{self.first_name} {self.last_name}"


class Movie(models.Model):
Expand Down
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 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"]
17 changes: 16 additions & 1 deletion cinema/urls.py
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)),
]
86 changes: 85 additions & 1 deletion cinema/views.py
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
4 changes: 3 additions & 1 deletion cinema_service/urls.py
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")),
]

0 comments on commit 7b7b8d0

Please sign in to comment.