Skip to content
Open

Develop #1019

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
6 changes: 6 additions & 0 deletions .djlintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"indent": 2,
"max_line_length": 100,
"format_css": true,
"format_js": true
}
30 changes: 30 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
FROM python:3.11-alpine
LABEL maintainer="student@mate.academy"

ENV PYTHONUNBUFFERED 1

WORKDIR /app

# Установка зависимостей системы
RUN apk add --update --no-cache \
postgresql-client \
jpeg-dev \
libc-dev \
linux-headers \
postgresql-dev \
zlib-dev

Comment on lines +11 to +16
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The wait_for_db command gets connections["default"] but never calls ensure_connection() to verify the connection actually works. Add db_conn.ensure_connection() after line 12.

COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

COPY . .

# Создание папок для статики и медиа
RUN mkdir -p /vol/web/media
RUN mkdir -p /vol/web/static

# Безопасность: запуск от не-root пользователя
RUN adduser -D user
RUN chown -R user:user /vol/
RUN chmod -R 755 /vol/web
USER user
17 changes: 17 additions & 0 deletions cinema/management/commands/wait_for_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import time
from django.db import connections
from django.db.utils import OperationalError
from django.core.management.base import BaseCommand


class Command(BaseCommand):
def handle(self, *args, **options):
self.stdout.write("Waiting for database...")
db_conn = None
while not db_conn:
try:
db_conn = connections["default"]
Comment on lines +12 to +13
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

After getting the connection with connections['default'], you must call db_conn.ensure_connection() to actually validate the database is reachable. Without this, the command will succeed even if the database is unreachable.

self.stdout.write(self.style.SUCCESS("Database available!"))
except OperationalError:
self.stdout.write("Database unavailable, waiting 1 second...")
time.sleep(1)
9 changes: 8 additions & 1 deletion cinema_service/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"""
from datetime import timedelta
from pathlib import Path
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The wait_for_db command gets the connection but doesn't call db_conn.ensure_connection() to actually validate that the database is reachable. This call should be added after getting the connection.

import os


# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Expand Down Expand Up @@ -84,10 +86,15 @@
# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases


DATABASES = {
"default": {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The database ENGINE is still sqlite3, but the task explicitly requires switching to PostgreSQL. Change ENGINE to django.db.backends.postgresql for proper Docker integration.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The ENGINE is still set to 'django.db.backends.sqlite3'. It must be changed to 'django.db.backends.postgresql' to connect to the PostgreSQL container.

"ENGINE": "django.db.backends.sqlite3",
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The NAME is set to SQLite file db.sqlite3. When using PostgreSQL, NAME should be the database name (cinema). Remove the BASE_DIR / "db.sqlite3" line and use os.getenv("DB_NAME", "cinema") instead.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The NAME should use an environment variable (e.g., os.getenv('DB_NAME', 'cinema')) instead of pointing to a local SQLite file BASE_DIR / 'db.sqlite3'.

"NAME": BASE_DIR / "db.sqlite3",
"USER": os.getenv("DB_USER", "postgres"),
"PASSWORD": os.getenv("DB_PASS", "postgres"),
"HOST": os.getenv("DB_HOST", "localhost"),
"PORT": "5432",
}
}

Expand Down Expand Up @@ -132,7 +139,7 @@
# https://docs.djangoproject.com/en/4.0/howto/static-files/

STATIC_URL = "static/"

STATIC_ROOT = "/vol/web/static"
MEDIA_URL = "/media/"
MEDIA_ROOT = BASE_DIR / "media"

Expand Down
41 changes: 41 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# @format

version: '3.8'

services:
db:
image: postgres:15-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_DB=cinema
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
Comment on lines +11 to +13
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

After getting db_conn = connections["default"], you need to call db_conn.ensure_connection() inside the try block to actually validate that the database connection works. Without this call, the connection object might exist but not be functional.


app:
build:
context: .
ports:
- '8000:8000'
volumes:
- .:/app
- static_data:/vol/web/static
- media_data:/vol/web/media
environment:
- DB_HOST=db
- DB_NAME=cinema
- DB_USER=postgres
- DB_PASS=postgres
env_file:
- .env
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The .dockerignore file is missing entirely. It must be created to exclude unnecessary files (like __pycache__, .git, *.pyc, etc.) from Docker images to make them thin as required by the task.

command: >
sh -c "python manage.py wait_for_db &&
python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000"
depends_on:
- db

volumes:
postgres_data:
static_data:
media_data:
5 changes: 5 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[pytest]
DJANGO_SETTINGS_MODULE = cinema_service.settings
python_files = tests.py test_*.py *_tests.py
filterwarnings =
ignore::django.utils.deprecation.RemovedInDjango70Warning
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ djangorestframework
djangorestframework-simplejwt
drf-spectacular
Pillow

black
djlint
crispy-bootstrap4
pytest
pytest-django
Loading