Skip to content

Commit b57760b

Browse files
authored
Merge pull request #3 from evrone/support/update-template
Update template with Django 5 and PostreSQL 16
2 parents a77ed3e + 613a14d commit b57760b

14 files changed

+2340
-542
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
Template for Django projects based on:
88
- Simplicity
99
- Purity
10-
- [Two Scoops of Django](https://www.feldroy.com/books/two-scoops-of-django-3-x)
10+
- [Two Scoops of Django 3.X](https://daniel.feldroy.com/books/tech)
1111
- [The Twelve-Factor App](https://12factor.net)
1212

1313

1414
## Features
1515
- [`poetry`](https://python-poetry.org) modern Python package manager.
1616
- [OpenAPI](https://spec.openapis.org/oas/latest.html) endpoint at `/openapi`
1717
- [`.pre-commit`](https://pre-commit.com) hooks with predefined [`black`]([`poetry`](https://python-poetry.org) modern Python package manager.) and [`isort`](https://github.com/PyCQA/isort) configs.
18-
- [`pytest`](https://docs.pytest.org/en/6.2.x/) with basic setup
18+
- [`pytest`](https://docs.pytest.org/en/8.0.x/) with basic setup
1919
- Basic dependencies such `django`, `djangorestframework` etc.
20-
- `docker-compose` infrastructure file with database ([PostgreSQL](https://www.postgresql.org)) and cache ([Redis](https://redis.io)).
20+
- `docker-compose` infrastructure file with database ([PostgreSQL](https://www.postgresql.org)).
2121
- [mailhog](https://github.com/mailhog/MailHog) for testing emails
2222
- Dockerfile for application deployment
2323
- Settings based on environment variables.

{{cookiecutter.project_name}}/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,6 @@ dmypy.json
125125

126126
# PyCharm
127127
.idea
128+
129+
# Static files
130+
/static

{{cookiecutter.project_name}}/.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
default_language_version:
2-
python: python3.9
2+
python: python3.12
33

44
repos:
55
- repo: local
+14-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
FROM python:3.9
1+
FROM python:3.12-slim
22

3-
ENV PYTHONUNBUFFERED=1
3+
ENV PYTHONUNBUFFERED 1
4+
ENV PYTHONDONTWRITEBYTECODE 1
5+
6+
COPY /scripts/wait-for-it.sh /wait-for-it.sh
7+
RUN chmod +x wait-for-it.sh
8+
9+
COPY /scripts/start.sh /start.sh
10+
RUN chmod +x /start.sh
11+
12+
COPY /scripts/start-dev.sh /start-dev.sh
13+
RUN chmod +x /start-dev.sh
414

515
COPY poetry.lock pyproject.toml /
6-
RUN pip install poetry==1.1.6 \
16+
RUN pip install poetry==1.8.2 \
717
&& poetry config virtualenvs.create false \
818
&& poetry install --no-dev --no-root
919

1020
WORKDIR /app
11-
COPY app docker-entrypoint.sh ./
1221

13-
CMD ["./docker-entrypoint.sh"]
22+
COPY app ./
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
.PHONY: static
2+
3+
docker_compose_path = "docker-compose.yml"
4+
5+
DC = docker-compose -f $(docker_compose_path)
6+
DC_RUN = $(DC) run --rm
7+
DC_RUN_MNG = $(DC_RUN) django python manage.py
8+
9+
###################################################################################################
10+
## Build/run/stop/etc. ############################################################################
11+
12+
createsuperuser:
13+
$(DC_RUN_MNG) createsuperuser
14+
15+
static:
16+
$(DC_RUN_MNG) collectstatic --no-input
17+
18+
build:
19+
$(DC) build --no-cache
20+
make static
21+
22+
up:
23+
$(DC) up -d --build
24+
25+
down:
26+
$(DC) down
27+
28+
restart:
29+
$(DC) restart
30+
31+
###################################################################################################
32+
## Databases ######################################################################################
33+
34+
migrations: # produce migrations from code
35+
$(DC_RUN_MNG) makemigrations
36+
37+
migrate: # apply db migrations
38+
$(DC_RUN_MNG) migrate
39+
40+
###################################################################################################
41+
## Testing ########################################################################################
42+
43+
# Run tests locally. All pytest args can be specified using `pytest-args`:
44+
# $ make tests pytest-args='-k test_name' # Run specific test.
45+
# $ make tests pytest-args='--ff -x' # Run all tests. Stop if any fail. Rerun last failed if any.
46+
47+
test:
48+
@echo 'Running tests with arguments: '$(pytest-args)
49+
$(DC_RUN) django poetry run pytest --ds=settings.test -v $(pytest-args) .
50+
51+
test-coverage:
52+
@echo 'Running test coverage with arguments: '$(pytest-args)
53+
$(DC_RUN) django pytest --ds=settings.test --cov=. --cov-report=term-missing --cov-config=../pyproject.toml -c ../pyproject.toml .
54+
55+
###################################################################################################
56+
## Code tools #####################################################################################
57+
58+
install-pre-commit:
59+
poetry run pre-commit install
60+
61+
check:
62+
poetry run black --check backend
63+
poetry run isort --check backend
64+
65+
format:
66+
poetry run black .
67+
poetry run isort .
68+
69+
lint:
70+
poetry run black --check backend
71+
poetry run isort --check backend
72+
poetry run flake8 --inline-quotes '"'
73+
@# For some reason, mypy and pylint fails to resolve PYTHONPATH, set manually.
74+
PYTHONPATH=./app poetry run pylint app
75+
PYTHONPATH=./app poetry run mypy --namespace-packages --show-error-codes app --check-untyped-defs --ignore-missing-imports --show-traceback
76+
77+
safety:
78+
poetry run safety check --policy-file=.safety-policy.yml

{{cookiecutter.project_name}}/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
* Install [Docker](https://docs.docker.com/engine/install/)
88
* Install [docker-compose](https://docs.docker.com/compose/install/)
99
* Run command `poetry install && poetry shell`
10-
* Run command `pre-commit install`
11-
* Run command `docker-compose up -d`
12-
* Run command `python manage.py migrate`
13-
* Run command `python manage.py runserver`
10+
* Run command `make install-pre-commit`
11+
* Run command `make up`
12+
* Run command `make migrate`
13+
* Run command `make createsuperuser`
1414

1515

1616
## How to deploy

{{cookiecutter.project_name}}/app/settings/base.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"django.middleware.clickjacking.XFrameOptionsMiddleware",
3939
]
4040

41+
4142
ROOT_URLCONF = "settings.urls"
4243

4344
TEMPLATES = [
@@ -61,11 +62,11 @@
6162

6263
DATABASES = {
6364
"default": {
64-
"ENGINE": "django.db.backends.postgresql_psycopg2",
65+
"ENGINE": "django.db.backends.postgresql",
6566
"NAME": env.str("DB_NAME", "{{cookiecutter.project_name}}"),
6667
"USER": env.str("DB_USER", "{{cookiecutter.project_name}}"),
6768
"PASSWORD": env.str("DB_PASSWORD", "{{cookiecutter.project_name}}"),
68-
"HOST": env.str("DB_HOST", "localhost"),
69+
"HOST": env.str("DB_HOST", "db"),
6970
"PORT": env.str("DB_PORT", "5432"),
7071
}
7172
}
@@ -93,9 +94,10 @@
9394

9495
USE_I18N = True
9596

96-
USE_L10N = True
97-
9897
USE_TZ = True
9998

100-
10199
STATIC_URL = "/static/"
100+
STATIC_ROOT = BASE_DIR.parent / "static"
101+
STATIC_APP = BASE_DIR / "static"
102+
103+
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from settings.base import *
2+
3+
DEBUG = True
4+
5+
ALLOWED_HOSTS = ["*"]
6+
7+
EMAIL_HOST = 'localhost'
8+
EMAIL_PORT = '1025'
9+
10+
SECRET_KEY = "test-a-valid-secret-key" # noqa: S105
11+
12+
PASSWORD_HASHERS = [
13+
"django.contrib.auth.hashers.MD5PasswordHasher",
14+
]
15+

{{cookiecutter.project_name}}/docker-compose.yml

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
version: "3.9"
22

33
services:
4-
database:
5-
image: postgres:13.2
4+
db:
5+
image: postgres:16
66
container_name: {{cookiecutter.project_name}}-database
77
restart: always
88
volumes:
@@ -14,17 +14,24 @@ services:
1414
ports:
1515
- "5432:5432"
1616

17-
cache:
18-
image: redis:6.2.5
19-
container_name: {{cookiecutter.project_name}}-cache
17+
django:
18+
build:
19+
context: .
2020
restart: always
21+
volumes:
22+
- ./app:/app
23+
- ./static:/static
24+
depends_on:
25+
- db
26+
working_dir: /app
27+
command: [ "/wait-for-it.sh", "db:5432", "--", "../start-dev.sh" ]
2128
ports:
22-
- "6379:6379"
29+
- "8000:8000"
2330

2431
# Email testing
2532
mailhog:
2633
image: mailhog/mailhog:v1.0.1
27-
container_name: { { cookiecutter.project_name } }-mailhog
34+
container_name: {{ cookiecutter.project_name }}-mailhog
2835
volumes:
2936
- ./docker-maildir:/maildir
3037
command: [ "-storage=maildir", "-maildir-path=/maildir" ]

0 commit comments

Comments
 (0)