Skip to content

Commit

Permalink
Use ruff, django5.1, poetry@2 (#2768)
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn authored Jan 14, 2025
1 parent ada0f2c commit 6be1d6e
Show file tree
Hide file tree
Showing 27 changed files with 1,003 additions and 1,462 deletions.
4 changes: 2 additions & 2 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ include:
# Building template project:
mirror-github-build:
stage: build
image: python:3.11.9
image: python:3.12.7
variables:
POETRY_VERSION: '1.8.3'
POETRY_VERSION: '2.0.1'
before_script:
# Installing poetry:
- curl -sSL 'https://install.python-poetry.org' | python -
Expand Down
6 changes: 0 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ repos:
hooks:
- id: shellcheck
args: ["--severity=style"]
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.9.1
hooks:
- id: ruff
args: ["--exit-non-zero-on-fix"]
- id: ruff-format

# Should be the last:
- repo: meta
Expand Down
19 changes: 10 additions & 9 deletions .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,24 @@ version: 2
build:
os: ubuntu-22.04
tools:
python: '3.11'
python: '3.12'
jobs:
# See https://github.com/readthedocs/readthedocs.org/issues/4912
pre_create_environment:
# Select Python version (keep in sync with other versions):
- asdf install python 3.11.9
- asdf global python 3.11.9
- python -m pip install --upgrade --no-cache-dir virtualenv

# Install poetry:
- asdf plugin add poetry
- asdf install poetry latest
- asdf global poetry latest

# Go to the project's folder and export requirements:
- cd '{{cookiecutter.project_name}}'
- poetry config virtualenvs.create false
post_install:
# We build django with `DJANGO_ENV=production`, so no dev deps:
- . "$READTHEDOCS_VIRTUALENV_PATH/bin/activate" && cd '{{cookiecutter.project_name}}' && poetry install --only main --only docs
- poetry self add poetry-plugin-export
- poetry export --only main --only docs --format=requirements.txt --output=requirements.txt

python:
install:
- requirements: requirements.txt

sphinx:
configuration: '{{cookiecutter.project_name}}/docs/conf.py'
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@



Bleeding edge `django4.2` template focused on code quality and security.
Bleeding edge `django5.1` template focused on code quality and security.

---

Expand All @@ -22,11 +22,11 @@ Just like `django-admin.py startproject` but better.
## Features

- Always [`up-to-date`](https://github.com/wemake-services/wemake-django-template/pulls?utf8=%E2%9C%93&q=is%3Apr%20author%3Aapp%2Fdependabot) with the help of [`@dependabot`](https://dependabot.com/)
- Supports latest `python3.11+`
- [`poetry`](https://github.com/python-poetry/poetry) for managing dependencies
- Supports latest `python3.12`
- [`poetry@2`](https://github.com/python-poetry/poetry) for managing dependencies
- [`mypy`](https://mypy.readthedocs.io) and [`django-stubs`](https://github.com/typeddjango/django-stubs) for static typing
- [`pytest`](https://pytest.org/) and [`hypothesis`](https://github.com/HypothesisWorks/hypothesis) for unit tests
- [`flake8`](http://flake8.pycqa.org/en/latest/) and [`wemake-python-styleguide`](https://wemake-python-styleguide.readthedocs.io/en/latest/) for linting
- [`ruff`](https://docs.astral.sh/ruff) and [`wemake-python-styleguide`](https://wemake-python-styleguide.readthedocs.io/en/latest/) for linting
- [`docker`](https://www.docker.com/) for development, testing, and production
- [`sphinx`](http://www.sphinx-doc.org/en/master/) for documentation
- [`Gitlab CI`](https://about.gitlab.com/gitlab-ci/) with full `build`, `test`, and `deploy` [pipeline configured by default](https://gitlab.com/sobolevn/wemake-django-template/-/pipelines)
Expand Down
4 changes: 3 additions & 1 deletion tests/test_project_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ def test_pyproject_toml(
path = baked_project.project_path / 'pyproject.toml'

pyproject = tomli.loads(path.read_text())
project = pyproject['tool']['poetry']
project = pyproject['project']
poetry = pyproject['tool']['poetry']

assert project['name'] == context['project_name']
assert project['description'] == context['project_verbose_name']
assert poetry


@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion {{cookiecutter.project_name}}/.python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.11.9
3.12.7
14 changes: 7 additions & 7 deletions {{cookiecutter.project_name}}/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ services:
image: "postgres:17-alpine"
restart: unless-stopped
volumes:
- pgdata:/var/lib/postgresql/data
- postgres-data:/var/lib/postgresql/data
networks:
- postgresnet
- postgres-net
env_file: ./config/.env

web:
Expand All @@ -37,8 +37,8 @@ services:
depends_on:
- db
networks:
- webnet
- postgresnet
- web-net
- postgres-net
env_file: ./config/.env
environment:
DJANGO_DATABASE_HOST: db
Expand All @@ -58,10 +58,10 @@ services:

networks:
# Network for postgres, use it for services that need access to the db:
postgresnet:
postgres-net:
# Network for your internals, use it by default:
webnet:
web-net:

volumes:
pgdata:
postgres-data:
django-static:
13 changes: 4 additions & 9 deletions {{cookiecutter.project_name}}/docker/django/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# This Dockerfile uses multi-stage build to customize DEV and PROD images:
# https://docs.docker.com/develop/develop-images/multistage-build/

FROM python:3.11.9-slim-bookworm AS development_build
FROM python:3.12.7-slim-bookworm AS development_build

LABEL maintainer="{{ cookiecutter.project_domain }}"
LABEL vendor="{{ cookiecutter.project_domain }}"
Expand All @@ -24,20 +24,16 @@ ENV DJANGO_ENV=${DJANGO_ENV} \
PIP_DISABLE_PIP_VERSION_CHECK=1 \
PIP_DEFAULT_TIMEOUT=100 \
PIP_ROOT_USER_ACTION=ignore \
PIP_CONSTRAINT='/var/cache/pip_constraint.txt' \
# tini:
TINI_VERSION=v0.19.0 \
# poetry:
POETRY_VERSION=1.8.3 \
POETRY_VERSION=2.0.1 \
POETRY_VIRTUALENVS_CREATE=false \
POETRY_CACHE_DIR='/var/cache/pypoetry' \
POETRY_HOME='/usr/local'

SHELL ["/bin/bash", "-eo", "pipefail", "-c"]

# Copy constrains for pip:
COPY docker/django/pip_constraint.txt /var/cache/pip_constraint.txt

# System deps (we don't use exact versions because it is hard to update them,
# pin when needed):
# hadolint ignore=DL3008
Expand Down Expand Up @@ -82,11 +78,10 @@ COPY --chown=web:web ./poetry.lock ./pyproject.toml /code/
# hadolint ignore=SC2046
RUN --mount=type=cache,target="$POETRY_CACHE_DIR" \
echo "$DJANGO_ENV" \
&& poetry run pip install --constraint="$PIP_CONSTRAINT" pip \
&& poetry --version && poetry run pip --version \
&& poetry install \
&& poetry sync \
$(if [ "$DJANGO_ENV" = 'production' ]; then echo '--only main'; fi) \
--no-interaction --no-ansi --sync
--no-interaction --no-ansi --compile

# This is a special case. We need to run this script as an entry point:
COPY ./docker/django/entrypoint.sh /docker-entrypoint.sh
Expand Down
4 changes: 3 additions & 1 deletion {{cookiecutter.project_name}}/docker/django/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ run_ci () {
dotenv-linter config/.env config/.env.template

# Running linting for all python files in the project:
ruff check --exit-non-zero-on-fix --diff
ruff format --check --diff
flake8 .

# Linl HTML formatting:
djlint --check server
djlint --lint server

# Running type checking, see https://github.com/typeddjango/django-stubs
mypy manage.py server tests
mypy .

# Running tests:
pytest
Expand Down

This file was deleted.

6 changes: 3 additions & 3 deletions {{cookiecutter.project_name}}/docker/docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ services:
depends_on:
- web
networks:
- proxynet
- proxy-net

web:
<<: &web # Image for production:
Expand All @@ -42,7 +42,7 @@ services:

command: bash ./docker/django/gunicorn.sh
networks:
- proxynet
- proxy-net
expose:
- 8000

Expand All @@ -56,7 +56,7 @@ services:
networks:
# Network for your proxy server and application to connect them,
# do not use it for anything else!
proxynet:
proxy-net:

volumes:
django-media:
Expand Down
36 changes: 10 additions & 26 deletions {{cookiecutter.project_name}}/docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@

import os
import sys
from pathlib import Path
from typing import cast

import django
import tomli

# We need `server` to be importable from here:
sys.path.insert(0, os.path.abspath('..'))
_ROOT = Path('..').resolve(strict=True)
sys.path.insert(0, str(_ROOT))

# Django setup, all deps must be present to succeed:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'server.settings')
Expand All @@ -31,12 +33,13 @@

# -- Project information -----------------------------------------------------


def _get_project_meta() -> dict[str, str]: # lying about return type
with open('../pyproject.toml', mode='rb') as pyproject:
return cast(
dict[str, str],
tomli.load(pyproject)['tool']['poetry'],
)
pyproject = _ROOT / 'pyproject.toml'
return cast(
dict[str, str],
tomli.loads(pyproject.read_text())['project'],
)


pkg_meta = _get_project_meta()
Expand All @@ -50,9 +53,6 @@ def _get_project_meta() -> dict[str, str]: # lying about return type

# -- General configuration ------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
needs_sphinx = '7.2'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
Expand All @@ -64,10 +64,8 @@ def _get_project_meta() -> dict[str, str]: # lying about return type
'sphinx.ext.viewcode',
'sphinx.ext.githubpages',
'sphinx.ext.napoleon',

# https://github.com/executablebooks/MyST-Parser
'myst_parser',

# 3rd party, order matters:
# https://github.com/wemake-services/wemake-django-template/issues/159
'sphinx_autodoc_typehints',
Expand Down Expand Up @@ -111,23 +109,9 @@ def _get_project_meta() -> dict[str, str]: # lying about return type

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
html_theme = 'alabaster'
html_theme = 'furo'

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']

# Custom sidebar templates, must be a dictionary that maps document names
# to template names.
#
# This is required for the alabaster theme
# refs: http://alabaster.readthedocs.io/en/latest/installation.html#sidebars
html_sidebars = {
'**': [
'about.html',
'navigation.html',
'moreinfo.html',
'searchbox.html',
],
}
10 changes: 5 additions & 5 deletions {{cookiecutter.project_name}}/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ def main() -> None:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'server.settings')

try:
from django.core import management # noqa: WPS433
from django.core import management # noqa: PLC0415
except ImportError:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and " +
'available on your PYTHONPATH environment variable? Did you ' +
'forget to activate a virtual environment?',
)
"Couldn't import Django. Are you sure it's installed and "
+ 'available on your PYTHONPATH environment variable? Did you '
+ 'forget to activate a virtual environment?',
) from None

management.execute_from_command_line(sys.argv)

Expand Down
Loading

0 comments on commit 6be1d6e

Please sign in to comment.