A1: Django skeleton with web and worker processes (ADR-54, ADR-55, WEB-01, WEB-07, ARC-70..74)

Django 5.2 project (config/, apps/accounts with a custom User and
case-insensitive usernames, apps/lottery with the run_worker command,
apps/web with a placeholder home and /health/live, apps/ops), SQLite in WAL
with IMMEDIATE transactions, secrets and hosts from the environment. The
compose stack is now web + worker + caddy and serves https://localhost; the
old FastAPI service is out of the stack until roadmap D1. The three old
infrastructure tests follow the new Dockerfile and compose.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
davideandClaude Sonnet 5.5 committed 2026-11-16 12:00:57 +01:00
1 parent 79f7ebe53e
commit fc23a75c9b
43 files changed
+709 -61

No files matched your search

View File
Whitespace-only changes.
View File
Whitespace-only changes.
+6
View File
@@ -0,0 +1,6 @@
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import User
admin.site.register(User, UserAdmin)
+6
View File
@@ -0,0 +1,6 @@
from django.apps import AppConfig
class AccountsConfig(AppConfig):
name = "apps.accounts"
label = "accounts"
+43
View File
@@ -0,0 +1,43 @@
# Generated by Django 5.2.19 on 2026-11-16 10:51
import django.contrib.auth.models
import django.contrib.auth.validators
import django.db.models.functions.text
import django.utils.timezone
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
]
operations = [
migrations.CreateModel(
name='User',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('password', models.CharField(max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')),
('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')),
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')),
('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')),
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')),
],
options={
'constraints': [models.UniqueConstraint(django.db.models.functions.text.Lower('username'), name='accounts_user_username_ci_unique')],
},
managers=[
('objects', django.contrib.auth.models.UserManager()),
],
),
]
Whitespace-only changes.
+16
View File
@@ -0,0 +1,16 @@
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.db.models.functions import Lower
class User(AbstractUser):
"""Players and staff share one account model (ADR-54, ADR-56).
Staff is a role (`is_staff` plus explicit permissions), not a separate table.
"""
class Meta:
constraints = [
# ADR-38: one case-insensitive namespace. The name is stored as typed.
models.UniqueConstraint(Lower("username"), name="accounts_user_username_ci_unique"),
]
View File
Whitespace-only changes.
+6
View File
@@ -0,0 +1,6 @@
from django.apps import AppConfig
class LotteryConfig(AppConfig):
name = "apps.lottery"
label = "lottery"
View File
Whitespace-only changes.
Whitespace-only changes.
@@ -0,0 +1,39 @@
"""The background worker process (ADR-55).
A1 ships the process shell only: it starts, logs, stops cleanly on SIGTERM and shows up as a
compose service. The round scheduler (A4) and the chain tasks (Phase B) plug into `tick`.
"""
import logging
import signal
import threading
from django.core.management.base import BaseCommand
logger = logging.getLogger(__name__)
TICK_SECONDS = 5
def tick() -> None:
"""One pass over everything the worker is responsible for. Empty until A4."""
class Command(BaseCommand):
help = "Run the background worker (round scheduler and chain tasks)."
def add_arguments(self, parser):
parser.add_argument("--once", action="store_true", help="Run a single tick and exit.")
def handle(self, *args, **options):
stop = threading.Event()
for signum in (signal.SIGTERM, signal.SIGINT):
signal.signal(signum, lambda *_: stop.set())
logger.info("worker started")
while not stop.is_set():
tick()
if options["once"]:
break
stop.wait(TICK_SECONDS)
logger.info("worker stopped")
View File
Whitespace-only changes.
+6
View File
@@ -0,0 +1,6 @@
from django.apps import AppConfig
class OpsConfig(AppConfig):
name = "apps.ops"
label = "ops"
View File
Whitespace-only changes.
+6
View File
@@ -0,0 +1,6 @@
from django.apps import AppConfig
class WebConfig(AppConfig):
name = "apps.web"
label = "web"
+14
View File
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="it">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>PLM Lottery</title>
</head>
<body>
<main>
<h1>PLM Lottery</h1>
<p>Il sito è in costruzione.</p>
</main>
</body>
</html>
+9
View File
@@ -0,0 +1,9 @@
from django.urls import path
from . import views
app_name = "web"
urlpatterns = [
path("", views.home, name="home"),
path("health/live", views.health_live, name="health-live"),
]
+18
View File
@@ -0,0 +1,18 @@
from django.db import connection
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render
def home(request):
"""Placeholder landing page: the real home (WEB-10) arrives with A6."""
return render(request, "web/home.html")
def health_live(request):
"""Liveness for the container healthcheck: the process answers and the database opens (ARC-73)."""
try:
with connection.cursor() as cursor:
cursor.execute("SELECT 1")
except Exception:
return HttpResponse("database unavailable", status=503, content_type="text/plain")
return JsonResponse({"status": "ok"})