diff --git a/src/registrar/forms/application_wizard.py b/src/registrar/forms/application_wizard.py index 590e5f1fa..59c044353 100644 --- a/src/registrar/forms/application_wizard.py +++ b/src/registrar/forms/application_wizard.py @@ -596,13 +596,13 @@ class OtherContactsForm(RegistrarForm): ) def __init__(self, *args, **kwargs): - self.form_data_deleted = False + self.form_data_marked_for_deletion = False super().__init__(*args, **kwargs) - def remove_form_data(self): + def mark_form_for_deletion(self): logger.info("removing form data from other contact") self.data = {} - self.form_data_deleted = True + self.form_data_marked_for_deletion = True def clean(self): """ @@ -613,7 +613,7 @@ class OtherContactsForm(RegistrarForm): validation """ - if self.form_data_deleted: + if self.form_data_marked_for_deletion: # Set form_is_empty to True initially form_is_empty = True for name, field in self.fields.items(): @@ -662,11 +662,15 @@ class BaseOtherContactsFormSet(RegistrarFormSet): def from_database(cls, obj): return super().from_database(obj, cls.JOIN, cls.on_fetch) - def remove_form_data(self): + def mark_formset_for_deletion(self): + """Mark other contacts formset for deletion. + Updates forms in formset as well to mark them for deletion. + This has an effect on validity checks and to_database methods. + """ logger.info("removing form data from other contact set") self.formset_data_marked_for_deletion = True for form in self.forms: - form.remove_form_data() + form.mark_form_for_deletion() def is_valid(self): if self.formset_data_marked_for_deletion: @@ -707,9 +711,11 @@ class NoOtherContactsForm(RegistrarForm): self.form_data_marked_for_deletion = False super().__init__(*args, **kwargs) - def remove_form_data(self): + def mark_form_for_deletion(self): + """Marks no_other_contacts form for deletion. + This changes behavior of validity checks and to_database + methods.""" logger.info("removing form data from no other contacts") - # self.data = {"no_other_contacts_rationale": ""} self.form_data_marked_for_deletion = True def clean(self): diff --git a/src/registrar/views/application.py b/src/registrar/views/application.py index 31948e034..24a3de7d0 100644 --- a/src/registrar/views/application.py +++ b/src/registrar/views/application.py @@ -486,70 +486,70 @@ class OtherContacts(ApplicationWizard): template_name = "application_other_contacts.html" forms = [forms.OtherContactsYesNoForm, forms.OtherContactsFormSet, forms.NoOtherContactsForm] - def post(self, request, *args, **kwargs) -> HttpResponse: - """This method handles POST requests.""" - # Log the keys and values of request.POST - for key, value in request.POST.items(): - logger.info("Key: %s, Value: %s", key, value) - # if accessing this class directly, redirect to the first step - if self.__class__ == ApplicationWizard: - return self.goto(self.steps.first) + # def post(self, request, *args, **kwargs) -> HttpResponse: + # """This method handles POST requests.""" + # # Log the keys and values of request.POST + # for key, value in request.POST.items(): + # logger.info("Key: %s, Value: %s", key, value) + # # if accessing this class directly, redirect to the first step + # if self.__class__ == ApplicationWizard: + # return self.goto(self.steps.first) - # which button did the user press? - button: str = request.POST.get("submit_button", "") + # # which button did the user press? + # button: str = request.POST.get("submit_button", "") - forms = self.get_forms(use_post=True) - # forms is now set as follows: - # forms.0 is yes no form - # forms.1 - forms.length-1 are other contacts forms - # forms.length is no other contacts form - yes_no_form = forms[0] - other_contacts_forms = forms[1] - no_other_contacts_form = forms[2] + # forms = self.get_forms(use_post=True) + # # forms is now set as follows: + # # forms.0 is yes no form + # # forms.1 - forms.length-1 are other contacts forms + # # forms.length is no other contacts form + # yes_no_form = forms[0] + # other_contacts_forms = forms[1] + # no_other_contacts_form = forms[2] - all_forms_valid = True - # test first for yes_no_form validity - if yes_no_form.is_valid(): - logger.info("yes no form is valid") - # test for has_contacts - if yes_no_form.cleaned_data.get('has_other_contacts'): - logger.info("has other contacts") - # remove data from no_other_contacts_form and set - # form to always_valid - no_other_contacts_form.remove_form_data() - # test that the other_contacts_forms and no_other_contacts_forms are valid - if not self.is_valid(forms[1:]): - all_forms_valid = False - else: - logger.info("has no other contacts") - # remove data from each other_contacts_form - other_contacts_forms.remove_form_data() - # test that the other_contacts_forms and no_other_contacts_forms are valid - if not self.is_valid(forms[1:]): - all_forms_valid = False - else: - all_forms_valid = False + # all_forms_valid = True + # # test first for yes_no_form validity + # if yes_no_form.is_valid(): + # logger.info("yes no form is valid") + # # test for has_contacts + # if yes_no_form.cleaned_data.get('has_other_contacts'): + # logger.info("has other contacts") + # # remove data from no_other_contacts_form and set + # # form to always_valid + # no_other_contacts_form.remove_form_data() + # # test that the other_contacts_forms and no_other_contacts_forms are valid + # if not self.is_valid(forms[1:]): + # all_forms_valid = False + # else: + # logger.info("has no other contacts") + # # remove data from each other_contacts_form + # other_contacts_forms.remove_form_data() + # # test that the other_contacts_forms and no_other_contacts_forms are valid + # if not self.is_valid(forms[1:]): + # all_forms_valid = False + # else: + # all_forms_valid = False - if all_forms_valid: - logger.info("all forms are valid") - # always save progress - self.save(forms) - else: - context = self.get_context_data() - context["forms"] = forms - return render(request, self.template_name, context) + # if all_forms_valid: + # logger.info("all forms are valid") + # # always save progress + # self.save(forms) + # else: + # context = self.get_context_data() + # context["forms"] = forms + # return render(request, self.template_name, context) - # if user opted to save their progress, - # return them to the page they were already on - if button == "save": - messages.success(request, "Your progress has been saved!") - return self.goto(self.steps.current) - # if user opted to save progress and return, - # return them to the home page - if button == "save_and_return": - return HttpResponseRedirect(reverse("home")) - # otherwise, proceed as normal - return self.goto_next_step() + # # if user opted to save their progress, + # # return them to the page they were already on + # if button == "save": + # messages.success(request, "Your progress has been saved!") + # return self.goto(self.steps.current) + # # if user opted to save progress and return, + # # return them to the home page + # if button == "save_and_return": + # return HttpResponseRedirect(reverse("home")) + # # otherwise, proceed as normal + # return self.goto_next_step() # def post(self, request, *args, **kwargs) -> HttpResponse: # parent_form = forms.OtherContactsYesNoForm(request.POST, **kwargs) @@ -567,44 +567,37 @@ class OtherContacts(ApplicationWizard): # no_other_contacts_form.data = {} # return super().post(request, *args, **kwargs) - # def is_valid(self, forms: list) -> bool: - # """Returns True if all forms in the wizard are valid.""" - # if forms[0].is_valid(): - # # test for has_contacts - # if forms[0].cleaned_data.get('has_other_contacts'): - # logger.info("testing validity on other contacts") - # validity_list = [] + def is_valid(self, forms: list) -> bool: + """Overrides default behavior defined in ApplicationWizard. + Depending on value in other_contacts_yes_no_form, marks forms in + other_contacts or no_other_contacts for deletion. Then validates + all forms. + """ + other_contacts_yes_no_form = forms[0] + other_contacts_forms = forms[1] + no_other_contacts_form = forms[2] - # # Iterate over the sublist of forms from index 2 to the second-to-last index - # for form in forms[1:-1]: - # # Check if the form is valid and append the result to the validity_list - # logger.info(f"testing validity of form of type {form.__class__.__name__}") - # validity_list.append(form.is_valid()) - - # # Check if all elements in validity_list are True - # return all(validity_list) - # # return all(form.is_valid() for form in forms[2:-2]) - # else: - # logger.info("testing validity on no other contacts") - # return forms[-1].is_valid() - # # if has contacts , return if next length-2 are valid - # # else return last form in list is valid - # else: - # return False - # # are_valid = (form.is_valid() for form in forms) - # # return all(are_valid) - - - # def save(self, forms: list): - # """ - # Unpack the form responses onto the model object properties. - - # Saves the application to the database. - # """ - # logger.info("in save") - # for form in forms: - # if form is not None and hasattr(form, "to_database"): - # form.to_database(self.application) + all_forms_valid = True + # test first for yes_no_form validity + if other_contacts_yes_no_form.is_valid(): + logger.info("yes no form is valid") + # test for has_contacts + if other_contacts_yes_no_form.cleaned_data.get('has_other_contacts'): + logger.info("has other contacts") + # remove data from no_other_contacts_form and set + # form to always_valid + no_other_contacts_form.mark_form_for_deletion() + # test that the other_contacts_forms and no_other_contacts_forms are valid + all_forms_valid = all(form.is_valid() for form in forms[1:]) + else: + logger.info("has no other contacts") + # remove data from each other_contacts_form + other_contacts_forms.mark_formset_for_deletion() + # test that the other_contacts_forms and no_other_contacts_forms are valid + all_forms_valid = all(form.is_valid() for form in forms[1:]) + else: + all_forms_valid = False + return all_forms_valid class NoOtherContacts(ApplicationWizard):