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

Open
wants to merge 3 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
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
__pycache__/
*.pyc
*.pyo
*.db
*.sqlite3
*.db3
.env
.idea/
.DS_Store
9 changes: 9 additions & 0 deletions cinema/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.db import models

class Movie(models.Model):
title = models.CharField(max_length=255)
description = models.CharField(max_length=1024)
duration = models.PositiveIntegerField()

def __str__(self):
return self.title
7 changes: 7 additions & 0 deletions cinema/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from rest_framework import serializers
from .models import Movie

class MovieSerializer(serializers.ModelSerializer):
class Meta:
model = Movie
fields = "__all__"
7 changes: 7 additions & 0 deletions cinema/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.urls import path
from . import views

urlpatterns = [
path("movies/", views.movie_list, name="movie-list"),
path("movies/<int:pk>/", views.movie_detail, name="movie-detail"),
]
37 changes: 37 additions & 0 deletions cinema/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from django.shortcuts import get_object_or_404
from .models import Movie
from .serializers import MovieSerializer

@api_view(["GET", "POST"])
def movie_list(request):
if request.method == "GET":
movies = Movie.objects.all()
serializer = MovieSerializer(movies, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)

elif request.method == "POST":
serializer = MovieSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)

@api_view(["GET", "PUT", "DELETE"])
def movie_detail(request, pk):
movie = get_object_or_404(Movie, pk=pk)

if request.method == "GET":
serializer = MovieSerializer(movie)
return Response(serializer.data, status=status.HTTP_200_OK)

elif request.method == "PUT":
serializer = MovieSerializer(movie, data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)

elif request.method == "DELETE":
movie.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
124 changes: 124 additions & 0 deletions cinema_project/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
"""Django settings for cinema_project project.

Generated by "django-admin startproject" using Django 5.1.4.

For more information on this file, see
https://docs.djangoproject.com/en/5.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.1/ref/settings/
"""

from pathlib import Path

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


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "django-insecure-3g4aa#6-y#9+d98vsme+dmmm4w2@%zss64k_=+ib0pcsqgv_fl"

Choose a reason for hiding this comment

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

The SECRET_KEY is hardcoded and exposed in the settings file. For security reasons, especially in production, it's recommended to keep the secret key in an environment variable or a separate configuration file that is not included in version control.

Choose a reason for hiding this comment

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

The SECRET_KEY is hardcoded in the settings file. It's crucial to move this to an environment variable or a separate configuration file to enhance security. This is a critical issue that needs to be addressed before deploying to production.


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

Choose a reason for hiding this comment

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

The DEBUG setting is set to True. This should be set to False in a production environment to avoid exposing sensitive information.

Choose a reason for hiding this comment

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

The DEBUG setting is currently set to True. This should be set to False in a production environment to prevent exposing sensitive information. This is a critical issue that needs to be addressed before deploying to production.


ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"rest_framework",
"cinema",
]

MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = "cinema_project.urls"

TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]

WSGI_APPLICATION = "cinema_project.wsgi.application"


# Database
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases

DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}


# Password validation
# https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
{
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
},
{
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
},
{
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
},
]


# Internationalization
# https://docs.djangoproject.com/en/5.1/topics/i18n/

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.1/howto/static-files/

STATIC_URL = "static/"

# Default primary key field type
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
7 changes: 7 additions & 0 deletions cinema_project/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
path("admin/", admin.site.urls),
path("api/cinema/", include("cinema.urls")),
]