From ac13cbd96972e76c09af98ba81b5d774fbd8727a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Sat, 18 Dec 2021 15:50:11 +0100
Subject: [PATCH] general: pass quixote form as "html_form" to templates
 (#59882)

"form" is kept for legacy external templates (in publik-base-theme), to
be removed once those templates are updated to use html_form.
---
 wcs/backoffice/data_management.py                       | 2 +-
 wcs/forms/actions.py                                    | 2 +-
 wcs/forms/root.py                                       | 6 ++++--
 wcs/qommon/template.py                                  | 7 ++++---
 wcs/templates/wcs/action.html                           | 2 +-
 wcs/templates/wcs/backoffice/card-data-import-form.html | 2 +-
 wcs/templates/wcs/formdata_filling.html                 | 2 +-
 wcs/templates/wcs/formdata_popup_filling.html           | 2 +-
 wcs/templates/wcs/formdata_validation.html              | 2 +-
 9 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/wcs/backoffice/data_management.py b/wcs/backoffice/data_management.py
index 868a93b8c..85e504166 100644
--- a/wcs/backoffice/data_management.py
+++ b/wcs/backoffice/data_management.py
@@ -207,7 +207,7 @@ class CardPage(FormPage):
 
         get_response().breadcrumb.append(('import_csv', _('Import CSV')))
         html_top('data_management', _('Import CSV'))
-        context['form'] = form
+        context['html_form'] = form
 
         for field in self.get_import_csv_fields():
             if not hasattr(field, 'required'):
diff --git a/wcs/forms/actions.py b/wcs/forms/actions.py
index 3a56292a6..5779249b8 100644
--- a/wcs/forms/actions.py
+++ b/wcs/forms/actions.py
@@ -68,7 +68,7 @@ class ActionDirectory(Directory, FormTemplateMixin):
             return self.submit()
         context = {
             'view': self,
-            'form': form,
+            'html_form': form,
         }
         return template.QommonTemplateResponse(
             templates=list(self.get_formdef_template_variants(self.templates)), context=context
diff --git a/wcs/forms/root.py b/wcs/forms/root.py
index 39757bd8d..2b3322d32 100644
--- a/wcs/forms/root.py
+++ b/wcs/forms/root.py
@@ -621,14 +621,15 @@ class FormPage(Directory, FormTemplateMixin):
             context['tracking_code_box'] = lambda: self.tracking_code_box(data, magictoken)
         self.modify_filling_context(context, page, data)
 
+        context['html_form'] = form
         if self.is_popup:
-            context['form_obj'] = form
             return template.QommonTemplateResponse(
                 templates=list(self.get_formdef_template_variants(self.popup_filling_templates)),
                 context=context,
                 is_django_native=True,
             )
         else:
+            # legacy
             context['form'] = form
 
         return template.QommonTemplateResponse(
@@ -1649,10 +1650,11 @@ class FormPage(Directory, FormTemplateMixin):
 
         context = {
             'view': self,
-            'form': form,
+            'html_form': form,
             'form_side': lambda: self.form_side(step_no=1, page=None, data=data, magictoken=magictoken),
             'steps': lambda: self.step(1, None),
         }
+        context['form'] = form  # legacy
         if self.has_draft_support() and data:
             context['tracking_code_box'] = lambda: self.tracking_code_box(data, magictoken)
         return template.QommonTemplateResponse(
diff --git a/wcs/qommon/template.py b/wcs/qommon/template.py
index 9a0d70261..5fec74d92 100644
--- a/wcs/qommon/template.py
+++ b/wcs/qommon/template.py
@@ -422,10 +422,11 @@ class QommonTemplateResponse:
 
     def add_media(self):
         # run add_media so we get them in the page <head>
-        if 'form' in self.context:
+        if 'html_form' in self.context:
+            self.context['html_form'].add_media()
+        if 'form' in self.context and hasattr(self.context['form'], 'add_media'):
+            # legacy name, conflicting with formdata "form*" variables
             self.context['form'].add_media()
-        if 'form_obj' in self.context:  # "add card" popup case
-            self.context['form_obj'].add_media()
 
 
 class TemplateError(Exception):
diff --git a/wcs/templates/wcs/action.html b/wcs/templates/wcs/action.html
index 9e1605c8a..aed4ca352 100644
--- a/wcs/templates/wcs/action.html
+++ b/wcs/templates/wcs/action.html
@@ -10,6 +10,6 @@
 <p>
 {% trans "Please confirm action." %}
 </p>
-{{ form.render|safe }}
+{{ html_form.render|safe }}
 {% endif %}
 {% endblock %}
diff --git a/wcs/templates/wcs/backoffice/card-data-import-form.html b/wcs/templates/wcs/backoffice/card-data-import-form.html
index 7cb7efdbe..bbe89118d 100644
--- a/wcs/templates/wcs/backoffice/card-data-import-form.html
+++ b/wcs/templates/wcs/backoffice/card-data-import-form.html
@@ -22,6 +22,6 @@
 {% else %}
 <p>{% trans "You can add data to this card by uploading a file." %}</p>
 <p><a href="data-sample-csv">{% trans "Download sample file for this card" %}</a></p>
-{{ form.render|safe }}
+{{ html_form.render|safe }}
 {% endif %}
 {% endblock %}
diff --git a/wcs/templates/wcs/formdata_filling.html b/wcs/templates/wcs/formdata_filling.html
index ebe7cd0b5..c593c7e56 100644
--- a/wcs/templates/wcs/formdata_filling.html
+++ b/wcs/templates/wcs/formdata_filling.html
@@ -44,7 +44,7 @@ This form is limited to one per user and the associated user has already one.
 {% endif %}
 {% endblock %}
 
-{{ form.render|safe }}
+{{ html_form.render|safe }}
 {% endblock %}
 
 {% endblock %}
diff --git a/wcs/templates/wcs/formdata_popup_filling.html b/wcs/templates/wcs/formdata_popup_filling.html
index b16c410a7..37688f703 100644
--- a/wcs/templates/wcs/formdata_popup_filling.html
+++ b/wcs/templates/wcs/formdata_popup_filling.html
@@ -11,7 +11,7 @@
 {{ publisher.get_request.session.display_message|safe }}
 {% endblock %}
 
-{{ form_obj.render|safe }}
+{{ html_form.render|safe }}
 {% endblock %}
 
 {% block footer %}{% endblock %}
diff --git a/wcs/templates/wcs/formdata_validation.html b/wcs/templates/wcs/formdata_validation.html
index e703089f4..e1fa2ec5f 100644
--- a/wcs/templates/wcs/formdata_validation.html
+++ b/wcs/templates/wcs/formdata_validation.html
@@ -6,6 +6,6 @@
  <div class="infonotice">
  {% standard_text "check-before-submit" %}
  </div>
- {{ form.render|safe }}
+ {{ html_form.render|safe }}
 </div>
 {% endblock %}
-- 
2.34.1

