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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 11 additions & 1 deletion cinema/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,20 @@
Ticket,
)


class TicketInLine(admin.TabularInline):
model = Ticket
extra = 1


@admin.register(Order)
class OrderAdmin(admin.ModelAdmin):
inlines = (TicketInLine,)


admin.site.register(CinemaHall)
admin.site.register(Genre)
admin.site.register(Actor)
admin.site.register(Movie)
admin.site.register(MovieSession)
admin.site.register(Order)
admin.site.register(Ticket)
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) -> str:
return f"{self.first_name} {self.last_name}"

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

Expand Down
108 changes: 107 additions & 1 deletion cinema/serializers.py
Original file line number Diff line number Diff line change
@@ -1 +1,107 @@
# write serializers here
from rest_framework import serializers

from cinema.models import (
Movie,
MovieSession,
Genre,
CinemaHall,
Actor
)


class ActorSerializer(serializers.ModelSerializer):

class Meta:
model = Actor
fields = ("id", "first_name", "last_name", "full_name")
read_only_fields = ("id",)


class CinemaHallSerializer(serializers.ModelSerializer):

class Meta:
model = CinemaHall
fields = ("id", "name", "rows", "seats_in_row", "capacity")
read_only_fields = ("id",)


class GenreSerializer(serializers.ModelSerializer):

class Meta:
model = Genre
fields = ("id", "name")
read_only_fields = ("id",)


class MovieSerializer(serializers.ModelSerializer):

class Meta:
model = Movie
fields = ("id", "title", "description", "duration", "genres", "actors")
read_only_fields = ("id",)


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 MovieRetrieveSerializer(MovieSerializer):

genres = GenreSerializer(many=True)
actors = ActorSerializer(many=True)


class MovieSessionSerializer(serializers.ModelSerializer):
class Meta:
model = MovieSession
fields = ("id", "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"
)
read_only_fields = ("id",)


class MovieSessionRetrieveSerializer(
MovieSessionSerializer
):

cinema_hall = CinemaHallSerializer(read_only=True)
movie = MovieListSerializer()

class Meta:
model = MovieSession
fields = ("id", "show_time", "movie", "cinema_hall")
read_only_fields = ("id",)
25 changes: 24 additions & 1 deletion cinema/urls.py
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
# write urls here
from django.urls import path, include
from rest_framework import routers

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


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

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

Choose a reason for hiding this comment

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

urlpatterns = router.urls


app_name = "cinema"
58 changes: 57 additions & 1 deletion cinema/views.py
Original file line number Diff line number Diff line change
@@ -1 +1,57 @@
# write views here
from rest_framework import viewsets

from cinema.serializers import (
MovieSerializer,
MovieListSerializer,
MovieRetrieveSerializer,
MovieSessionSerializer,
MovieSessionListSerializer,
MovieSessionRetrieveSerializer,
ActorSerializer,
GenreSerializer,
CinemaHallSerializer
)
from cinema.models import (
Movie,
MovieSession,
Actor,
Genre,
CinemaHall
)


class MovieViewSet(viewsets.ModelViewSet):
queryset = Movie.objects.prefetch_related("genres", "actors")

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


class MovieSessionViewSet(viewsets.ModelViewSet):
queryset = MovieSession.objects.select_related("cinema_hall", "movie")

def get_serializer_class(self):
if self.action == "list":
return MovieSessionListSerializer
if self.action == "retrieve":
return MovieSessionRetrieveSerializer
return MovieSessionSerializer


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


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


class CinemaHallViewSet(viewsets.ModelViewSet):
queryset = CinemaHall.objects.all()
serializer_class = CinemaHallSerializer
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"))
]
Binary file added db.sqlite3
Binary file not shown.
8 changes: 7 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
asgiref==3.8.1
Django==4.0.4
djangorestframework==3.13.1
flake8==5.0.4
flake8-quotes==3.3.1
flake8-variables-names==0.0.5
pep8-naming==0.13.2
mccabe==0.7.0
pep8-naming==0.13.2
pycodestyle==2.9.1
pyflakes==2.5.0
pytz==2024.2
sqlparse==0.5.2
Loading