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 -%} - Remove - {%- endset %} - {{ table_rows.append([ - {"html": footnote_link}, - {"text": footnote.get_description().description|default("")}, - {"html": remove_button} - ]) or "" }} - {% endif %} - {% endfor %} - {{ govukTable({ - "head": [ - {"text": "ID"}, - {"text": "Description"}, - {"text": ""} - ], - "rows": table_rows, - "caption": "Footnotes currently assigned to the measure", - "captionClasses": "govuk-table__caption--m" - }) }} - {% else %} - No footnotes found. - - {% endif %} +{% with measure_footnotes = form.request.session["instance_footnotes_" ~ form.instance.sid] %} + {% if measure_footnotes %} + {% set table_rows = [] %} + {% for footnote in footnotes %} + {% if footnote.pk in measure_footnotes %} + {% set footnote_link -%} + {{ footnote|string }} + {%- endset %} + {% set remove_button -%} + Remove + {%- endset %} + {{ table_rows.append([ + {"html": footnote_link}, + {"text": footnote.get_description().description|default("")}, + {"html": remove_button} + ]) or "" }} + {% endif %} + {% endfor %} + {{ govukTable({ + "head": [ + {"text": "ID"}, + {"text": "Description"}, + {"text": ""} + ], + "rows": table_rows, + "caption": "Footnotes currently assigned to the measure", + "captionClasses": "govuk-table__caption--m" + }) }} + {% endif %} {% endwith %} {{ crispy(footnotes_formset.management_form, no_form_tags) }} - Add a footnote - Start typing the ID of the footnote or terms used in the description - {% for form in footnotes_formset %} - {{ crispy(form) }} - {% endfor %} - {% if footnotes_formset.data[footnotes_formset.prefix ~ "-ADD"] %} - {{ crispy(footnotes_formset.empty_form)|replace("__prefix__", footnotes_formset.forms|length)|safe }} - {% endif %} +{% for form in footnotes_formset %} + {{ crispy(form) }} +{% endfor %} + +{% if footnotes_formset.data[footnotes_formset.prefix ~ "-ADD"] %} + {{ crispy(footnotes_formset.empty_form)|replace("__prefix__", footnotes_formset.forms|length)|safe }} +{% endif %} - {% call govukFieldset({"legend": {}, "classes": "govuk-!-padding-top-3"}) %} - - Add another footnote - + Add another footnote +
No footnotes found.