incremental updates

This commit is contained in:
David Kennedy 2024-01-03 09:52:57 -05:00
parent 8ddc1d32b9
commit 2894421a5a
No known key found for this signature in database
GPG key ID: 6528A5386E66B96B
2 changed files with 103 additions and 104 deletions

View file

@ -596,13 +596,13 @@ class OtherContactsForm(RegistrarForm):
) )
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.form_data_deleted = False self.form_data_marked_for_deletion = False
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
def remove_form_data(self): def mark_form_for_deletion(self):
logger.info("removing form data from other contact") logger.info("removing form data from other contact")
self.data = {} self.data = {}
self.form_data_deleted = True self.form_data_marked_for_deletion = True
def clean(self): def clean(self):
""" """
@ -613,7 +613,7 @@ class OtherContactsForm(RegistrarForm):
validation validation
""" """
if self.form_data_deleted: if self.form_data_marked_for_deletion:
# Set form_is_empty to True initially # Set form_is_empty to True initially
form_is_empty = True form_is_empty = True
for name, field in self.fields.items(): for name, field in self.fields.items():
@ -662,11 +662,15 @@ class BaseOtherContactsFormSet(RegistrarFormSet):
def from_database(cls, obj): def from_database(cls, obj):
return super().from_database(obj, cls.JOIN, cls.on_fetch) 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") logger.info("removing form data from other contact set")
self.formset_data_marked_for_deletion = True self.formset_data_marked_for_deletion = True
for form in self.forms: for form in self.forms:
form.remove_form_data() form.mark_form_for_deletion()
def is_valid(self): def is_valid(self):
if self.formset_data_marked_for_deletion: if self.formset_data_marked_for_deletion:
@ -707,9 +711,11 @@ class NoOtherContactsForm(RegistrarForm):
self.form_data_marked_for_deletion = False self.form_data_marked_for_deletion = False
super().__init__(*args, **kwargs) 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") logger.info("removing form data from no other contacts")
# self.data = {"no_other_contacts_rationale": ""}
self.form_data_marked_for_deletion = True self.form_data_marked_for_deletion = True
def clean(self): def clean(self):

View file

@ -486,70 +486,70 @@ class OtherContacts(ApplicationWizard):
template_name = "application_other_contacts.html" template_name = "application_other_contacts.html"
forms = [forms.OtherContactsYesNoForm, forms.OtherContactsFormSet, forms.NoOtherContactsForm] forms = [forms.OtherContactsYesNoForm, forms.OtherContactsFormSet, forms.NoOtherContactsForm]
def post(self, request, *args, **kwargs) -> HttpResponse: # def post(self, request, *args, **kwargs) -> HttpResponse:
"""This method handles POST requests.""" # """This method handles POST requests."""
# Log the keys and values of request.POST # # Log the keys and values of request.POST
for key, value in request.POST.items(): # for key, value in request.POST.items():
logger.info("Key: %s, Value: %s", key, value) # logger.info("Key: %s, Value: %s", key, value)
# if accessing this class directly, redirect to the first step # # if accessing this class directly, redirect to the first step
if self.__class__ == ApplicationWizard: # if self.__class__ == ApplicationWizard:
return self.goto(self.steps.first) # return self.goto(self.steps.first)
# which button did the user press? # # which button did the user press?
button: str = request.POST.get("submit_button", "") # button: str = request.POST.get("submit_button", "")
forms = self.get_forms(use_post=True) # forms = self.get_forms(use_post=True)
# forms is now set as follows: # # forms is now set as follows:
# forms.0 is yes no form # # forms.0 is yes no form
# forms.1 - forms.length-1 are other contacts forms # # forms.1 - forms.length-1 are other contacts forms
# forms.length is no other contacts form # # forms.length is no other contacts form
yes_no_form = forms[0] # yes_no_form = forms[0]
other_contacts_forms = forms[1] # other_contacts_forms = forms[1]
no_other_contacts_form = forms[2] # no_other_contacts_form = forms[2]
all_forms_valid = True # all_forms_valid = True
# test first for yes_no_form validity # # test first for yes_no_form validity
if yes_no_form.is_valid(): # if yes_no_form.is_valid():
logger.info("yes no form is valid") # logger.info("yes no form is valid")
# test for has_contacts # # test for has_contacts
if yes_no_form.cleaned_data.get('has_other_contacts'): # if yes_no_form.cleaned_data.get('has_other_contacts'):
logger.info("has other contacts") # logger.info("has other contacts")
# remove data from no_other_contacts_form and set # # remove data from no_other_contacts_form and set
# form to always_valid # # form to always_valid
no_other_contacts_form.remove_form_data() # no_other_contacts_form.remove_form_data()
# test that the other_contacts_forms and no_other_contacts_forms are valid # # test that the other_contacts_forms and no_other_contacts_forms are valid
if not self.is_valid(forms[1:]): # if not self.is_valid(forms[1:]):
all_forms_valid = False # all_forms_valid = False
else: # else:
logger.info("has no other contacts") # logger.info("has no other contacts")
# remove data from each other_contacts_form # # remove data from each other_contacts_form
other_contacts_forms.remove_form_data() # other_contacts_forms.remove_form_data()
# test that the other_contacts_forms and no_other_contacts_forms are valid # # test that the other_contacts_forms and no_other_contacts_forms are valid
if not self.is_valid(forms[1:]): # if not self.is_valid(forms[1:]):
all_forms_valid = False # all_forms_valid = False
else: # else:
all_forms_valid = False # all_forms_valid = False
if all_forms_valid: # if all_forms_valid:
logger.info("all forms are valid") # logger.info("all forms are valid")
# always save progress # # always save progress
self.save(forms) # self.save(forms)
else: # else:
context = self.get_context_data() # context = self.get_context_data()
context["forms"] = forms # context["forms"] = forms
return render(request, self.template_name, context) # return render(request, self.template_name, context)
# if user opted to save their progress, # # if user opted to save their progress,
# return them to the page they were already on # # return them to the page they were already on
if button == "save": # if button == "save":
messages.success(request, "Your progress has been saved!") # messages.success(request, "Your progress has been saved!")
return self.goto(self.steps.current) # return self.goto(self.steps.current)
# if user opted to save progress and return, # # if user opted to save progress and return,
# return them to the home page # # return them to the home page
if button == "save_and_return": # if button == "save_and_return":
return HttpResponseRedirect(reverse("home")) # return HttpResponseRedirect(reverse("home"))
# otherwise, proceed as normal # # otherwise, proceed as normal
return self.goto_next_step() # return self.goto_next_step()
# def post(self, request, *args, **kwargs) -> HttpResponse: # def post(self, request, *args, **kwargs) -> HttpResponse:
# parent_form = forms.OtherContactsYesNoForm(request.POST, **kwargs) # parent_form = forms.OtherContactsYesNoForm(request.POST, **kwargs)
@ -567,44 +567,37 @@ class OtherContacts(ApplicationWizard):
# no_other_contacts_form.data = {} # no_other_contacts_form.data = {}
# return super().post(request, *args, **kwargs) # return super().post(request, *args, **kwargs)
# def is_valid(self, forms: list) -> bool: def is_valid(self, forms: list) -> bool:
# """Returns True if all forms in the wizard are valid.""" """Overrides default behavior defined in ApplicationWizard.
# if forms[0].is_valid(): Depending on value in other_contacts_yes_no_form, marks forms in
# # test for has_contacts other_contacts or no_other_contacts for deletion. Then validates
# if forms[0].cleaned_data.get('has_other_contacts'): all forms.
# logger.info("testing validity on other contacts") """
# validity_list = [] 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 all_forms_valid = True
# for form in forms[1:-1]: # test first for yes_no_form validity
# # Check if the form is valid and append the result to the validity_list if other_contacts_yes_no_form.is_valid():
# logger.info(f"testing validity of form of type {form.__class__.__name__}") logger.info("yes no form is valid")
# validity_list.append(form.is_valid()) # test for has_contacts
if other_contacts_yes_no_form.cleaned_data.get('has_other_contacts'):
# # Check if all elements in validity_list are True logger.info("has other contacts")
# return all(validity_list) # remove data from no_other_contacts_form and set
# # return all(form.is_valid() for form in forms[2:-2]) # form to always_valid
# else: no_other_contacts_form.mark_form_for_deletion()
# logger.info("testing validity on no other contacts") # test that the other_contacts_forms and no_other_contacts_forms are valid
# return forms[-1].is_valid() all_forms_valid = all(form.is_valid() for form in forms[1:])
# # if has contacts , return if next length-2 are valid else:
# # else return last form in list is valid logger.info("has no other contacts")
# else: # remove data from each other_contacts_form
# return False other_contacts_forms.mark_formset_for_deletion()
# # are_valid = (form.is_valid() for form in forms) # test that the other_contacts_forms and no_other_contacts_forms are valid
# # return all(are_valid) all_forms_valid = all(form.is_valid() for form in forms[1:])
else:
all_forms_valid = False
# def save(self, forms: list): return all_forms_valid
# """
# 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)
class NoOtherContacts(ApplicationWizard): class NoOtherContacts(ApplicationWizard):