Replace form with modelform

Pull in content from related PR and refactor around it
This commit is contained in:
zandercymatics 2024-05-20 11:25:32 -06:00
parent d202d2601f
commit 9a6ccc1c44
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
10 changed files with 230 additions and 363 deletions

View file

@ -55,6 +55,34 @@ class UserProfileForm(forms.ModelForm):
"required": "Enter your email address in the required format, like name@example.com."
}
self.fields["phone"].error_messages["required"] = "Enter your phone number."
self.domainInfo = None
DomainHelper.disable_field(self.fields["email"], disable_required=True)
class FinishSetupProfileForm(UserProfileForm):
"""Form for updating user profile."""
full_name = forms.CharField(required=True, label="Full name")
def clean(self):
cleaned_data = super().clean()
# Remove the full name property
if "full_name" in cleaned_data:
# Delete the full name element as its purely decorative.
# We include it as a normal Charfield for all the advantages
# and utility that it brings, but we're playing pretend.
del cleaned_data["full_name"]
return cleaned_data
def __init__(self, *args, **kwargs):
"""Override the inerited __init__ method to update the fields."""
super().__init__(*args, **kwargs)
# Set custom form label for email
self.fields["email"].label = "Organization email"
self.fields["title"].label = "Title or role in your organization"
# Define the "full_name" value
if self.instance and hasattr(self.instance, 'full_name'):
self.fields["full_name"].initial = self.instance.get_formatted_name()