Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
38 changes: 38 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# .flake8
[flake8]
# Core style
max-line-length = 100
max-complexity = 10
extend-ignore =
E203,W503,
D200,D212,D411,D415,
D203,D100,D104,D101
select = E,F,W,C90,D

# Docstrings (flake8-docstrings / pydocstyle)
docstring-convention = google

# Project hygiene
exclude =
.git,
__pycache__,
.venv,
./venv,
venv,
.tox,
.mypy_cache,
.pytest_cache,
node_modules,
build,
dist,
*/core/settings/utils.py,
*/core/settings/,
*.egg-info,
*/static/*,
*/media/*

# Per-file relaxations
per-file-ignores =
__init__.py:F401
core/settings/*.py:F401,F403,F405,D100
*/migrations/*:D100,D101,D102,D103,D104,D105,D106,D107
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
run: pip install flake8 flake8-docstrings pydoclint[flake8]

- name: Run flake8
run: flake8
run: flake8 --config=.flake8

build_image:
needs: flake
Expand Down
27 changes: 0 additions & 27 deletions .run/Run Server.run.xml

This file was deleted.

63 changes: 51 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
export COMPOSE_FILE=deployment/docker-compose.yml:deployment/docker-compose.override.yml
SHELL := /bin/bash

# Paths for dev override/template
OVERRIDE_PATH := deployment/docker-compose.override.yml
OVERRIDE_TEMPLATE_PATH := deployment/docker-compose.override.template.yml

ENV_PATH := deployment/.env
ENV_TEMPLATE_PATH := deployment/.template.env

.PHONY: build up dev down flake wait-db frontend-test dev-ci-test dev-entrypoint dev-initialize dev-runserver dev-test sleep ensure-dev-files

build:
@echo
@echo "------------------------------------------------------------------"
@echo "Building in production mode"
@echo "------------------------------------------------------------------"
@docker compose build

up:
up: ensure-dev-files
@echo
@echo "------------------------------------------------------------------"
@echo "Running in production mode"
@echo "------------------------------------------------------------------"
@docker compose ${ARGS} up -d nginx django
@docker compose $(ARGS) up -d nginx django

dev:
dev: ensure-dev-files
@echo
@echo "------------------------------------------------------------------"
@echo "Running in dev mode"
@echo "------------------------------------------------------------------"
@docker compose ${ARGS} up -d dev worker
@docker compose $(ARGS) up -d dev worker

down:
@echo
Expand All @@ -38,28 +47,27 @@ flake:
@flake8

wait-db:
@docker compose ${ARGS} exec -T db su - postgres -c "until pg_isready; do sleep 5; done"

@docker compose $(ARGS) exec -T db su - postgres -c "until pg_isready; do sleep 5; done"

frontend-test:
@docker compose exec -T dev sh -c "cd /home/web/django_project/frontend && npm install && npm test"
@docker compose $(ARGS) exec -T dev sh -c "cd /home/web/django_project/frontend && npm install && npm test"

# -----------------------------------------------------------------------------
# ----------------------------------- D E V -----------------------------------
# -----------------------------------------------------------------------------
dev-ci-test:
dev-ci-test: ensure-dev-files
@echo
@echo "------------------------------------------------------------------"
@echo "Running in DEVELOPMENT mode for CI test"
@echo "------------------------------------------------------------------"
@docker compose ${ARGS} up --no-recreate --no-deps -d db worker redis dev
@docker compose $(ARGS) up --no-recreate --no-deps -d db worker rabbitmq dev

dev-entrypoint:
@echo
@echo "------------------------------------------------------------------"
@echo "Running entrypoint.sh in DEVELOPMENT mode"
@echo "------------------------------------------------------------------"
@docker compose ${ARGS} exec -T dev "/home/web/django_project/entrypoint.sh"
@docker compose $(ARGS) exec -T dev "/home/web/django_project/entrypoint.sh"

dev-initialize:
@echo
Expand All @@ -80,11 +88,42 @@ dev-test:
@echo "------------------------------------------------------------------"
@echo "Run tests"
@echo "------------------------------------------------------------------"
@docker compose exec -T dev python manage.py test --keepdb --noinput
@docker compose $(ARGS) exec -T dev python manage.py test --keepdb --noinput

# -----------------------------------------------------------------------------
# --------------------------------- U T I L S ---------------------------------
# -----------------------------------------------------------------------------
sleep:
@sleep 50
@echo "Done"
@echo "Done"

# -----------------------------------------------------------------------------
# ---------------------------- P R E - F L I G H T -----------------------------
# -----------------------------------------------------------------------------
ensure-dev-files:
@echo
@echo "------------------------------------------------------------------"
@echo "Pre-flight: ensuring dev override and env files exist"
@echo "------------------------------------------------------------------"
@# Ensure deployment/docker-compose.override.yml
@if [ ! -f "$(OVERRIDE_PATH)" ]; then \
if [ -f "$(OVERRIDE_TEMPLATE_PATH)" ]; then \
cp "$(OVERRIDE_TEMPLATE_PATH)" "$(OVERRIDE_PATH)"; \
echo "Created $(OVERRIDE_PATH) from $(OVERRIDE_TEMPLATE_PATH)."; \
else \
echo "$(OVERRIDE_PATH) not found AND no template at $(OVERRIDE_TEMPLATE_PATH)."; \
fi; \
else \
echo "$(OVERRIDE_PATH) already exists; leaving it untouched."; \
fi
@# Ensure .env from .template.env
@if [ ! -f "$(ENV_PATH)" ]; then \
if [ -f "$(ENV_TEMPLATE_PATH)" ]; then \
cp "$(ENV_TEMPLATE_PATH)" "$(ENV_PATH)"; \
echo "Created .env from .template.env."; \
else \
echo ".env not found AND no .template.env present."; \
fi; \
else \
echo ".env already exists; leaving it untouched."; \
fi
4 changes: 2 additions & 2 deletions deployment/.template.env
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ DATABASE_USERNAME=docker
DATABASE_PASSWORD=docker
DATABASE_HOST=db

REDIS_HOST=redis
REDIS_PASSWORD=redis_password
RABBITMQ_DEFAULT_USER=guest
RABBITMQ_DEFAULT_PASS=password

RABBITMQ_HOST=rabbitmq
SENTRY_DSN=
4 changes: 2 additions & 2 deletions deployment/docker-compose.override.template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ services:
image: kartoza/${COMPOSE_PROJECT_NAME:-django_react_base}:dev
env_file:
- .env
entrypoint: [ "/home/web/django_project/entrypoint.dev.sh" ]
volumes:
- ../django_project:/home/web/django_project
- ./volumes/static:/home/web/static
Expand All @@ -82,7 +81,8 @@ services:
- "8001:22"
links:
- db
- redis
- rabbitmq
- memcached
- worker
- webpack
depends_on:
Expand Down
20 changes: 12 additions & 8 deletions deployment/docker-compose.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ services:
- INITIAL_FIXTURES=True
- SECRET_KEY=SECRET_KEY

# Redis config
- REDIS_HOST=redis
- REDIS_PASSWORD=redis_password
# Rabbitmq config
- RABBITMQ_DEFAULT_USER=guest
- RABBITMQ_DEFAULT_PASS=password
- RABBITMQ_HOST=rabbitmq

# Email where alters should be sent. This will be used by let's encrypt and as the django admin email.
- ADMIN_USERNAME=admin
Expand All @@ -40,15 +41,17 @@ services:
entrypoint: [ ]
links:
- db
- redis
- rabbitmq
- memcached
working_dir: /home/web/django_project

dev:
image: ${APP_IMAGE}:dev
container_name: "django_react_base-dev"
links:
- db
- redis
- rabbitmq
- memcached
- worker
volumes:
- media-data:/home/web/media
Expand All @@ -66,9 +69,10 @@ services:
- INITIAL_FIXTURES=True
- SECRET_KEY=SECRET_KEY

# Redis config
- REDIS_HOST=redis
- REDIS_PASSWORD=redis_password
# Rabbitmq config
- RABBITMQ_DEFAULT_USER=guest
- RABBITMQ_DEFAULT_PASS=password
- RABBITMQ_HOST=rabbitmq

# Email where alters should be sent. This will be used by let's encrypt and as the django admin email.
- ADMIN_USERNAME=admin
Expand Down
30 changes: 24 additions & 6 deletions deployment/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,21 @@ x-common-django:
restart: on-failure

services:
redis:
image: bitnamilegacy/redis:8.2.1
rabbitmq:
image: rabbitmq:3-management
hostname: rabbitmq
environment:
- REDIS_PASSWORD=${REDIS_PASSWORD:-redis_password}
- RABBITMQ_DEFAULT_USER=${RABBITMQ_DEFAULT_USER:-guest}
- RABBITMQ_DEFAULT_PASS=${RABBITMQ_DEFAULT_PASS:-guest}
ports:
- "5672:5672"
- "15672:15672" # Management UI (optional)
restart: on-failure

memcached:
image: memcached:1.6-alpine
command: ["memcached", "-m", "128", "-I", "5m"]
restart: on-failure

db:
image: kartoza/postgis:17-3.5
Expand Down Expand Up @@ -58,25 +69,32 @@ services:
django:
<<: *default-common-django
command: 'uwsgi --ini /uwsgi.conf'
depends_on:
- db
- rabbitmq
- memcached
links:
- db
- worker
- rabbitmq
- memcached

worker:
<<: *default-common-django
entrypoint: []
command: 'celery -A core worker -l info'
links:
- db
- redis
- rabbitmq
- memcached

celery_beat:
<<: *default-common-django
entrypoint: [ ]
command: 'celery -A core beat --loglevel=info --scheduler django_celery_beat.schedulers:DatabaseScheduler'
links:
- db
- redis
- rabbitmq
- memcached

nginx:
image: nginx
Expand Down
3 changes: 1 addition & 2 deletions deployment/docker/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,4 @@ psycopg2-binary==2.9.10
# This extension enables you to store the periodic task schedule in the database.
django-celery-beat==2.8.1

# Python client for Redis database and key-value store
redis==6.4.0
pymemcache>=4.0
1 change: 1 addition & 0 deletions django_project/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# __init__.py
3 changes: 3 additions & 0 deletions django_project/core/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# core/__init__.py
from .celery import app as celery_app
__all__ = ("celery_app",)
8 changes: 0 additions & 8 deletions django_project/core/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@
# this is also used in manage.py
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')

# Get the base REDIS URL, default to redis' default
BASE_REDIS_URL = (
f'redis://default:{os.environ.get("REDIS_PASSWORD", "")}'
f'@{os.environ.get("REDIS_HOST", "")}',
)

app = Celery('django_react_base')

# Using a string here means the worker don't have to serialize
Expand All @@ -25,7 +19,5 @@
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()

app.conf.broker_url = BASE_REDIS_URL

# this allows you to schedule items in the Django admin.
app.conf.beat_scheduler = 'django_celery_beat.schedulers.DatabaseScheduler'
22 changes: 8 additions & 14 deletions django_project/core/context_processors.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
"""Context processors."""
from django.conf import settings
from django.http import HttpRequest


def sentry_dsn(request):
"""
Return the Sentry DSN setting.
def sentry_dsn(request: HttpRequest) -> dict[str, str]:
"""Inject Sentry DSN into the template context.

Parameters
----------
request : django.http.HttpRequest
The incoming HTTP request (not used in this function).
Args:
request: Incoming HTTP request (unused).

Returns
-------
dict
Dictionary containing the Sentry DSN with key ``"SENTRY_DSN"``.
Returns:
dict[str, str]: Mapping with the ``"SENTRY_DSN"`` key.
"""
return {
'SENTRY_DSN': settings.SENTRY_DSN
}
return {"SENTRY_DSN": settings.SENTRY_DSN}
Loading