This commit is contained in:
Rachid Mrad 2024-01-04 20:14:37 -05:00
parent a313c5496b
commit 913f918787
No known key found for this signature in database
GPG key ID: EF38E4CEC4A8F3CF
5 changed files with 81 additions and 84 deletions

View file

@ -7,6 +7,7 @@ from phonenumber_field.formfields import PhoneNumberField # type: ignore
from django import forms
from django.core.validators import RegexValidator, MaxLengthValidator
from django.utils.safestring import mark_safe
from django.db.models.fields.related import ForeignObjectRel
from api.views import DOMAIN_API_MESSAGES
@ -123,7 +124,14 @@ class RegistrarFormSet(forms.BaseFormSet):
obj.save()
query = getattr(obj, join).order_by("created_at").all() # order matters
related_name = obj._meta.get_field(join).related_query_name()
related_name = ""
field = obj._meta.get_field(join)
if isinstance(field, ForeignObjectRel) and callable(field.related_query_name):
related_name = field.related_query_name()
elif hasattr(field, "related_query_name") and callable(field.related_query_name):
related_name = field.related_query_name()
# the use of `zip` pairs the forms in the formset with the
# related objects gotten from the database -- there should always be
@ -148,7 +156,7 @@ class RegistrarFormSet(forms.BaseFormSet):
db_obj.save()
# no matching database object, create it
elif db_obj is None and cleaned and not cleaned.get('delete', False):
elif db_obj is None and cleaned and not cleaned.get("delete", False):
kwargs = pre_create(db_obj, cleaned)
getattr(obj, join).create(**kwargs)
@ -576,15 +584,12 @@ class OtherContactsYesNoForm(RegistrarForm):
# No pre-selection for new applications
default_value = None
self.fields['has_other_contacts'] = forms.TypedChoiceField(
coerce=lambda x: x.lower() == 'true' if x is not None else None,
choices=(
(True, "Yes, I can name other employees."),
(False, "No (We'll ask you to explain why).")
),
self.fields["has_other_contacts"] = forms.TypedChoiceField(
coerce=lambda x: x.lower() == "true" if x is not None else None,
choices=((True, "Yes, I can name other employees."), (False, "No (We'll ask you to explain why).")),
initial=default_value,
widget=forms.RadioSelect
)
widget=forms.RadioSelect,
)
class OtherContactsForm(RegistrarForm):
@ -644,14 +649,22 @@ class OtherContactsForm(RegistrarForm):
for field in self.fields:
if field in self.errors:
del self.errors[field]
return {'delete': True}
return {"delete": True}
return self.cleaned_data
class BaseOtherContactsFormSet(RegistrarFormSet):
JOIN = "other_contacts"
REVERSE_JOINS = ["user", "authorizing_official", "submitted_applications", "contact_applications", "information_authorizing_official", "submitted_applications_information", "contact_applications_information"]
REVERSE_JOINS = [
"user",
"authorizing_official",
"submitted_applications",
"contact_applications",
"information_authorizing_official",
"submitted_applications_information",
"contact_applications_information",
]
def __init__(self, *args, **kwargs):
self.formset_data_marked_for_deletion = False
@ -720,11 +733,7 @@ class NoOtherContactsForm(RegistrarForm):
message="Response must be less than 1000 characters.",
)
],
error_messages={
"required": (
"Rationale for no other employees is required."
)
},
error_messages={"required": ("Rationale for no other employees is required.")},
)
def __init__(self, *args, **kwargs):

View file

@ -446,35 +446,23 @@ class TestDomainApplication(TestCase):
"""has_rationale() returns true when an application has no_other_contacts_rationale"""
self.started_application.no_other_contacts_rationale = "You talkin' to me?"
self.started_application.save()
self.assertEquals(
self.started_application.has_rationale(),
True
)
self.assertEquals(self.started_application.has_rationale(), True)
def test_has_rationale_returns_false(self):
"""has_rationale() returns false when an application has no no_other_contacts_rationale"""
self.assertEquals(
self.started_application.has_rationale(),
False
)
self.assertEquals(self.started_application.has_rationale(), False)
def test_has_other_contacts_returns_true(self):
"""has_other_contacts() returns true when an application has other_contacts"""
# completed_application has other contacts by default
self.assertEquals(
self.started_application.has_other_contacts(),
True
)
self.assertEquals(self.started_application.has_other_contacts(), True)
def test_has_other_contacts_returns_false(self):
"""has_other_contacts() returns false when an application has no other_contacts"""
application = completed_application(
status=DomainApplication.ApplicationStatus.STARTED, name="no-others.gov", has_other_contacts=False
)
self.assertEquals(
application.has_other_contacts(),
False
)
self.assertEquals(application.has_other_contacts(), False)
class TestPermissions(TestCase):

View file

@ -494,7 +494,7 @@ class OtherContacts(ApplicationWizard):
# test first for yes_no_form validity
if other_contacts_yes_no_form.is_valid():
# test for has_contacts
if other_contacts_yes_no_form.cleaned_data.get('has_other_contacts'):
if other_contacts_yes_no_form.cleaned_data.get("has_other_contacts"):
# mark the no_other_contacts_form for deletion
no_other_contacts_form.mark_form_for_deletion()
# test that the other_contacts_forms and no_other_contacts_forms are valid