From 121c85d7b424e4b3bc3a432ca88696be004fb772 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 3 Oct 2019 11:42:52 +0200
Subject: [PATCH] fields: render template for title and subtitle (#36620)

---
 tests/test_fields.py     | 66 ++++++++++++++++++++++++++++++++++++++++
 tests/test_form_pages.py | 26 ++++++++++++++++
 wcs/fields.py            | 15 +++------
 wcs/forms/common.py      |  7 +++--
 4 files changed, 102 insertions(+), 12 deletions(-)

diff --git a/tests/test_fields.py b/tests/test_fields.py
index a1f6fae7..96660406 100644
--- a/tests/test_fields.py
+++ b/tests/test_fields.py
@@ -162,6 +162,39 @@ def test_title():
     field.add_to_form(form)
     assert '<h3 data-field-id="None" class="test">Foobar</h3>' in str(form.render())
 
+    # test for variable substitution
+    pub.substitutions.feed(MockSubstitutionVariables())
+    field = fields.TitleField(label='{{ bar }}')
+    form = Form(use_tokens=False)
+    field.add_to_form(form)
+    assert '<h3 data-field-id="None">Foobar</h3>' in str(form.render())
+
+    pub.substitutions.feed(MockSubstitutionVariables())
+    field = fields.TitleField(label='[bar]')
+    form = Form(use_tokens=False)
+    field.add_to_form(form)
+    assert '<h3 data-field-id="None">Foobar</h3>' in str(form.render())
+
+    # test for proper escaping of substitution variables
+    pub.substitutions.feed(MockSubstitutionVariables())
+    field = fields.TitleField(label='{{ foo }}')
+    form = Form(use_tokens=False)
+    field.add_to_form(form)
+    assert '<h3 data-field-id="None">1 &lt; 3</h3>' in str(form.render())
+
+    pub.substitutions.feed(MockSubstitutionVariables())
+    field = fields.TitleField(label='[foo]')
+    form = Form(use_tokens=False)
+    field.add_to_form(form)
+    assert '<h3 data-field-id="None">1 &lt; 3</h3>' in str(form.render())
+
+    # test for html content
+    field = fields.TitleField(label='<i>Foobar&eacute;</i>')
+    form = Form(use_tokens=False)
+    field.add_to_form(form)
+    assert '<i>Foobar&eacute;</i>' in str(form.render())
+    assert field.unhtmled_label == 'Foobaré'
+
 def test_subtitle():
     field = fields.SubtitleField(label='Foobar')
     form = Form(use_tokens=False)
@@ -173,6 +206,39 @@ def test_subtitle():
     field.add_to_form(form)
     assert '<h4 data-field-id="None" class="test">Foobar</h4>' in str(form.render())
 
+    # test for variable substitution
+    pub.substitutions.feed(MockSubstitutionVariables())
+    field = fields.SubtitleField(label='{{ bar }}')
+    form = Form(use_tokens=False)
+    field.add_to_form(form)
+    assert '<h4 data-field-id="None">Foobar</h4>' in str(form.render())
+
+    pub.substitutions.feed(MockSubstitutionVariables())
+    field = fields.SubtitleField(label='[bar]')
+    form = Form(use_tokens=False)
+    field.add_to_form(form)
+    assert '<h4 data-field-id="None">Foobar</h4>' in str(form.render())
+
+    # test for proper escaping of substitution variables
+    pub.substitutions.feed(MockSubstitutionVariables())
+    field = fields.SubtitleField(label='{{ foo }}')
+    form = Form(use_tokens=False)
+    field.add_to_form(form)
+    assert '<h4 data-field-id="None">1 &lt; 3</h4>' in str(form.render())
+
+    pub.substitutions.feed(MockSubstitutionVariables())
+    field = fields.SubtitleField(label='[foo]')
+    form = Form(use_tokens=False)
+    field.add_to_form(form)
+    assert '<h4 data-field-id="None">1 &lt; 3</h4>' in str(form.render())
+
+    # test for html content
+    field = fields.SubtitleField(label='<i>Foobar&eacute;</i>')
+    form = Form(use_tokens=False)
+    field.add_to_form(form)
+    assert '<i>Foobar&eacute;</i>' in str(form.render())
+    assert field.unhtmled_label == 'Foobaré'
+
 def test_comment():
     field = fields.CommentField(label='Foobar')
     form = Form(use_tokens=False)
diff --git a/tests/test_form_pages.py b/tests/test_form_pages.py
index c80bae61..0173d436 100644
--- a/tests/test_form_pages.py
+++ b/tests/test_form_pages.py
@@ -513,6 +513,32 @@ def test_form_multi_page(pub):
         data = formdef.data_class().get(data_id)
         assert data.data == {'1': 'foo', '3': 'bar'}
 
+
+def test_form_multi_page_title_and_subtitle_as_template(pub):
+    formdef = create_formdef()
+    formdef.fields = [fields.PageField(id='0', label='1st page', type='page'),
+            fields.StringField(id='1', label='string', varname='foo'),
+            fields.PageField(id='2', label='2nd page', type='page'),
+            fields.TitleField(id='4', label='title of seconde page {{ form_var_foo }}',
+                              type='title'),
+            fields.SubtitleField(id='5', label='subtitle of second page {{ form_var_foo }}',
+                                 type='subtitle'),
+            fields.StringField(id='3', label='string 2')]
+    formdef.store()
+    resp = get_app(pub).get('/test/')
+    formdef.data_class().wipe()
+    resp.form['f1'] = '35 < 42'
+    resp = resp.form.submit('submit')
+    resp.form['f3'] = 'bar'
+    resp = resp.form.submit('submit')
+    assert 'Check values then click submit.' in resp.body
+    resp = resp.form.submit('submit').follow()
+    assert 'The form has been recorded' in resp.body
+    body = resp.body
+    assert '<div class="title "><h3>title of seconde page 35 &lt; 42</h3></div>' in resp.body
+    assert '<div class="subtitle "><h4>subtitle of second page 35 &lt; 42</h4></div>' in resp.body
+
+
 def test_form_multi_page_condition(pub):
     formdef = create_formdef()
     formdef.fields = [fields.PageField(id='0', label='1st page', type='page'),
diff --git a/wcs/fields.py b/wcs/fields.py
index dd96fa8e..1a8eabce 100644
--- a/wcs/fields.py
+++ b/wcs/fields.py
@@ -569,12 +569,15 @@ def register_field_class(klass):
 class TitleField(Field):
     key = 'title'
     description = N_('Title')
+    html_template = '<h3%s>%s</h3>'
 
     def add_to_form(self, form, value = None):
+        import wcs.workflows
         extra_attributes = ' data-field-id="%s"' % self.id
         if self.extra_css_class:
             extra_attributes += ' class="%s"' % self.extra_css_class
-        widget = HtmlWidget(htmltext('<h3%s>%s</h3>' % (extra_attributes, self.label)))
+        label = wcs.workflows.template_on_html_string(self.label)
+        widget = HtmlWidget(htmltext(self.html_template % (extra_attributes, label)))
         form.widgets.append(widget)
         return widget
     add_to_view_form = add_to_form
@@ -597,15 +600,7 @@ register_field_class(TitleField)
 class SubtitleField(TitleField):
     key = 'subtitle'
     description = N_('Subtitle')
-
-    def add_to_form(self, form, value = None):
-        extra_attributes = ' data-field-id="%s"' % self.id
-        if self.extra_css_class:
-            extra_attributes += ' class="%s"' % self.extra_css_class
-        widget = HtmlWidget(htmltext('<h4%s>%s</h4>' % (extra_attributes, self.label)))
-        form.widgets.append(widget)
-        return widget
-    add_to_view_form = add_to_form
+    html_template = '<h4%s>%s</h4>'
 
 register_field_class(SubtitleField)
 
diff --git a/wcs/forms/common.py b/wcs/forms/common.py
index 76cf5d6e..44b60959 100644
--- a/wcs/forms/common.py
+++ b/wcs/forms/common.py
@@ -384,6 +384,7 @@ class FormStatusPage(Directory, FormTemplateMixin):
         return r.getvalue()
 
     def display_fields(self, fields=None, form_url='', include_unset_required_fields=False):
+        import wcs.workflows
         if fields is None:
             fields = self.formdef.fields
         r = TemplateIO(html=True)
@@ -454,11 +455,13 @@ class FormStatusPage(Directory, FormTemplateMixin):
                 continue
 
             if f.type == 'title':
-                r += htmltext('<div class="title %s"><h3>%s</h3></div>') % (f.extra_css_class or '', f.label)
+                label = wcs.workflows.template_on_html_string(f.label)
+                r += htmltext('<div class="title %s"><h3>%s</h3></div>' % (f.extra_css_class or '', label))
                 continue
 
             if f.type == 'subtitle':
-                r += htmltext('<div class="subtitle %s"><h4>%s</h4></div>') % (f.extra_css_class or '', f.label)
+                label = wcs.workflows.template_on_html_string(f.label)
+                r += htmltext('<div class="subtitle %s"><h4>%s</h4></div>' % (f.extra_css_class or '', label))
                 continue
 
             css_classes = ['field', 'field-type-%s' % f.key]
-- 
2.20.1

