Finally fixed!

This commit is contained in:
CocoByte 2023-12-17 18:14:08 -07:00
parent d16aad99a4
commit a1fe3aacca
No known key found for this signature in database
GPG key ID: BBFAA2526384C97F

View file

@ -15,6 +15,7 @@ from registrar.templatetags.url_helpers import public_site_url
from registrar.utility import errors from registrar.utility import errors
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
from registrar.management.commands.utility.terminal_helper import TerminalColors, TerminalHelper
class RegistrarForm(forms.Form): class RegistrarForm(forms.Form):
@ -262,7 +263,7 @@ class OrganizationContactForm(RegistrarForm):
validators=[ validators=[
RegexValidator( RegexValidator(
"^[0-9]{5}(?:-[0-9]{4})?$|^$", "^[0-9]{5}(?:-[0-9]{4})?$|^$",
message="Enter a zip code in the required format, like 12345 or 12345-6789.", message="Enter a zip code in the form of 12345 or 12345-6789.",
) )
], ],
) )
@ -557,6 +558,7 @@ class YourContactForm(RegistrarForm):
class OtherContactsForm(RegistrarForm): class OtherContactsForm(RegistrarForm):
first_name = forms.CharField( first_name = forms.CharField(
# required=False, #is required, but validate in clean() instead to allow for custom form deletion condition
label="First name / given name", label="First name / given name",
error_messages={"required": "Enter the first name / given name of this contact."}, error_messages={"required": "Enter the first name / given name of this contact."},
) )
@ -565,10 +567,12 @@ class OtherContactsForm(RegistrarForm):
label="Middle name (optional)", label="Middle name (optional)",
) )
last_name = forms.CharField( last_name = forms.CharField(
# required=False, #is required, but validate in clean() instead to allow for custom form deletion condition
label="Last name / family name", label="Last name / family name",
error_messages={"required": "Enter the last name / family name of this contact."}, error_messages={"required": "Enter the last name / family name of this contact."},
) )
title = forms.CharField( title = forms.CharField(
# required=False, #is required, but validate in clean() instead to allow for custom form deletion condition
label="Title or role in your organization", label="Title or role in your organization",
error_messages={ error_messages={
"required": ( "required": (
@ -577,27 +581,100 @@ class OtherContactsForm(RegistrarForm):
}, },
) )
email = forms.EmailField( email = forms.EmailField(
# required=False, #is required, but validate in clean() instead to allow for custom form deletion condition
label="Email", label="Email",
error_messages={"invalid": ("Enter an email address in the required format, like name@example.com.")}, error_messages={"invalid": ("Enter an email address in the required format, like name@example.com.")},
) )
phone = PhoneNumberField( phone = PhoneNumberField(
# required=False, #is required, but validate in clean() instead to allow for custom form deletion condition
label="Phone", label="Phone",
error_messages={"required": "Enter a phone number for this contact."}, error_messages={"required": "Enter a phone number for this contact."},
) )
# Override clean in order to correct validation logic
def clean(self):
cleaned = self.cleaned_data # super().clean()
TerminalHelper.print_debug(f"""{TerminalColors.MAGENTA} CLEANING...
FIELDS:
{self.fields}
CLEANED:
{cleaned}{TerminalColors.ENDC}""")
form_is_empty = all(v is None or v == '' for v in cleaned.values())
# if not form_is_empty: #TODO: add check for "marked for deleteion" when implementing button
#Validation Logic
# if not self.cleaned_data["first_name"]:
# self.add_error('first_name', "Enter the first name / given name of this contact.")
# if not self.cleaned_data["last_name"]:
# self.add_error('last_name', "Enter the last name / family name of this contact.")
# if not self.cleaned_data["title"]:
# self.add_error('title', "Enter the title or role in your organization of this contact (e.g., Chief Information Officer).")
if form_is_empty:
TerminalHelper.print_debug(f"""{TerminalColors.MAGENTA} ***** BLANK FORM DETECTED *******
{TerminalColors.ENDC}""")
# clear any errors raised by the form fields
# (before this clean() method is run, each field
# performs its own clean, which could result in
# errors that we wish to ignore at this point)
#
# NOTE: we cannot just clear() the errors list.
# That causes problems.
for field in self.fields:
if field in self.errors:
del self.errors[field]
return cleaned
# # Always return a value to use as the new cleaned data, even if
# # this method didn't change it.
# return data
# for field in self.fields.values():
# isBlank = field is None or field == ''
# TerminalHelper.print_debug(f"""{TerminalColors.YELLOW} {field} is blank = {isBlank} {TerminalColors.ENDC}""")
# TerminalHelper.print_debug(f"""{TerminalColors.YELLOW} {field.required} {TerminalColors.ENDC}""")
# return None
# return super().clean()
# def _should_delete_form(self, form):
# TerminalHelper.print_debug(f"{TerminalColors.MAGENTA} SHOULD DELETE FORM?...{TerminalColors.ENDC}")
# """Return whether or not the form was marked for deletion."""
# return all(field is None or field == '' for field in self.fields.values())
class BaseOtherContactsFormSet(RegistrarFormSet): class BaseOtherContactsFormSet(RegistrarFormSet):
JOIN = "other_contacts" JOIN = "other_contacts"
# def get_deletion_widget(self):
# delete_button = '<button type="button" class="usa-button usa-button--unstyled display-block float-right-tablet delete-record">Delete</button>'
# return delete_button
# # if form.cleaned_data.get(forms.formsets.DELETION_FIELD_NAME):
# # return True # marked for delete
# # fields = ('name', 'question', 'amount', 'measure', 'comment')
# print(form.cleaned_data)
# if not any(form.cleaned_data):
# return True
# return False
def should_delete(self, cleaned): def should_delete(self, cleaned):
empty = (isinstance(v, str) and not v.strip() for v in cleaned.values()) # TerminalHelper.print_debug(f"{TerminalColors.MAGENTA} SHOULD DELETE OTHER CONTACTS?... {cleaned.values()}{TerminalColors.ENDC}")
empty = (isinstance(v, str) and (v.strip() == "" or v == None) for v in cleaned.values())
return all(empty) return all(empty)
def to_database(self, obj: DomainApplication): def to_database(self, obj: DomainApplication):
TerminalHelper.print_debug(f"{TerminalColors.OKCYAN} TO DATABASE {TerminalColors.ENDC}")
self._to_database(obj, self.JOIN, self.should_delete, self.pre_update, self.pre_create) self._to_database(obj, self.JOIN, self.should_delete, self.pre_update, self.pre_create)
@classmethod @classmethod
def from_database(cls, obj): def from_database(cls, obj):
TerminalHelper.print_debug(f"{TerminalColors.OKCYAN} FROM DATABASE {TerminalColors.ENDC}")
return super().from_database(obj, cls.JOIN, cls.on_fetch) return super().from_database(obj, cls.JOIN, cls.on_fetch)
@ -606,6 +683,7 @@ OtherContactsFormSet = forms.formset_factory(
extra=1, extra=1,
absolute_max=1500, # django default; use `max_num` to limit entries absolute_max=1500, # django default; use `max_num` to limit entries
formset=BaseOtherContactsFormSet, formset=BaseOtherContactsFormSet,
# can_delete=True #TODO: use when implementing delete button?
) )
@ -634,7 +712,6 @@ class AnythingElseForm(RegistrarForm):
], ],
) )
class RequirementsForm(RegistrarForm): class RequirementsForm(RegistrarForm):
is_policy_acknowledged = forms.BooleanField( is_policy_acknowledged = forms.BooleanField(
label="I read and agree to the requirements for operating .gov domains.", label="I read and agree to the requirements for operating .gov domains.",