Skip to content

Commit

Permalink
Merge pull request #3 from evrone/support/update-template
Browse files Browse the repository at this point in the history
Update template with Django 5 and PostreSQL 16
  • Loading branch information
vokson committed Apr 8, 2024
2 parents a77ed3e + 613a14d commit b57760b
Show file tree
Hide file tree
Showing 14 changed files with 2,340 additions and 542 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
Template for Django projects based on:
- Simplicity
- Purity
- [Two Scoops of Django](https://www.feldroy.com/books/two-scoops-of-django-3-x)
- [Two Scoops of Django 3.X](https://daniel.feldroy.com/books/tech)
- [The Twelve-Factor App](https://12factor.net)


## Features
- [`poetry`](https://python-poetry.org) modern Python package manager.
- [OpenAPI](https://spec.openapis.org/oas/latest.html) endpoint at `/openapi`
- [`.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.
- [`pytest`](https://docs.pytest.org/en/6.2.x/) with basic setup
- [`pytest`](https://docs.pytest.org/en/8.0.x/) with basic setup
- Basic dependencies such `django`, `djangorestframework` etc.
- `docker-compose` infrastructure file with database ([PostgreSQL](https://www.postgresql.org)) and cache ([Redis](https://redis.io)).
- `docker-compose` infrastructure file with database ([PostgreSQL](https://www.postgresql.org)).
- [mailhog](https://github.com/mailhog/MailHog) for testing emails
- Dockerfile for application deployment
- Settings based on environment variables.
Expand Down
3 changes: 3 additions & 0 deletions {{cookiecutter.project_name}}/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,6 @@ dmypy.json

# PyCharm
.idea

# Static files
/static
2 changes: 1 addition & 1 deletion {{cookiecutter.project_name}}/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
default_language_version:
python: python3.9
python: python3.12

repos:
- repo: local
Expand Down
19 changes: 14 additions & 5 deletions {{cookiecutter.project_name}}/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
FROM python:3.9
FROM python:3.12-slim

ENV PYTHONUNBUFFERED=1
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1

COPY /scripts/wait-for-it.sh /wait-for-it.sh
RUN chmod +x wait-for-it.sh

COPY /scripts/start.sh /start.sh
RUN chmod +x /start.sh

COPY /scripts/start-dev.sh /start-dev.sh
RUN chmod +x /start-dev.sh

COPY poetry.lock pyproject.toml /
RUN pip install poetry==1.1.6 \
RUN pip install poetry==1.8.2 \
&& poetry config virtualenvs.create false \
&& poetry install --no-dev --no-root

WORKDIR /app
COPY app docker-entrypoint.sh ./

CMD ["./docker-entrypoint.sh"]
COPY app ./
78 changes: 78 additions & 0 deletions {{cookiecutter.project_name}}/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
.PHONY: static

docker_compose_path = "docker-compose.yml"

DC = docker-compose -f $(docker_compose_path)
DC_RUN = $(DC) run --rm
DC_RUN_MNG = $(DC_RUN) django python manage.py

###################################################################################################
## Build/run/stop/etc. ############################################################################

createsuperuser:
$(DC_RUN_MNG) createsuperuser

static:
$(DC_RUN_MNG) collectstatic --no-input

build:
$(DC) build --no-cache
make static

up:
$(DC) up -d --build

down:
$(DC) down

restart:
$(DC) restart

###################################################################################################
## Databases ######################################################################################

migrations: # produce migrations from code
$(DC_RUN_MNG) makemigrations

migrate: # apply db migrations
$(DC_RUN_MNG) migrate

###################################################################################################
## Testing ########################################################################################

# Run tests locally. All pytest args can be specified using `pytest-args`:
# $ make tests pytest-args='-k test_name' # Run specific test.
# $ make tests pytest-args='--ff -x' # Run all tests. Stop if any fail. Rerun last failed if any.

test:
@echo 'Running tests with arguments: '$(pytest-args)
$(DC_RUN) django poetry run pytest --ds=settings.test -v $(pytest-args) .

test-coverage:
@echo 'Running test coverage with arguments: '$(pytest-args)
$(DC_RUN) django pytest --ds=settings.test --cov=. --cov-report=term-missing --cov-config=../pyproject.toml -c ../pyproject.toml .

###################################################################################################
## Code tools #####################################################################################

install-pre-commit:
poetry run pre-commit install

check:
poetry run black --check backend
poetry run isort --check backend

format:
poetry run black .
poetry run isort .

lint:
poetry run black --check backend
poetry run isort --check backend
poetry run flake8 --inline-quotes '"'
@# For some reason, mypy and pylint fails to resolve PYTHONPATH, set manually.
PYTHONPATH=./app poetry run pylint app
PYTHONPATH=./app poetry run mypy --namespace-packages --show-error-codes app --check-untyped-defs --ignore-missing-imports --show-traceback

safety:
poetry run safety check --policy-file=.safety-policy.yml
8 changes: 4 additions & 4 deletions {{cookiecutter.project_name}}/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
* Install [Docker](https://docs.docker.com/engine/install/)
* Install [docker-compose](https://docs.docker.com/compose/install/)
* Run command `poetry install && poetry shell`
* Run command `pre-commit install`
* Run command `docker-compose up -d`
* Run command `python manage.py migrate`
* Run command `python manage.py runserver`
* Run command `make install-pre-commit`
* Run command `make up`
* Run command `make migrate`
* Run command `make createsuperuser`


## How to deploy
Expand Down
12 changes: 7 additions & 5 deletions {{cookiecutter.project_name}}/app/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]


ROOT_URLCONF = "settings.urls"

TEMPLATES = [
Expand All @@ -61,11 +62,11 @@

DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"ENGINE": "django.db.backends.postgresql",
"NAME": env.str("DB_NAME", "{{cookiecutter.project_name}}"),
"USER": env.str("DB_USER", "{{cookiecutter.project_name}}"),
"PASSWORD": env.str("DB_PASSWORD", "{{cookiecutter.project_name}}"),
"HOST": env.str("DB_HOST", "localhost"),
"HOST": env.str("DB_HOST", "db"),
"PORT": env.str("DB_PORT", "5432"),
}
}
Expand Down Expand Up @@ -93,9 +94,10 @@

USE_I18N = True

USE_L10N = True

USE_TZ = True


STATIC_URL = "/static/"
STATIC_ROOT = BASE_DIR.parent / "static"
STATIC_APP = BASE_DIR / "static"

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
15 changes: 15 additions & 0 deletions {{cookiecutter.project_name}}/app/settings/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from settings.base import *

DEBUG = True

ALLOWED_HOSTS = ["*"]

EMAIL_HOST = 'localhost'
EMAIL_PORT = '1025'

SECRET_KEY = "test-a-valid-secret-key" # noqa: S105

PASSWORD_HASHERS = [
"django.contrib.auth.hashers.MD5PasswordHasher",
]

21 changes: 14 additions & 7 deletions {{cookiecutter.project_name}}/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
version: "3.9"

services:
database:
image: postgres:13.2
db:
image: postgres:16
container_name: {{cookiecutter.project_name}}-database
restart: always
volumes:
Expand All @@ -14,17 +14,24 @@ services:
ports:
- "5432:5432"

cache:
image: redis:6.2.5
container_name: {{cookiecutter.project_name}}-cache
django:
build:
context: .
restart: always
volumes:
- ./app:/app
- ./static:/static
depends_on:
- db
working_dir: /app
command: [ "/wait-for-it.sh", "db:5432", "--", "../start-dev.sh" ]
ports:
- "6379:6379"
- "8000:8000"

# Email testing
mailhog:
image: mailhog/mailhog:v1.0.1
container_name: { { cookiecutter.project_name } }-mailhog
container_name: {{ cookiecutter.project_name }}-mailhog
volumes:
- ./docker-maildir:/maildir
command: [ "-storage=maildir", "-maildir-path=/maildir" ]
Expand Down
Loading

0 comments on commit b57760b

Please sign in to comment.