Files
plm-lottery/apps/accounts/forms.py
T
davideandClaude Sonnet 5.5 efb3e2e702 A5: sign up, sign in, sign out, profile and the base layout (WEB-02, WEB-06, WEB-11, WEB-50..54)
Registration (case-insensitive unique names, 8-256 char passwords, no email),
sign-in with a generic error and a fresh session, internal-only ?next=,
POST-only sign-out, password change that closes the other sessions. The
failed-login backoff, the registration quota and the password-change guard
live in the database so every web process shares them; the client IP is the
last X-Forwarded-For hop. One responsive stylesheet with light/dark theme,
labelled fields, alerts and no inline scripts or styles; Italian error pages
and a readable CSRF failure. Mutation checks: open redirects, uncounted
failures and a missing CSRF middleware are all caught.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 14:07:45 +01:00

71 lines
3.5 KiB
Python

from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth.password_validation import validate_password
from .validators import username_validator
User = get_user_model()
class LoginForm(forms.Form):
username = forms.CharField(label="Nome utente", max_length=150, strip=True,
widget=forms.TextInput(attrs={"autocomplete": "username", "autocapitalize": "none",
"spellcheck": "false", "autofocus": True}))
password = forms.CharField(label="Password", max_length=1024, strip=False,
widget=forms.PasswordInput(attrs={"autocomplete": "current-password"}))
class RegisterForm(forms.Form):
username = forms.CharField(
label="Nome utente", min_length=3, max_length=32, strip=True, validators=[username_validator],
help_text="Da 3 a 32 caratteri: lettere, numeri, punto, trattino e trattino basso.",
widget=forms.TextInput(attrs={"autocomplete": "username", "autocapitalize": "none", "spellcheck": "false",
"autofocus": True}),
)
password = forms.CharField(label="Password", strip=False, help_text="Almeno 8 caratteri.",
widget=forms.PasswordInput(attrs={"autocomplete": "new-password"}))
password_confirm = forms.CharField(label="Ripeti la password", strip=False,
widget=forms.PasswordInput(attrs={"autocomplete": "new-password"}))
def clean_username(self):
username = self.cleaned_data["username"]
if User.objects.filter(username__iexact=username).exists(): # one namespace, any case (ADR-38)
raise forms.ValidationError("Questo nome utente è già in uso.", code="username_taken")
return username
def clean_password(self):
password = self.cleaned_data["password"]
validate_password(password)
return password
def clean(self):
cleaned = super().clean()
if cleaned.get("password") and cleaned.get("password_confirm") and cleaned["password"] != cleaned["password_confirm"]:
self.add_error("password_confirm", "Le due password non coincidono.")
return cleaned
class PasswordChangeForm(forms.Form):
current_password = forms.CharField(label="Password attuale", max_length=1024, strip=False,
widget=forms.PasswordInput(attrs={"autocomplete": "current-password"}))
new_password = forms.CharField(label="Nuova password", strip=False, help_text="Almeno 8 caratteri.",
widget=forms.PasswordInput(attrs={"autocomplete": "new-password"}))
new_password_confirm = forms.CharField(label="Ripeti la nuova password", strip=False,
widget=forms.PasswordInput(attrs={"autocomplete": "new-password"}))
def __init__(self, user, *args, **kwargs):
super().__init__(*args, **kwargs)
self.user = user
def clean_new_password(self):
password = self.cleaned_data["new_password"]
validate_password(password, self.user)
return password
def clean(self):
cleaned = super().clean()
if cleaned.get("new_password") and cleaned.get("new_password_confirm") and \
cleaned["new_password"] != cleaned["new_password_confirm"]:
self.add_error("new_password_confirm", "Le due password non coincidono.")
return cleaned