cleanup and lint

This commit is contained in:
Rachid Mrad 2024-01-09 13:17:43 -05:00
parent f2b12e01d0
commit 3895a04879
No known key found for this signature in database
GPG key ID: EF38E4CEC4A8F3CF
3 changed files with 43 additions and 44 deletions

View file

@ -17,6 +17,7 @@ from registrar.utility import errors
logger = logging.getLogger(__name__)
class RegistrarForm(forms.Form):
"""
A common set of methods and configuration.
@ -94,14 +95,14 @@ 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):
"""Helper for finding whether an object is joined more than once."""
# threshold is the number of related objects that are acceptable
# when determining if related objects exist. threshold is 0 for most
# relationships. if the relationship is related_name, we know that
# there is already exactly 1 acceptable relationship (the one we are
# attempting to delete), so the threshold is 1
# attempting to delete), so the threshold is 1
threshold = 1 if rel == related_name else 0
# Raise a KeyError if rel is not a defined field on the db_obj model
@ -640,7 +641,7 @@ class OtherContactsForm(RegistrarForm):
label="Email",
error_messages={
"required": ("Enter an email address in the required format, like name@example.com."),
"invalid": ("Enter an email address in the required format, like name@example.com.")
"invalid": ("Enter an email address in the required format, like name@example.com."),
},
)
phone = PhoneNumberField(
@ -659,7 +660,7 @@ class OtherContactsForm(RegistrarForm):
"""
self.form_data_marked_for_deletion = False
super().__init__(*args, **kwargs)
self.empty_permitted=False
self.empty_permitted = False
def mark_form_for_deletion(self):
self.form_data_marked_for_deletion = True
@ -668,8 +669,8 @@ class OtherContactsForm(RegistrarForm):
"""
This method overrides the default behavior for forms.
This cleans the form after field validation has already taken place.
In this override, allow for a form which is deleted by user or marked for
deletion by formset to be considered valid even though certain required fields have
In this override, allow for a form which is deleted by user or marked for
deletion by formset to be considered valid even though certain required fields have
not passed field validation
"""
if self.form_data_marked_for_deletion or self.cleaned_data.get("DELETE"):
@ -694,9 +695,9 @@ class OtherContactsForm(RegistrarForm):
class BaseOtherContactsFormSet(RegistrarFormSet):
"""
FormSet for Other Contacts
There are two conditions by which a form in the formset can be marked for deletion.
One is if the user clicks 'DELETE' button, and this is submitted in the form. The
One is if the user clicks 'DELETE' button, and this is submitted in the form. The
other is if the YesNo form, which is submitted with this formset, is set to No; in
this case, all forms in formset are marked for deletion. Both of these conditions
must co-exist.
@ -705,6 +706,7 @@ class BaseOtherContactsFormSet(RegistrarFormSet):
tested and handled; this is configured with REVERSE_JOINS, which is an array of
strings representing the relationships between contact model and other models.
"""
JOIN = "other_contacts"
REVERSE_JOINS = [
"user",
@ -718,7 +720,7 @@ class BaseOtherContactsFormSet(RegistrarFormSet):
def get_deletion_widget(self):
return forms.HiddenInput(attrs={"class": "deletion"})
def __init__(self, *args, **kwargs):
"""
Override __init__ for RegistrarFormSet.
@ -737,14 +739,14 @@ class BaseOtherContactsFormSet(RegistrarFormSet):
Implements should_delete method from BaseFormSet.
"""
return self.formset_data_marked_for_deletion or cleaned.get("DELETE", False)
def pre_create(self, db_obj, cleaned):
"""Code to run before an item in the formset is created in the database."""
# remove DELETE from cleaned
if "DELETE" in cleaned:
cleaned.pop('DELETE')
cleaned.pop("DELETE")
return cleaned
def to_database(self, obj: DomainApplication):
self._to_database(obj, self.JOIN, self.REVERSE_JOINS, self.should_delete, self.pre_update, self.pre_create)