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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
venv/
.pytest_cache/
**__pycache__/
db.sqlite3
A-n-a-s-t-a-s-i-i-a marked this conversation as resolved.
Show resolved Hide resolved
A-n-a-s-t-a-s-i-i-a marked this conversation as resolved.
Show resolved Hide resolved
67 changes: 66 additions & 1 deletion cinema/serializers.py
Original file line number Diff line number Diff line change
@@ -1 +1,66 @@
# 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 = "__all__"


class ActorSerializer(serializers.ModelSerializer):
full_name = serializers.SerializerMethodField()

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 MovieSerializer(serializers.ModelSerializer):
class Meta:
model = Movie
fields = ("id", "title", "description", "duration", "genres", "actors")


class MovieListSerializer(MovieSerializer):
actors = serializers.StringRelatedField(many=True)
genres = serializers.StringRelatedField(many=True)


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(serializers.ModelSerializer):
movie_title = serializers.CharField(source="movie.title")
cinema_hall_name = serializers.CharField(source="cinema_hall.name")
cinema_hall_capacity = (serializers.IntegerField(
source="cinema_hall.capacity")
)

A-n-a-s-t-a-s-i-i-a marked this conversation as resolved.
Show resolved Hide resolved
class Meta:
model = MovieSession
fields = ("id", "show_time", "movie_title",
"cinema_hall_name", "cinema_hall_capacity")


class MovieSessionRetrieveSerializer(MovieSessionSerializer):
movie = MovieListSerializer()
cinema_hall = CinemaHallSerializer()
18 changes: 17 additions & 1 deletion cinema/urls.py
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
# write urls here
from django.urls import path, include
from rest_framework import routers

from cinema import views

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

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

app_name = "cinema"
64 changes: 63 additions & 1 deletion cinema/views.py
Original file line number Diff line number Diff line change
@@ -1 +1,63 @@
# 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,
MovieRetrieveSerializer,
MovieSessionListSerializer,
MovieSessionRetrieveSerializer)


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().prefetch_related("genres", "actors")
serializer_class = MovieSerializer
A-n-a-s-t-a-s-i-i-a marked this conversation as resolved.
Show resolved Hide resolved

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

A-n-a-s-t-a-s-i-i-a marked this conversation as resolved.
Show resolved Hide resolved
def get_queryset(self):
queryset = self.queryset
if self.action in ("list", "retrieve"):
return queryset.prefetch_related("genres", "actors")
return queryset

A-n-a-s-t-a-s-i-i-a marked this conversation as resolved.
Show resolved Hide resolved

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

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

A-n-a-s-t-a-s-i-i-a marked this conversation as resolved.
Show resolved Hide resolved
def get_queryset(self):
queryset = self.queryset
if self.action in ("list", "retrieve"):
return queryset.select_related("movie", "cinema_hall")
return queryset
15 changes: 11 additions & 4 deletions cinema_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

from pathlib import Path

from decouple import config

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

Expand All @@ -20,12 +22,10 @@
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = (
"django-insecure-6vubhk2$++agnctay_4pxy_8cq)mosmn(*-#2b^v4cgsh-^!i3"
)
SECRET_KEY = config("SECRET_KEY")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
DEBUG = False

ALLOWED_HOSTS = []

Expand All @@ -42,6 +42,7 @@
"rest_framework",
"cinema",
"user",
"debug_toolbar",
A-n-a-s-t-a-s-i-i-a marked this conversation as resolved.
Show resolved Hide resolved
]

MIDDLEWARE = [
Expand All @@ -52,6 +53,7 @@
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"debug_toolbar.middleware.DebugToolbarMiddleware",
A-n-a-s-t-a-s-i-i-a marked this conversation as resolved.
Show resolved Hide resolved
]

ROOT_URLCONF = "cinema_service.urls"
Expand Down Expand Up @@ -131,3 +133,8 @@
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"


INTERNAL_IPS = [
"127.0.0.1",
]
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 debug_toolbar.toolbar import debug_toolbar_urls
A-n-a-s-t-a-s-i-i-a marked this conversation as resolved.
Show resolved Hide resolved
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")),
]
10 changes: 9 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
asgiref==3.8.1
Django==4.0.4
django-debug-toolbar==3.2.1
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
python-decouple==3.8
pytz==2024.2
sqlparse==0.5.3
Loading