Vaihe 1/1: users-moduuli, kirjautuminen ja käyttäjähallinta
- shared-moduuli: DomainError, EmailAddress, UnitOfWork, tapahtumat, TimestampedModel, daisyUI-lomakerenderöinti ja yhteiset pohjat - users-moduuli Clean Architecture -kerroksin: oma käyttäjämalli (AUTH_USER_MODEL), roolit ja oikeudet, harjoittelija-, vastuuhenkilö- ja viimeisen pääkäyttäjän säännöt - kirjautuminen sähköpostilla, yritysten rajoitus, "muista minut", istunnon katkaisu kun tunnus poistetaan käytöstä tai harjoittelu päättyy - kertakäyttöiset salasanalinkit (72 h / 60 min, uusi mitätöi vanhan), salasanasääntö ja create_admin-komento - käyttäjähallinta: lista (HTMX-haku), lomakkeet ja kopioitava linkki - Tailwind 4 + daisyUI 5 + HTMX + Alpine npm-buildilla, suomennokset - testit: domain, application (fakeilla), integraatio ja selaintestit (axe, vaalea/tumma, 1920/320 px); ruff, mypy ja import-linter - dokumentit päivitetty toteutuksen mukaisiksi Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
"""Form helpers that give every form the same daisyUI styling and ARIA wiring."""
|
||||
|
||||
from collections.abc import Mapping
|
||||
from typing import Any
|
||||
|
||||
from django import forms
|
||||
from django.forms.renderers import TemplatesSetting
|
||||
from django.forms.widgets import Input
|
||||
|
||||
from apps.shared.domain.exceptions import DomainError
|
||||
|
||||
|
||||
class DaisyFormRenderer(TemplatesSetting):
|
||||
"""Renders ``{{ field.as_field_group }}`` / ``{{ form }}`` with ``components/field.html``."""
|
||||
|
||||
form_template_name = "components/form.html"
|
||||
field_template_name = "components/field.html"
|
||||
|
||||
|
||||
_WIDGET_CLASSES: list[tuple[type[forms.Widget], str]] = [
|
||||
(forms.CheckboxInput, "checkbox"),
|
||||
(forms.RadioSelect, "radio"),
|
||||
(forms.CheckboxSelectMultiple, "checkbox"),
|
||||
(forms.Select, "select w-full"),
|
||||
(forms.Textarea, "textarea w-full"),
|
||||
(Input, "input w-full"),
|
||||
]
|
||||
|
||||
|
||||
class DaisyFormMixin:
|
||||
"""Adds daisyUI classes to widgets and links field errors with ``aria-describedby``."""
|
||||
|
||||
fields: dict[str, forms.Field]
|
||||
|
||||
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
||||
super().__init__(*args, **kwargs)
|
||||
for field in self.fields.values():
|
||||
widget = field.widget
|
||||
for widget_type, css in _WIDGET_CLASSES:
|
||||
if isinstance(widget, widget_type):
|
||||
existing = widget.attrs.get("class", "")
|
||||
widget.attrs["class"] = f"{css} {existing}".strip()
|
||||
break
|
||||
|
||||
def full_clean(self) -> None:
|
||||
super().full_clean() # type: ignore[misc]
|
||||
form: Any = self
|
||||
for name in form.errors:
|
||||
if name not in self.fields:
|
||||
continue
|
||||
bound = form[name]
|
||||
widget = self.fields[name].widget
|
||||
error_id = f"{bound.auto_id}_error"
|
||||
described = widget.attrs.get("aria-describedby", "")
|
||||
if error_id not in described.split():
|
||||
widget.attrs["aria-describedby"] = f"{described} {error_id}".strip()
|
||||
widget.attrs["aria-invalid"] = "true"
|
||||
|
||||
def add_domain_error(self, error: DomainError, messages: Mapping[str, object]) -> None:
|
||||
"""Show a domain rule violation as a field (or non-field) error."""
|
||||
form: Any = self
|
||||
message = str(messages.get(error.code, messages.get("*", error.code)))
|
||||
field = error.field if error.field in self.fields else None
|
||||
form.add_error(field, message)
|
||||
@@ -0,0 +1,6 @@
|
||||
from django.http import HttpRequest, HttpResponse
|
||||
from django.shortcuts import render
|
||||
|
||||
|
||||
def home(request: HttpRequest) -> HttpResponse:
|
||||
return render(request, "home.html")
|
||||
Reference in New Issue
Block a user