From 464f21c7d0160dc52fb81cf0d05351b4e7488cdb Mon Sep 17 00:00:00 2001 From: Roman Vynnytskyi Date: Sun, 8 Dec 2024 14:58:33 +0200 Subject: [PATCH 01/11] update gitignore file --- .gitignore | 383 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 377 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index b26d6116..27edab89 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,379 @@ -.idea/ -.vscode/ -*.iml +# Created by https://www.toptal.com/developers/gitignore/api/pycharm,python,django +# Edit at https://www.toptal.com/developers/gitignore?templates=pycharm,python,django + +### Django ### +*.log +*.pot +*.pyc +__pycache__/ +local_settings.py +db.sqlite3 +db.sqlite3-journal +media + +# If your build process includes running collectstatic, then you probably don't need or want to include staticfiles/ +# in your Git repository. Update and uncomment the following line accordingly. +# /staticfiles/ + +### Django.Python Stack ### +# Byte-compiled / optimized / DLL files +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo + +# Django stuff: + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments .env -.DS_Store +.venv +env/ venv/ -.pytest_cache/ -**__pycache__/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + +### PyCharm ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# AWS User-specific +.idea/**/aws.xml + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# SonarLint plugin +.idea/sonarlint/ + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### PyCharm Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr + +# Sonarlint plugin +# https://plugins.jetbrains.com/plugin/7973-sonarlint +.idea/**/sonarlint/ + +# SonarQube Plugin +# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin +.idea/**/sonarIssues.xml + +# Markdown Navigator plugin +# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced +.idea/**/markdown-navigator.xml +.idea/**/markdown-navigator-enh.xml +.idea/**/markdown-navigator/ + +# Cache file creation bug +# See https://youtrack.jetbrains.com/issue/JBR-2257 +.idea/$CACHE_FILE$ + +# CodeStream plugin +# https://plugins.jetbrains.com/plugin/12206-codestream +.idea/codestream.xml + +# Azure Toolkit for IntelliJ plugin +# https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij +.idea/**/azureSettings.xml + +### Python ### +# Byte-compiled / optimized / DLL files + +# C extensions + +# Distribution / packaging + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. + +# Installer logs + +# Unit test / coverage reports + +# Translations + +# Django stuff: + +# Flask stuff: + +# Scrapy stuff: + +# Sphinx documentation + +# PyBuilder + +# Jupyter Notebook + +# IPython + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm + +# Celery stuff + +# SageMath parsed files + +# Environments + +# Spyder project settings + +# Rope project settings + +# mkdocs documentation + +# mypy + +# Pyre type checker + +# pytype static type analyzer + +# Cython debug symbols + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. + +### Python Patch ### +# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration +poetry.toml + +# ruff +.ruff_cache/ + +# LSP config files +pyrightconfig.json + +# End of https://www.toptal.com/developers/gitignore/api/pycharm,python,django \ No newline at end of file From bd946a48c1c9bf9a0a445c049cd17678c803e7ce Mon Sep 17 00:00:00 2001 From: Roman Vynnytskyi Date: Sun, 8 Dec 2024 14:59:28 +0200 Subject: [PATCH 02/11] define full_name method for slug_field --- cinema/models.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/cinema/models.py b/cinema/models.py index c42d2a3d..637d1734 100644 --- a/cinema/models.py +++ b/cinema/models.py @@ -1,6 +1,6 @@ +from django.conf import settings from django.core.exceptions import ValidationError from django.db import models -from django.conf import settings class CinemaHall(models.Model): @@ -27,6 +27,11 @@ class Actor(models.Model): first_name = models.CharField(max_length=255) last_name = models.CharField(max_length=255) + @property + def full_name(self): + return f"{self.first_name} {self.last_name}" + + def __str__(self): return self.first_name + " " + self.last_name @@ -48,7 +53,7 @@ def __str__(self): class MovieSession(models.Model): show_time = models.DateTimeField() movie = models.ForeignKey(Movie, on_delete=models.CASCADE) - cinema_hall = models.ForeignKey(CinemaHall, on_delete=models.CASCADE) + cinema_hall = models.ForeignKey(CinemaHall, on_delete=models.CASCADE, related_name="movie_sessions") class Meta: ordering = ["-show_time"] @@ -92,9 +97,9 @@ def clean(self): raise ValidationError( { ticket_attr_name: f"{ticket_attr_name} number " - f"must be in available range: " - f"(1, {cinema_hall_attr_name}): " - f"(1, {count_attrs})" + f"must be in available range: " + f"(1, {cinema_hall_attr_name}): " + f"(1, {count_attrs})" } ) From 344b26ee42feaaf35a555fd056dc0e830847baaa Mon Sep 17 00:00:00 2001 From: Roman Vynnytskyi Date: Sun, 8 Dec 2024 15:00:07 +0200 Subject: [PATCH 03/11] write serializers for each model except order and ticket --- cinema/serializers.py | 82 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/cinema/serializers.py b/cinema/serializers.py index 612ca7e2..95ca936c 100644 --- a/cinema/serializers.py +++ b/cinema/serializers.py @@ -1 +1,81 @@ -# write serializers here +from rest_framework import serializers + +from cinema.models import CinemaHall, Actor, Genre, Movie, MovieSession + + +class CinemaHallSerializer(serializers.ModelSerializer): + class Meta: + model = CinemaHall + fields = ("id", "name", "rows", "seats_in_row") + + +class GenreSerializer(serializers.ModelSerializer): + class Meta: + model = Genre + fields = ("id", "name") + + +class ActorSerializer(serializers.ModelSerializer): + class Meta: + model = Actor + fields = ("id", "first_name", "last_name") + + +class MovieSerializer(serializers.ModelSerializer): + class Meta: + model = Movie + field = ("id", "title", "description", "duration", "genres", "actors") + + +class MovieRetrieveSerializer(MovieSerializer): + genres = GenreSerializer(many=True, read_only=True) + actors = ActorSerializer(many=True, read_only=True) + + +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 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" + ) + + +class MovieSessionRetrieveSerializer(MovieSessionSerializer): + movie = MovieListSerializer() + cinema_hall = CinemaHallSerializer() From a1c323072f5637c2dad947a9aebb642c4d11612f Mon Sep 17 00:00:00 2001 From: Roman Vynnytskyi Date: Sun, 8 Dec 2024 15:00:55 +0200 Subject: [PATCH 04/11] write routres for future viewsets --- cinema/urls.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/cinema/urls.py b/cinema/urls.py index 420f8e8c..86000a80 100644 --- a/cinema/urls.py +++ b/cinema/urls.py @@ -1 +1,17 @@ -# write urls here +from django.urls import path, include +from rest_framework import routers + +from cinema.views import CinemaHallViewSet, GenreViewSet, ActorViewSet, MovieViewSet, MovieSessionViewSet +app_name = "cinema" + +router = routers.DefaultRouter() +router.register("halls", CinemaHallViewSet) +router.register("genres", GenreViewSet) +router.register("actors", ActorViewSet) +router.register("movies", MovieViewSet) +router.register("sessions", MovieSessionViewSet) + + +urlpatterns = [ + path("", include(router.urls)) +] \ No newline at end of file From 77da474d8da200e9c3c82be2e35898a67571c413 Mon Sep 17 00:00:00 2001 From: Roman Vynnytskyi Date: Sun, 8 Dec 2024 15:03:11 +0200 Subject: [PATCH 05/11] write default serializers and define methods in movieviewset --- cinema/views.py | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/cinema/views.py b/cinema/views.py index ae87bfde..60485710 100644 --- a/cinema/views.py +++ b/cinema/views.py @@ -1 +1,43 @@ -# 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, MovieRetrieveSerializer, MovieListSerializer + + +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() + serializer_class = MovieSerializer + + def get_serializer_class(self): + if self.action == "retrieve": + return MovieRetrieveSerializer + elif self.action == "list": + return MovieListSerializer + return self.serializer_class + + def get_queryset(self): + queryset = self.queryset + if self.action in ("list", "retrieve"): + return queryset.prefetch_related("genres", "actors") + return queryset + + +class MovieSessionViewSet(viewsets.ModelViewSet): + queryset = MovieSession.objects.all() + serializer_class = MovieSessionSerializer From 6645cceac1dcf87d04f11098fd9aa78f7baa75e4 Mon Sep 17 00:00:00 2001 From: Roman Vynnytskyi Date: Sun, 8 Dec 2024 15:04:26 +0200 Subject: [PATCH 06/11] hide secret key to .env and write urls for admin and cinema --- cinema_service/settings.py | 12 +++++------- cinema_service/urls.py | 4 +++- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cinema_service/settings.py b/cinema_service/settings.py index 4b981921..d8622812 100644 --- a/cinema_service/settings.py +++ b/cinema_service/settings.py @@ -9,9 +9,9 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/4.0/ref/settings/ """ - +import os 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 @@ -20,14 +20,12 @@ # 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("DJANGO_SECRET_KEY", default="fallback-secret-key") # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +DEBUG = False -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = ['127.0.0.1', 'localhost'] # Application definition diff --git a/cinema_service/urls.py b/cinema_service/urls.py index 083932c6..1634965e 100644 --- a/cinema_service/urls.py +++ b/cinema_service/urls.py @@ -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", namespace="cinema")) +] \ No newline at end of file From 1f5c2a7ed60d6b5ca5233b84e4949d9cb92a9a0b Mon Sep 17 00:00:00 2001 From: Roman Vynnytskyi Date: Sun, 8 Dec 2024 15:05:04 +0200 Subject: [PATCH 07/11] add pytest.ini --- pytest.ini | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 pytest.ini diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 00000000..81a91f77 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,4 @@ +[pytest] +DJANGO_SETTINGS_MODULE=cinema_service.settings +django_find_project = True +addopts = --reuse-db --tb=short -v \ No newline at end of file From 456921e84eaaddf2a926c4fe5d41b3aa6dfde91e Mon Sep 17 00:00:00 2001 From: Roman Vynnytskyi Date: Sun, 8 Dec 2024 15:05:35 +0200 Subject: [PATCH 08/11] add requirements.txt --- .idea/.gitignore | 8 +++++ .../inspectionProfiles/profiles_settings.xml | 6 ++++ .idea/misc.xml | 7 +++++ .idea/modules.xml | 8 +++++ .idea/py-api-serializers.iml | 29 +++++++++++++++++++ .idea/vcs.xml | 6 ++++ requirements.txt | 3 +- 7 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/py-api-serializers.iml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..1c2fda56 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 00000000..105ce2da --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..b74f18a4 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..685a2829 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/py-api-serializers.iml b/.idea/py-api-serializers.iml new file mode 100644 index 00000000..f06ead6f --- /dev/null +++ b/.idea/py-api-serializers.iml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..c8397c94 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 108897e9..d1b2f423 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,5 @@ djangorestframework==3.13.1 flake8==5.0.4 flake8-quotes==3.3.1 flake8-variables-names==0.0.5 -pep8-naming==0.13.2 \ No newline at end of file +pep8-naming==0.13.2 +serializers~=0.2.4 \ No newline at end of file From 410c27847942c7a423f9bac7f49a1a2d8fddc151 Mon Sep 17 00:00:00 2001 From: Roman Vynnytskyi Date: Sun, 8 Dec 2024 15:09:39 +0200 Subject: [PATCH 09/11] correct flake8 requires --- cinema/migrations/0003_movie_duration.py | 6 ++--- cinema/migrations/0004_alter_genre_name.py | 6 ++--- cinema/models.py | 27 ++++++++-------------- cinema/serializers.py | 25 +++++--------------- cinema/tests/test_actor_api.py | 4 +--- cinema/tests/test_cinema_hall_api.py | 12 +++------- cinema/tests/test_movie_api.py | 4 +--- cinema/tests/test_movie_session_api.py | 8 ++----- cinema/urls.py | 13 +++++++---- cinema/views.py | 11 +++++++-- cinema_service/settings.py | 13 +++++------ cinema_service/urls.py | 4 ++-- 12 files changed, 55 insertions(+), 78 deletions(-) diff --git a/cinema/migrations/0003_movie_duration.py b/cinema/migrations/0003_movie_duration.py index 7355c91a..e75f6794 100644 --- a/cinema/migrations/0003_movie_duration.py +++ b/cinema/migrations/0003_movie_duration.py @@ -6,13 +6,13 @@ class Migration(migrations.Migration): dependencies = [ - ('cinema', '0002_initial'), + ("cinema", "0002_initial"), ] operations = [ migrations.AddField( - model_name='movie', - name='duration', + model_name="movie", + name="duration", field=models.IntegerField(default=123), preserve_default=False, ), diff --git a/cinema/migrations/0004_alter_genre_name.py b/cinema/migrations/0004_alter_genre_name.py index 0a4d429e..a0c446ee 100644 --- a/cinema/migrations/0004_alter_genre_name.py +++ b/cinema/migrations/0004_alter_genre_name.py @@ -6,13 +6,13 @@ class Migration(migrations.Migration): dependencies = [ - ('cinema', '0003_movie_duration'), + ("cinema", "0003_movie_duration"), ] operations = [ migrations.AlterField( - model_name='genre', - name='name', + model_name="genre", + name="name", field=models.CharField(max_length=255, unique=True), ), ] diff --git a/cinema/models.py b/cinema/models.py index 637d1734..d6ee85ea 100644 --- a/cinema/models.py +++ b/cinema/models.py @@ -31,7 +31,6 @@ class Actor(models.Model): def full_name(self): return f"{self.first_name} {self.last_name}" - def __str__(self): return self.first_name + " " + self.last_name @@ -53,7 +52,9 @@ def __str__(self): class MovieSession(models.Model): show_time = models.DateTimeField() movie = models.ForeignKey(Movie, on_delete=models.CASCADE) - cinema_hall = models.ForeignKey(CinemaHall, on_delete=models.CASCADE, related_name="movie_sessions") + cinema_hall = models.ForeignKey( + CinemaHall, on_delete=models.CASCADE, related_name="movie_sessions" + ) class Meta: ordering = ["-show_time"] @@ -64,9 +65,7 @@ def __str__(self): class Order(models.Model): created_at = models.DateTimeField(auto_now_add=True) - user = models.ForeignKey( - settings.AUTH_USER_MODEL, on_delete=models.CASCADE - ) + user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) def __str__(self): return str(self.created_at) @@ -79,9 +78,7 @@ class Ticket(models.Model): movie_session = models.ForeignKey( MovieSession, on_delete=models.CASCADE, related_name="tickets" ) - order = models.ForeignKey( - Order, on_delete=models.CASCADE, related_name="tickets" - ) + order = models.ForeignKey(Order, on_delete=models.CASCADE, related_name="tickets") row = models.IntegerField() seat = models.IntegerField() @@ -90,23 +87,19 @@ def clean(self): (self.row, "row", "count_rows"), (self.seat, "seat", "count_seats_in_row"), ]: - count_attrs = getattr( - self.movie_session.cinema_hall, cinema_hall_attr_name - ) + count_attrs = getattr(self.movie_session.cinema_hall, cinema_hall_attr_name) if not (1 <= ticket_attr_value <= count_attrs): raise ValidationError( { ticket_attr_name: f"{ticket_attr_name} number " - f"must be in available range: " - f"(1, {cinema_hall_attr_name}): " - f"(1, {count_attrs})" + f"must be in available range: " + f"(1, {cinema_hall_attr_name}): " + f"(1, {count_attrs})" } ) def __str__(self): - return ( - f"{str(self.movie_session)} (row: {self.row}, seat: {self.seat})" - ) + return f"{str(self.movie_session)} (row: {self.row}, seat: {self.seat})" class Meta: unique_together = ("movie_session", "row", "seat") diff --git a/cinema/serializers.py b/cinema/serializers.py index 95ca936c..a08fc4e0 100644 --- a/cinema/serializers.py +++ b/cinema/serializers.py @@ -33,15 +33,9 @@ class MovieRetrieveSerializer(MovieSerializer): class MovieListSerializer(MovieSerializer): - genres = serializers.SlugRelatedField( - many=True, - read_only=True, - slug_field="name" - ) + genres = serializers.SlugRelatedField(many=True, read_only=True, slug_field="name") actors = serializers.SlugRelatedField( - many=True, - read_only=True, - slug_field="full_name" + many=True, read_only=True, slug_field="full_name" ) @@ -52,17 +46,10 @@ class Meta: 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 - ) + 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 + source="cinema_hall.capacity", read_only=True ) class Meta: @@ -72,7 +59,7 @@ class Meta: "show_time", "movie_title", "cinema_hall_name", - "cinema_hall_capacity" + "cinema_hall_capacity", ) diff --git a/cinema/tests/test_actor_api.py b/cinema/tests/test_actor_api.py index 3fa7a469..097e42ca 100644 --- a/cinema/tests/test_actor_api.py +++ b/cinema/tests/test_actor_api.py @@ -16,9 +16,7 @@ def test_get_actors(self): response = self.client.get("/api/cinema/actors/") self.assertEqual(response.status_code, status.HTTP_200_OK) actors_full_names = [actor["full_name"] for actor in response.data] - self.assertEqual( - sorted(actors_full_names), ["George Clooney", "Keanu Reeves"] - ) + self.assertEqual(sorted(actors_full_names), ["George Clooney", "Keanu Reeves"]) def test_post_actors(self): response = self.client.post( diff --git a/cinema/tests/test_cinema_hall_api.py b/cinema/tests/test_cinema_hall_api.py index d3e9ea77..be74b47a 100644 --- a/cinema/tests/test_cinema_hall_api.py +++ b/cinema/tests/test_cinema_hall_api.py @@ -31,9 +31,7 @@ def test_get_cinema_halls(self): self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data[0]["name"], blue_hall["name"]) self.assertEqual(response.data[0]["rows"], blue_hall["rows"]) - self.assertEqual( - response.data[0]["seats_in_row"], blue_hall["seats_in_row"] - ) + self.assertEqual(response.data[0]["seats_in_row"], blue_hall["seats_in_row"]) vip_hall = { "name": "VIP", "rows": 6, @@ -43,9 +41,7 @@ def test_get_cinema_halls(self): self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data[1]["name"], vip_hall["name"]) self.assertEqual(response.data[1]["rows"], vip_hall["rows"]) - self.assertEqual( - response.data[1]["seats_in_row"], vip_hall["seats_in_row"] - ) + self.assertEqual(response.data[1]["seats_in_row"], vip_hall["seats_in_row"]) def test_post_cinema_halls(self): response = self.client.post( @@ -72,9 +68,7 @@ def test_get_cinema_hall(self): self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data["name"], vip_hall["name"]) self.assertEqual(response.data["rows"], vip_hall["rows"]) - self.assertEqual( - response.data["seats_in_row"], vip_hall["seats_in_row"] - ) + self.assertEqual(response.data["seats_in_row"], vip_hall["seats_in_row"]) self.assertEqual(response.data["capacity"], vip_hall["capacity"]) def test_get_invalid_cinema_hall(self): diff --git a/cinema/tests/test_movie_api.py b/cinema/tests/test_movie_api.py index 9d6b76bf..d4852de5 100644 --- a/cinema/tests/test_movie_api.py +++ b/cinema/tests/test_movie_api.py @@ -83,9 +83,7 @@ def test_get_movie(self): self.assertEqual(response.data["genres"][1]["name"], "Comedy") self.assertEqual(response.data["actors"][0]["first_name"], "Kate") self.assertEqual(response.data["actors"][0]["last_name"], "Winslet") - self.assertEqual( - response.data["actors"][0]["full_name"], "Kate Winslet" - ) + self.assertEqual(response.data["actors"][0]["full_name"], "Kate Winslet") def test_get_invalid_movie(self): response = self.client.get("/api/cinema/movies/100/") diff --git a/cinema/tests/test_movie_session_api.py b/cinema/tests/test_movie_session_api.py index cbd02713..d165662f 100644 --- a/cinema/tests/test_movie_session_api.py +++ b/cinema/tests/test_movie_session_api.py @@ -46,9 +46,7 @@ def test_get_movie_sessions(self): } self.assertEqual(movie_sessions.status_code, status.HTTP_200_OK) for field in movie_session: - self.assertEqual( - movie_sessions.data[0][field], movie_session[field] - ) + self.assertEqual(movie_sessions.data[0][field], movie_session[field]) def test_post_movie_session(self): movies = self.client.post( @@ -67,9 +65,7 @@ def test_get_movie_session(self): response = self.client.get("/api/cinema/movie_sessions/1/") self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data["movie"]["title"], "Titanic") - self.assertEqual( - response.data["movie"]["description"], "Titanic description" - ) + self.assertEqual(response.data["movie"]["description"], "Titanic description") self.assertEqual(response.data["movie"]["duration"], 123) self.assertEqual(response.data["movie"]["genres"], ["Drama", "Comedy"]) self.assertEqual(response.data["movie"]["actors"], ["Kate Winslet"]) diff --git a/cinema/urls.py b/cinema/urls.py index 86000a80..45ee8838 100644 --- a/cinema/urls.py +++ b/cinema/urls.py @@ -1,7 +1,14 @@ from django.urls import path, include from rest_framework import routers -from cinema.views import CinemaHallViewSet, GenreViewSet, ActorViewSet, MovieViewSet, MovieSessionViewSet +from cinema.views import ( + CinemaHallViewSet, + GenreViewSet, + ActorViewSet, + MovieViewSet, + MovieSessionViewSet, +) + app_name = "cinema" router = routers.DefaultRouter() @@ -12,6 +19,4 @@ router.register("sessions", MovieSessionViewSet) -urlpatterns = [ - path("", include(router.urls)) -] \ No newline at end of file +urlpatterns = [path("", include(router.urls))] diff --git a/cinema/views.py b/cinema/views.py index 60485710..73decb4d 100644 --- a/cinema/views.py +++ b/cinema/views.py @@ -1,8 +1,15 @@ from rest_framework import viewsets from cinema.models import CinemaHall, Genre, Actor, Movie, MovieSession -from cinema.serializers import CinemaHallSerializer, GenreSerializer, ActorSerializer, MovieSerializer, \ - MovieSessionSerializer, MovieRetrieveSerializer, MovieListSerializer +from cinema.serializers import ( + CinemaHallSerializer, + GenreSerializer, + ActorSerializer, + MovieSerializer, + MovieSessionSerializer, + MovieRetrieveSerializer, + MovieListSerializer, +) class CinemaHallViewSet(viewsets.ModelViewSet): diff --git a/cinema_service/settings.py b/cinema_service/settings.py index d8622812..030d75d9 100644 --- a/cinema_service/settings.py +++ b/cinema_service/settings.py @@ -9,9 +9,11 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/4.0/ref/settings/ """ + import os 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 @@ -25,7 +27,7 @@ # SECURITY WARNING: don't run with debug turned on in production! DEBUG = False -ALLOWED_HOSTS = ['127.0.0.1', 'localhost'] +ALLOWED_HOSTS = ["127.0.0.1", "localhost"] # Application definition @@ -93,16 +95,13 @@ "UserAttributeSimilarityValidator", }, { - "NAME": "django.contrib.auth.password_validation." - "MinimumLengthValidator", + "NAME": "django.contrib.auth.password_validation." "MinimumLengthValidator", }, { - "NAME": "django.contrib.auth.password_validation." - "CommonPasswordValidator", + "NAME": "django.contrib.auth.password_validation." "CommonPasswordValidator", }, { - "NAME": "django.contrib.auth.password_validation." - "NumericPasswordValidator", + "NAME": "django.contrib.auth.password_validation." "NumericPasswordValidator", }, ] diff --git a/cinema_service/urls.py b/cinema_service/urls.py index 1634965e..07220424 100644 --- a/cinema_service/urls.py +++ b/cinema_service/urls.py @@ -4,5 +4,5 @@ urlpatterns = [ path("admin/", admin.site.urls), - path("api/cinema/", include("cinema.urls", namespace="cinema")) -] \ No newline at end of file + path("api/cinema/", include("cinema.urls", namespace="cinema")), +] From 3620c3701b9483028a19753daa684bcb45216787 Mon Sep 17 00:00:00 2001 From: Roman Vynnytskyi Date: Mon, 9 Dec 2024 10:29:53 +0200 Subject: [PATCH 10/11] fix flake8 problems --- cinema/models.py | 13 +++++++++---- cinema/serializers.py | 10 +++++++--- cinema_service/settings.py | 9 ++++++--- cinema_service/urls.py | 3 ++- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/cinema/models.py b/cinema/models.py index d6ee85ea..cc8c844a 100644 --- a/cinema/models.py +++ b/cinema/models.py @@ -65,7 +65,8 @@ def __str__(self): class Order(models.Model): created_at = models.DateTimeField(auto_now_add=True) - user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) + user = models.ForeignKey(settings.AUTH_USER_MODEL, + on_delete=models.CASCADE) def __str__(self): return str(self.created_at) @@ -78,7 +79,9 @@ class Ticket(models.Model): movie_session = models.ForeignKey( MovieSession, on_delete=models.CASCADE, related_name="tickets" ) - order = models.ForeignKey(Order, on_delete=models.CASCADE, related_name="tickets") + order = models.ForeignKey(Order, + on_delete=models.CASCADE, + related_name="tickets") row = models.IntegerField() seat = models.IntegerField() @@ -87,7 +90,8 @@ def clean(self): (self.row, "row", "count_rows"), (self.seat, "seat", "count_seats_in_row"), ]: - count_attrs = getattr(self.movie_session.cinema_hall, cinema_hall_attr_name) + count_attrs = getattr(self.movie_session.cinema_hall, + cinema_hall_attr_name) if not (1 <= ticket_attr_value <= count_attrs): raise ValidationError( { @@ -99,7 +103,8 @@ def clean(self): ) def __str__(self): - return f"{str(self.movie_session)} (row: {self.row}, seat: {self.seat})" + return (f"{str(self.movie_session)}" + f" (row: {self.row}, seat: {self.seat})") class Meta: unique_together = ("movie_session", "row", "seat") diff --git a/cinema/serializers.py b/cinema/serializers.py index a08fc4e0..bb08bac1 100644 --- a/cinema/serializers.py +++ b/cinema/serializers.py @@ -33,7 +33,9 @@ class MovieRetrieveSerializer(MovieSerializer): class MovieListSerializer(MovieSerializer): - genres = serializers.SlugRelatedField(many=True, read_only=True, slug_field="name") + genres = serializers.SlugRelatedField(many=True, + read_only=True, + slug_field="name") actors = serializers.SlugRelatedField( many=True, read_only=True, slug_field="full_name" ) @@ -46,8 +48,10 @@ class Meta: 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) + 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 ) diff --git a/cinema_service/settings.py b/cinema_service/settings.py index 030d75d9..d564165a 100644 --- a/cinema_service/settings.py +++ b/cinema_service/settings.py @@ -95,13 +95,16 @@ "UserAttributeSimilarityValidator", }, { - "NAME": "django.contrib.auth.password_validation." "MinimumLengthValidator", + "NAME": + "django.contrib.auth.password_validation." "MinimumLengthValidator", }, { - "NAME": "django.contrib.auth.password_validation." "CommonPasswordValidator", + "NAME": + "django.contrib.auth.password_validation." "CommonPasswordValidator", }, { - "NAME": "django.contrib.auth.password_validation." "NumericPasswordValidator", + "NAME": + "django.contrib.auth.password_validation." "NumericPasswordValidator", }, ] diff --git a/cinema_service/urls.py b/cinema_service/urls.py index 07220424..dbef9426 100644 --- a/cinema_service/urls.py +++ b/cinema_service/urls.py @@ -4,5 +4,6 @@ urlpatterns = [ path("admin/", admin.site.urls), - path("api/cinema/", include("cinema.urls", namespace="cinema")), + path("api/cinema/", include("cinema.urls", + namespace="cinema")), ] From 24e0d7adcf6f9b4f00db8515a81d8095a0044f22 Mon Sep 17 00:00:00 2001 From: Roman Vynnytskyi Date: Mon, 9 Dec 2024 10:33:01 +0200 Subject: [PATCH 11/11] did ai required changes --- cinema/models.py | 1 - cinema/serializers.py | 2 +- cinema_service/urls.py | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/cinema/models.py b/cinema/models.py index cc8c844a..4e82185e 100644 --- a/cinema/models.py +++ b/cinema/models.py @@ -27,7 +27,6 @@ class Actor(models.Model): first_name = models.CharField(max_length=255) last_name = models.CharField(max_length=255) - @property def full_name(self): return f"{self.first_name} {self.last_name}" diff --git a/cinema/serializers.py b/cinema/serializers.py index bb08bac1..192fbc00 100644 --- a/cinema/serializers.py +++ b/cinema/serializers.py @@ -24,7 +24,7 @@ class Meta: class MovieSerializer(serializers.ModelSerializer): class Meta: model = Movie - field = ("id", "title", "description", "duration", "genres", "actors") + fields = ("id", "title", "description", "duration", "genres", "actors") class MovieRetrieveSerializer(MovieSerializer): diff --git a/cinema_service/urls.py b/cinema_service/urls.py index dbef9426..9288016e 100644 --- a/cinema_service/urls.py +++ b/cinema_service/urls.py @@ -1,5 +1,4 @@ from django.contrib import admin -from django.urls import path from django.urls import path, include urlpatterns = [