96 lines
3.0 KiB
Python
96 lines
3.0 KiB
Python
# retoor <retoor@molodetz.nl>
|
|
|
|
from pydantic import BaseModel, Field, field_validator, model_validator
|
|
from devplacepy.models.shared import _declared_age
|
|
|
|
|
|
class SignupForm(BaseModel):
|
|
username: str = Field(min_length=3, max_length=32)
|
|
email: str = Field(min_length=1, max_length=255)
|
|
password: str = Field(min_length=6, max_length=128)
|
|
confirm_password: str = Field(min_length=1, max_length=128)
|
|
birth_date: str = Field(min_length=1, max_length=20)
|
|
accept_terms: str = Field(default="")
|
|
|
|
@property
|
|
def age_band(self) -> str:
|
|
from devplacepy.database.moderation import age_band_for
|
|
|
|
return age_band_for(_declared_age(self.birth_date))
|
|
|
|
@field_validator("birth_date")
|
|
@classmethod
|
|
def old_enough(cls, value):
|
|
from devplacepy.database import minimum_age
|
|
|
|
minimum = minimum_age()
|
|
if _declared_age(value) < minimum:
|
|
raise ValueError(f"You must be at least {minimum} years old to join")
|
|
return value
|
|
|
|
@field_validator("accept_terms")
|
|
@classmethod
|
|
def terms_accepted(cls, value):
|
|
if value.strip().lower() not in ("1", "on", "true", "yes"):
|
|
raise ValueError(
|
|
"You must accept the Terms of Service and Community Guidelines"
|
|
)
|
|
return value
|
|
|
|
@field_validator("username")
|
|
@classmethod
|
|
def username_chars(cls, value):
|
|
if not value.isascii() or not all(
|
|
c.isalnum() or c in ("-", "_") for c in value
|
|
):
|
|
raise ValueError(
|
|
"Username can only contain letters, numbers, hyphens, and underscores"
|
|
)
|
|
from devplacepy.services.moderation.filter import classify
|
|
|
|
if classify(value).verdict == "block":
|
|
raise ValueError("That username breaks the community guidelines")
|
|
return value
|
|
|
|
@field_validator("email")
|
|
@classmethod
|
|
def email_has_at(cls, value):
|
|
if "@" not in value:
|
|
raise ValueError("Valid email is required")
|
|
return value
|
|
|
|
@model_validator(mode="after")
|
|
def passwords_match(self):
|
|
if self.password != self.confirm_password:
|
|
raise ValueError("Passwords do not match")
|
|
return self
|
|
|
|
|
|
class LoginForm(BaseModel):
|
|
email: str = Field(min_length=1, max_length=255)
|
|
password: str = Field(min_length=1, max_length=128)
|
|
remember_me: str = ""
|
|
next: str = ""
|
|
|
|
|
|
class ForgotPasswordForm(BaseModel):
|
|
email: str = Field(min_length=1, max_length=255)
|
|
|
|
@field_validator("email")
|
|
@classmethod
|
|
def email_has_at(cls, value):
|
|
if "@" not in value:
|
|
raise ValueError("Valid email is required")
|
|
return value
|
|
|
|
|
|
class ResetPasswordForm(BaseModel):
|
|
password: str = Field(min_length=6, max_length=128)
|
|
confirm_password: str = Field(min_length=1, max_length=128)
|
|
|
|
@model_validator(mode="after")
|
|
def passwords_match(self):
|
|
if self.password != self.confirm_password:
|
|
raise ValueError("Passwords do not match")
|
|
return self
|