From 8272b23de1dc2793646fd32565a447e28c716dce Mon Sep 17 00:00:00 2001 From: Tomasz Knapik Date: Tue, 12 Nov 2024 17:09:51 +0000 Subject: [PATCH] Install dj-database-url --- .isort.cfg | 6 ++++++ README.md | 42 +++++++++++++++++++---------------------- config/jinja2.py | 1 + config/settings/base.py | 14 ++++---------- docker-compose.yml | 6 +----- poetry.lock | 30 +++++++++++++++++++++++++++-- pyproject.toml | 1 + 7 files changed, 60 insertions(+), 40 deletions(-) create mode 100644 .isort.cfg diff --git a/.isort.cfg b/.isort.cfg new file mode 100644 index 0000000..3f1129d --- /dev/null +++ b/.isort.cfg @@ -0,0 +1,6 @@ +[settings] +profile=black +sections=FUTURE,STDLIB,DJANGO,THIRDPARTY,FIRSTPARTY,LOCALFOLDER +known_first_party=config +known_django=django +default_section=THIRDPARTY diff --git a/README.md b/README.md index 5ddb054..379e319 100644 --- a/README.md +++ b/README.md @@ -35,26 +35,22 @@ docker compose exec dev format In addition to the [base Docker image variables](https://github.com/nationalarchives/docker/blob/main/docker/tna-python-django/README.md#environment-variables), this application has support for: -| Variable | Purpose | Default | -| ------------------------ | --------------------------------------------------------- | --------------------------------------------------------- | -| `DJANGO_SETTINGS_MODULE` | The configuration to use | `config.settings.production` | -| `ALLOWED_HOSTS` | A comma-separated list of allowed hosts | _none_ on production and staging, `*` on develop and test | -| `USE_X_FORWARDED_HOST` | Use the `X-Forwarded-Host` header in preference to `Host` | `False` | -| `DEBUG` | If true, allow debugging | `False` | -| `COOKIE_DOMAIN` | The domain to save cookie preferences against | _none_ | -| `DATABASE_NAME` | The name of the Postgres database | _none_ | -| `DATABASE_USER` | The username needed to access the Postgres database | _none_ | -| `DATABASE_PASSWORD` | The password needed to access the Postgres database | _none_ | -| `DATABASE_HOST` | The Postgres database host | _none_ | -| `DATABASE_PORT` | The Postgres database port | `5432` | -| `CSP_IMG_SRC` | A comma separated list of CSP rules for `img-src` | `'self'` | -| `CSP_SCRIPT_SRC` | A comma separated list of CSP rules for `script-src` | `'self'` | -| `CSP_SCRIPT_SRC_ELEM` | A comma separated list of CSP rules for `script-src-elem` | `'self'` | -| `CSP_STYLE_SRC` | A comma separated list of CSP rules for `style-src` | `'self'` | -| `CSP_STYLE_SRC_ELEM` | A comma separated list of CSP rules for `style-src-elem` | `'self'` | -| `CSP_FONT_SRC` | A comma separated list of CSP rules for `font-src` | `'self'` | -| `CSP_CONNECT_SRC` | A comma separated list of CSP rules for `connect-src` | `'self'` | -| `CSP_MEDIA_SRC` | A comma separated list of CSP rules for `media-src` | `'self'` | -| `CSP_WORKER_SRC` | A comma separated list of CSP rules for `worker-src` | `'self'` | -| `CSP_FRAME_SRC` | A comma separated list of CSP rules for `frame-src` | `'self'` | -| `GA4_ID` | The Google Analytics 4 ID | _none_ | +| Variable | Purpose | Default | +| ------------------------ | -------------------------------------------------------------- | --------------------------------------------------------- | +| `DJANGO_SETTINGS_MODULE` | The configuration to use | `config.settings.production` | +| `ALLOWED_HOSTS` | A comma-separated list of allowed hosts | _none_ on production and staging, `*` on develop and test | +| `USE_X_FORWARDED_HOST` | Use the `X-Forwarded-Host` header in preference to `Host` | `False` | +| `DEBUG` | If true, allow debugging | `False` | +| `COOKIE_DOMAIN` | The domain to save cookie preferences against | _none_ | +| `DATABASE_URL` | The database's URL (`postgres://USER:PASSWORD@HOST:PORT/NAME`) | _none_ | +| `CSP_IMG_SRC` | A comma separated list of CSP rules for `img-src` | `'self'` | +| `CSP_SCRIPT_SRC` | A comma separated list of CSP rules for `script-src` | `'self'` | +| `CSP_SCRIPT_SRC_ELEM` | A comma separated list of CSP rules for `script-src-elem` | `'self'` | +| `CSP_STYLE_SRC` | A comma separated list of CSP rules for `style-src` | `'self'` | +| `CSP_STYLE_SRC_ELEM` | A comma separated list of CSP rules for `style-src-elem` | `'self'` | +| `CSP_FONT_SRC` | A comma separated list of CSP rules for `font-src` | `'self'` | +| `CSP_CONNECT_SRC` | A comma separated list of CSP rules for `connect-src` | `'self'` | +| `CSP_MEDIA_SRC` | A comma separated list of CSP rules for `media-src` | `'self'` | +| `CSP_WORKER_SRC` | A comma separated list of CSP rules for `worker-src` | `'self'` | +| `CSP_FRAME_SRC` | A comma separated list of CSP rules for `frame-src` | `'self'` | +| `GA4_ID` | The Google Analytics 4 ID | _none_ | diff --git a/config/jinja2.py b/config/jinja2.py index ba8f36d..9a7f66f 100644 --- a/config/jinja2.py +++ b/config/jinja2.py @@ -5,6 +5,7 @@ from django.conf import settings from django.templatetags.static import static from django.urls import reverse + from jinja2 import Environment diff --git a/config/settings/base.py b/config/settings/base.py index 648a39c..793159c 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -3,6 +3,8 @@ from pathlib import Path from sysconfig import get_path +import dj_database_url + from config.util import strtobool PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -69,16 +71,8 @@ # Database # https://docs.djangoproject.com/en/5.0/ref/settings/#databases -DATABASES = { - "default": { - "ENGINE": "django.db.backends.postgresql", - "NAME": os.environ.get("DATABASE_NAME", ""), - "USER": os.environ.get("DATABASE_USER", ""), - "PASSWORD": os.environ.get("DATABASE_PASSWORD", ""), - "HOST": os.environ.get("DATABASE_HOST", ""), - "PORT": os.environ.get("DATABASE_PORT", "5432"), - } -} +DATABASES = {"default": dj_database_url.config(conn_max_age=600)} + # Password validation # https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators diff --git a/docker-compose.yml b/docker-compose.yml index 6ac7e21..84db35c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,11 +10,7 @@ services: - DJANGO_SETTINGS_MODULE=config.settings.develop - SECRET_KEY=abc123 - NPM_DEVELOP_COMMAND=dev - - DATABASE_NAME=postgres - - DATABASE_USER=postgres - - DATABASE_PASSWORD=postgres - - DATABASE_HOST=db - - DATABASE_PORT=5432 + - DATABASE_URL=postgres://postgres:postgres@db:5432/postgres - DJANGO_SUPERUSER_PASSWORD=admin - DJANGO_SUPERUSER_USERNAME=admin - COOKIE_DOMAIN=localhost diff --git a/poetry.lock b/poetry.lock index 27963e0..eefa0f3 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.8.3 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.8.4 and should not be changed by hand. [[package]] name = "asgiref" @@ -175,6 +175,21 @@ files = [ {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, ] +[[package]] +name = "dj-database-url" +version = "2.3.0" +description = "Use Database URLs in your Django Application." +optional = false +python-versions = "*" +files = [ + {file = "dj_database_url-2.3.0-py3-none-any.whl", hash = "sha256:bb0d414ba0ac5cd62773ec7f86f8cc378a9dbb00a80884c2fc08cc570452521e"}, + {file = "dj_database_url-2.3.0.tar.gz", hash = "sha256:ae52e8e634186b57e5a45e445da5dc407a819c2ceed8a53d1fac004cc5288787"}, +] + +[package.dependencies] +Django = ">=4.2" +typing_extensions = ">=3.10.0.0" + [[package]] name = "django" version = "5.1.3" @@ -547,6 +562,17 @@ files = [ [package.dependencies] flask = ">=2" +[[package]] +name = "typing-extensions" +version = "4.12.2" +description = "Backported and Experimental Type Hints for Python 3.8+" +optional = false +python-versions = ">=3.8" +files = [ + {file = "typing_extensions-4.12.2-py3-none-any.whl", hash = "sha256:04e5ca0351e0f3f85c6853954072df659d0d13fac324d0072316b67d7794700d"}, + {file = "typing_extensions-4.12.2.tar.gz", hash = "sha256:1a7ead55c7e559dd4dee8856e3a88b41225abfe1ce8df57b7c13915fe121ffb8"}, +] + [[package]] name = "tzdata" version = "2024.2" @@ -609,4 +635,4 @@ brotli = ["brotli"] [metadata] lock-version = "2.0" python-versions = "^3.12" -content-hash = "3d1f74893baf9828a54fd62027b1a21905804062388dd1adc847762b2a04505c" +content-hash = "1e775d099894291b1ecc41bb74ae73b89aab7c934b795b02132dc7b2d8013218" diff --git a/pyproject.toml b/pyproject.toml index e6eb205..7b7c2ab 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,6 +14,7 @@ django = "^5.0.7" psycopg2-binary = "^2.9.9" django-csp = "^3.8" whitenoise = "^6.7.0" +dj-database-url = "^2.3.0" [tool.poetry.group.dev] optional = true