From 30f4da0ea226be1e3e56455d0b47a792f731d221 Mon Sep 17 00:00:00 2001 From: Dale Cannon <118175145+dalecannon@users.noreply.github.com> Date: Tue, 3 Oct 2023 16:55:40 +0100 Subject: [PATCH] TP2000-1067 Fix measure edit footnote save (#1050) * Tidy up template * Move constants, rename variable, re-order code * Catch footnote submitted directly on saving * Add test * Amend comment * Remove check for POST in request --- measures/constants.py | 4 + measures/forms.py | 26 ++++-- .../measures/create-footnotes-formset.jinja | 87 +++++++++---------- measures/tests/conftest.py | 2 +- measures/tests/test_forms.py | 6 +- measures/tests/test_views.py | 25 ++++++ measures/views.py | 16 ++-- 7 files changed, 100 insertions(+), 66 deletions(-) diff --git a/measures/constants.py b/measures/constants.py index afaf7318b..5f1e6d495 100644 --- a/measures/constants.py +++ b/measures/constants.py @@ -13,3 +13,7 @@ class MeasureEditSteps(models.TextChoices): "geographical_area_exclusions", "Geographical area exclusions", ) + + +MEASURE_CONDITIONS_FORMSET_PREFIX = "measure-conditions-formset" +MEASURE_COMMODITIES_FORMSET_PREFIX = "measure_commodities_duties_formset" diff --git a/measures/forms.py b/measures/forms.py index fd4608f28..969d7f010 100644 --- a/measures/forms.py +++ b/measures/forms.py @@ -45,6 +45,8 @@ from geo_areas.models import GeographicalArea from geo_areas.utils import get_all_members_of_geo_groups from measures import models +from measures.constants import MEASURE_COMMODITIES_FORMSET_PREFIX +from measures.constants import MEASURE_CONDITIONS_FORMSET_PREFIX from measures.constants import MeasureEditSteps from measures.models import MeasureExcludedGeographicalArea from measures.parsers import DutySentenceParser @@ -58,9 +60,6 @@ logger = logging.getLogger(__name__) -MEASURE_CONDITIONS_FORMSET_PREFIX = "measure-conditions-formset" -MEASURE_COMMODITIES_FORMSET_PREFIX = "measure_commodities_duties_formset" - class MeasureGeoAreaInitialDataMixin(FormSetSubmitMixin): def get_geo_area_initial(self): @@ -606,10 +605,8 @@ def clean(self): return cleaned_data def save(self, commit=True): - """Get the measure instance after form submission, get from session - storage any footnote pks created via the Footnote formset and any pks - not removed from the measure after editing and create footnotes via - FootnoteAssociationMeasure.""" + """Updates a measure instance's geographical area and exclusions, + duties, and footnote associations following form submission.""" instance = super().save(commit=False) if commit: instance.save() @@ -685,10 +682,21 @@ def save(self, commit=True): "component_measure", ) + # Footnotes added via "Add another footnote" button footnote_pks = [ - dct["footnote"] - for dct in self.request.session.get(f"formset_initial_{sid}", []) + form["footnote"] + for form in self.request.session.get(f"formset_initial_{sid}", []) ] + + # Footnote submitted directly via "Save" button + form_footnote = self.request.POST.get( + f"form-{len(footnote_pks)}-footnote", + None, + ) + if form_footnote: + footnote_pks.append(form_footnote) + + # Footnotes already on measure footnote_pks.extend(self.request.session.get(f"instance_footnotes_{sid}", [])) self.request.session.pop(f"formset_initial_{sid}", None) diff --git a/measures/jinja2/measures/create-footnotes-formset.jinja b/measures/jinja2/measures/create-footnotes-formset.jinja index 1229df6b6..b58867697 100644 --- a/measures/jinja2/measures/create-footnotes-formset.jinja +++ b/measures/jinja2/measures/create-footnotes-formset.jinja @@ -1,51 +1,46 @@ -{% with form_footnotes = form.request.session["instance_footnotes_" ~ form.instance.sid] %} - {% if form_footnotes %} - {% set table_rows = [] %} - {% for footnote in footnotes %} - {% if footnote.pk in form.request.session["instance_footnotes_"~form.instance.sid]%} - {% set footnote_link -%} - {{ footnote|string }} - {%- endset %} - {% set remove_button -%} - + Add another footnote +