refactor testing for joins, remove the no other contacts step

This commit is contained in:
Rachid Mrad 2024-01-03 16:06:11 -05:00
parent a175018ec7
commit 3783486be7
No known key found for this signature in database
GPG key ID: EF38E4CEC4A8F3CF
3 changed files with 13 additions and 21 deletions

View file

@ -39,7 +39,6 @@ for step, view in [
(Step.PURPOSE, views.Purpose),
(Step.YOUR_CONTACT, views.YourContact),
(Step.OTHER_CONTACTS, views.OtherContacts),
(Step.NO_OTHER_CONTACTS, views.NoOtherContacts),
(Step.ANYTHING_ELSE, views.AnythingElse),
(Step.REQUIREMENTS, views.Requirements),
(Step.REVIEW, views.Review),

View file

@ -96,6 +96,16 @@ class RegistrarFormSet(forms.BaseFormSet):
Hint: Subclass should call `self._to_database(...)`.
"""
raise NotImplementedError
def test_if_more_than_one_join(self, db_obj, rel, related_name):
logger.info(f"rel: {rel} | related_name: {related_name}")
threshold = 0
if rel == related_name:
threshold = 1
return getattr(db_obj, rel) is not None and getattr(db_obj, rel).count() > threshold
def _to_database(
self,
@ -133,17 +143,8 @@ class RegistrarFormSet(forms.BaseFormSet):
logger.info(cleaned)
# matching database object exists, update it
if db_obj is not None and cleaned:
if should_delete(cleaned):
number_of_reverse_joins = 0
for rel in reverse_joins:
count = getattr(db_obj, rel).count()
logger.info(f"Count for {rel}: {count}")
# Increment the counter if the count is greater than 0
if count > 0:
number_of_reverse_joins += 1
if any(getattr(db_obj, rel).count() > 1 for rel in reverse_joins) or number_of_reverse_joins > 1:
if should_delete(cleaned):
if any(self.test_if_more_than_one_join(db_obj, rel, related_name) for rel in reverse_joins):
logger.info("Object is joined to something")
# Remove the specific relationship without deleting the object
getattr(db_obj, related_name).remove(self.application)
@ -666,7 +667,7 @@ class OtherContactsForm(RegistrarForm):
class BaseOtherContactsFormSet(RegistrarFormSet):
JOIN = "other_contacts"
REVERSE_JOINS = ["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

View file

@ -42,7 +42,6 @@ class Step(StrEnum):
PURPOSE = "purpose"
YOUR_CONTACT = "your_contact"
OTHER_CONTACTS = "other_contacts"
NO_OTHER_CONTACTS = "no_other_contacts"
ANYTHING_ELSE = "anything_else"
REQUIREMENTS = "requirements"
REVIEW = "review"
@ -89,7 +88,6 @@ class ApplicationWizard(ApplicationWizardPermissionView, TemplateView):
Step.PURPOSE: _("Purpose of your domain"),
Step.YOUR_CONTACT: _("Your contact information"),
Step.OTHER_CONTACTS: _("Other employees from your organization"),
Step.NO_OTHER_CONTACTS: _("No other employees from your organization?"),
Step.ANYTHING_ELSE: _("Anything else?"),
Step.REQUIREMENTS: _("Requirements for operating .gov domains"),
Step.REVIEW: _("Review and submit your domain request"),
@ -102,7 +100,6 @@ class ApplicationWizard(ApplicationWizardPermissionView, TemplateView):
Step.TRIBAL_GOVERNMENT: lambda w: w.from_model("show_tribal_government", False),
Step.ORGANIZATION_ELECTION: lambda w: w.from_model("show_organization_election", False),
Step.ABOUT_YOUR_ORGANIZATION: lambda w: w.from_model("show_about_your_organization", False),
Step.NO_OTHER_CONTACTS: lambda w: w.from_model("show_no_other_contacts_rationale", False),
}
def __init__(self):
@ -528,11 +525,6 @@ class OtherContacts(ApplicationWizard):
return all_forms_valid
class NoOtherContacts(ApplicationWizard):
template_name = "application_no_other_contacts.html"
forms = [forms.NoOtherContactsForm]
class AnythingElse(ApplicationWizard):
template_name = "application_anything_else.html"
forms = [forms.AnythingElseForm]