working code, but needs cleanup

This commit is contained in:
David Kennedy 2024-01-03 07:13:04 -05:00
parent ce16ecbfb1
commit 8ddc1d32b9
No known key found for this signature in database
GPG key ID: 6528A5386E66B96B

View file

@ -646,6 +646,10 @@ class OtherContactsForm(RegistrarForm):
class BaseOtherContactsFormSet(RegistrarFormSet): class BaseOtherContactsFormSet(RegistrarFormSet):
JOIN = "other_contacts" JOIN = "other_contacts"
def __init__(self, *args, **kwargs):
self.formset_data_marked_for_deletion = False
super().__init__(*args, **kwargs)
def should_delete(self, cleaned): def should_delete(self, cleaned):
empty = (isinstance(v, str) and (v.strip() == "" or v is None) for v in cleaned.values()) empty = (isinstance(v, str) and (v.strip() == "" or v is None) for v in cleaned.values())
return all(empty) return all(empty)
@ -660,14 +664,24 @@ class BaseOtherContactsFormSet(RegistrarFormSet):
def remove_form_data(self): def remove_form_data(self):
logger.info("removing form data from other contact set") logger.info("removing form data from other contact set")
self.formset_data_marked_for_deletion = True
for form in self.forms: for form in self.forms:
form.remove_form_data() form.remove_form_data()
def is_valid(self):
if self.formset_data_marked_for_deletion:
self.validate_min = False
val = super().is_valid()
logger.info(f"other contacts form set is valid = {val}")
return val
OtherContactsFormSet = forms.formset_factory( OtherContactsFormSet = forms.formset_factory(
OtherContactsForm, OtherContactsForm,
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
min_num=1,
validate_min=True,
formset=BaseOtherContactsFormSet, formset=BaseOtherContactsFormSet,
) )