Source code for ferrosoft.apps.ferrobase.tenant.forms
from django import forms
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _
from ferrosoft import request
from ferrosoft.apps.ferrobase.models import User, Organization, Tenant, Language
[docs]
class UserResourcesForm(request.Form):
"""Request payload carrying the storage-location UUIDs to grant a user."""
storage_location = request.UUIDField()
[docs]
class UserSettingsForm(forms.ModelForm):
"""ModelForm for the user-editable subset of personal settings."""
[docs]
class Meta:
model = User
fields = [
"first_name",
"last_name",
"email",
"language",
"timezone",
"color_theme",
]
def __init__(self, *args, **kwargs):
"""Restrict the ``language`` choices to the platform's supported set."""
super().__init__(*args, **kwargs)
self.fields["language"].queryset = Language.objects.get_supported()
[docs]
class SignupForm(forms.Form):
"""Public self-service signup form: applicant identity and company."""
email = forms.EmailField(
label=_("Email address"),
)
first_name = forms.CharField(
label=_("First name"),
)
last_name = forms.CharField(
label=_("Last name"),
)
company = forms.CharField(
label=_("Company"),
)
[docs]
def clean_email(self):
"""Reject signup when an account with this email already exists."""
email = self.cleaned_data["email"]
if User.objects.filter(email=email).exists():
raise ValidationError(_("Please choose a different email address."))
return email
[docs]
def clean_company(self):
"""Reject company names already used by an organisation or tenant."""
company = self.cleaned_data["company"]
org_exists = Organization.objects.filter(name=company).exists()
tenant_exists = Tenant.objects.filter(name=company).exists()
if org_exists or tenant_exists:
raise ValidationError(_("Please choose a different name."))
return company
[docs]
class SignupUserForm(forms.Form):
"""Account activation form collecting and confirming the initial password."""
password = forms.CharField(
label=_("Password"),
widget=forms.PasswordInput,
min_length=User.MINIMUM_PASSWORD_LENGTH,
)
password_confirm = forms.CharField(
label=_("Confirm password"),
widget=forms.PasswordInput,
)
[docs]
def clean(self):
"""Require both password fields to match.
The placeholder defaults (``"1"`` / ``"2"``) ensure that missing
values are treated as mismatched rather than accidentally equal.
"""
data = super().clean()
if data.get("password", "1") != data.get("password_confirm", "2"):
raise forms.ValidationError(_("Passwords don't match"))
return data