Project

General

Profile

0001-forms-switch-comment-field-to-render-using-a-templat.patch

Frédéric Péters, 11 Feb 2019 02:26 PM

Download (10.3 KB)

View differences:

Subject: [PATCH] forms: switch comment field to render using a template
 (#30530)

 tests/test_backoffice_pages.py | 12 ++++++------
 tests/test_fields.py           | 15 +++++++--------
 tests/test_form_pages.py       |  8 ++++----
 wcs/fields.py                  | 32 ++++++++++++++------------------
 wcs/qommon/form.py             | 19 +++++++++++++++++++
 5 files changed, 50 insertions(+), 36 deletions(-)
tests/test_backoffice_pages.py
4257 4257
    app = login(get_app(pub))
4258 4258

  
4259 4259
    resp = app.get('/backoffice/submission/test/')
4260
    assert '<p data-field-id="7" class="comment-field ">XbarY</p>' in resp.body
4260
    assert resp.html.find('div', {'data-field-id': '7'}).text.strip() == 'XbarY'
4261 4261

  
4262 4262
    # check with publisher variable in named webservice call
4263 4263
    if not pub.site_options.has_section('variables'):
......
4272 4272
    wscall.store()
4273 4273

  
4274 4274
    resp = app.get('/backoffice/submission/test/')
4275
    assert '<p data-field-id="7" class="comment-field ">XbarY</p>' in resp.body
4275
    assert resp.html.find('div', {'data-field-id': '7'}).text.strip() == 'XbarY'
4276 4276

  
4277 4277
    # django-templated URL
4278 4278
    wscall.request = {'url': '{{ example_url }}json'}
4279 4279
    wscall.store()
4280 4280
    resp = app.get('/backoffice/submission/test/')
4281
    assert '<p data-field-id="7" class="comment-field ">XbarY</p>' in resp.body
4281
    assert resp.html.find('div', {'data-field-id': '7'}).text.strip() == 'XbarY'
4282 4282

  
4283 4283
    # webservice call in django template
4284 4284
    formdef.fields = [
......
4287 4287
    formdef.store()
4288 4288
    formdef.data_class().wipe()
4289 4289
    resp = app.get('/backoffice/submission/test/')
4290
    assert '<p data-field-id="7" class="comment-field ">dja-bar-ngo</p>' in resp.body
4290
    assert resp.html.find('div', {'data-field-id': '7'}).text.strip() == 'dja-bar-ngo'
4291 4291

  
4292 4292
def test_backoffice_session_var(pub):
4293 4293
    open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w').write('''[options]
......
4311 4311
    resp = app.get('/backoffice/submission/test/?session_var_foo=bar')
4312 4312
    assert resp.location.endswith('/backoffice/submission/test/')
4313 4313
    resp = resp.follow()
4314
    assert '<p data-field-id="7" class="comment-field ">XbarY</p>' in resp.body
4314
    assert resp.html.find('div', {'data-field-id': '7'}).text.strip() == 'XbarY'
4315 4315

  
4316 4316
    # django template
4317 4317
    formdef.fields = [
......
4322 4322
    resp = app.get('/backoffice/submission/test/?session_var_foo=jang')
4323 4323
    assert resp.location.endswith('/backoffice/submission/test/')
4324 4324
    resp = resp.follow()
4325
    assert '<p data-field-id="7" class="comment-field ">django</p>' in resp.body
4325
    assert resp.html.find('div', {'data-field-id': '7'}).text.strip() == 'django'
4326 4326

  
4327 4327
def test_backoffice_display_message(pub):
4328 4328
    user = create_user(pub)
tests/test_fields.py
3 3
import re
4 4
import shutil
5 5

  
6
from bs4 import BeautifulSoup
6 7
import pytest
7 8

  
8 9
from quixote import cleanup
......
155 156
    field = fields.CommentField(label='Foobar')
156 157
    form = Form(use_tokens=False)
157 158
    field.add_to_form(form)
158
    assert '<p data-field-id="None" class="comment-field ">Foobar</p>' in str(form.render())
159
    assert BeautifulSoup(str(form.render())).find('div').text.strip() == 'Foobar'
159 160

  
160 161
    field = fields.CommentField(label='Foo\n\nBar\n\nBaz')
161 162
    form = Form(use_tokens=False)
162 163
    field.add_to_form(form)
163
    assert '<p>Foo</p>\n<p>Bar</p>\n<p>Baz</p>' in str(form.render())
164
    assert '<div data-field-id="None" class="comment-field "' in str(form.render())
164
    assert str(BeautifulSoup(str(form.render())).find('div')) == '<div class="comment-field">\n<p>Foo</p>\n<p>Bar</p>\n<p>Baz</p>\n</div>'
165 165

  
166 166
    # test for variable substitution
167 167
    pub.substitutions.feed(MockSubstitutionVariables())
168 168
    field = fields.CommentField(label='{{ bar }}')
169 169
    form = Form(use_tokens=False)
170 170
    field.add_to_form(form)
171
    assert '<p data-field-id="None" class="comment-field ">Foobar</p>' in str(form.render())
171
    assert BeautifulSoup(str(form.render())).find('div').text.strip() == 'Foobar'
172 172

  
173 173
    field = fields.CommentField(label='[bar]')
174 174
    form = Form(use_tokens=False)
175 175
    field.add_to_form(form)
176
    assert '<p data-field-id="None" class="comment-field ">Foobar</p>' in str(form.render())
176
    assert BeautifulSoup(str(form.render())).find('div').text.strip() == 'Foobar'
177 177

  
178 178
    # test for proper escaping of substitution variables
179 179
    field = fields.CommentField(label='{{ foo }}')
180 180
    form = Form(use_tokens=False)
181 181
    field.add_to_form(form)
182
    assert '<p data-field-id="None" class="comment-field ">1 &lt; 3</p>' in str(form.render())
182
    assert '1 &lt; 3' in str(form.render())
183 183

  
184 184
    field = fields.CommentField(label='[foo]')
185 185
    form = Form(use_tokens=False)
186 186
    field.add_to_form(form)
187
    assert '<p data-field-id="None" class="comment-field ">1 &lt; 3</p>' in str(form.render())
187
    assert '1 &lt; 3' in str(form.render())
188 188

  
189 189
    # test for html content
190 190
    field = fields.CommentField(label='<p>Foobar</p>')
191 191
    form = Form(use_tokens=False)
192 192
    field.add_to_form(form)
193 193
    assert '<p>Foobar</p>' in str(form.render())
194
    assert '<div data-field-id="None" class="comment-field "' in str(form.render())
195 194
    assert field.unhtmled_label == 'Foobar'
196 195

  
197 196
    field = fields.CommentField(label='<p>Foobar&eacute;</p>')
tests/test_form_pages.py
5476 5476
            required=True, varname='baz',
5477 5477
            condition={'type': 'django', 'value': '"Pêche" in form_var_foo'}),
5478 5478
        fields.CommentField(type='comment', id='4', label='{{form_var_foo}}'),
5479
        fields.CommentField(type='comment', id='5', label='{% if "Pêche" in form_var_foo %}OK{% endif %}'),
5479
        fields.CommentField(type='comment', id='5', label='{% if "Pêche" in form_var_foo %}CHECK OK{% endif %}'),
5480 5480
    ]
5481 5481

  
5482 5482
    workflow = Workflow(name='test')
5483 5483
    st1 = workflow.add_status('Status1', 'st1')
5484 5484
    display1 = DisplayMessageWorkflowStatusItem()
5485
    display1.message = 'Message {% if "Pêche" in form_var_foo %}OK{% endif %}'
5485
    display1.message = 'Message {% if "Pêche" in form_var_foo %}CHECK OK{% endif %}'
5486 5486
    display1.to = []
5487 5487
    st1.items.append(display1)
5488 5488
    display1.parent = st1
......
5503 5503
    assert 'f3' in resp.form.fields  # check it's ok in field condition
5504 5504
    resp.form['f3'] = 'hop'
5505 5505
    assert '>Poire, Pêche<' in resp.body  # check it's displayed correctly
5506
    assert '>OK<' in resp.body  # check it's ok in template condition
5506
    assert 'CHECK OK' in resp.body  # check it's ok in template condition
5507 5507
    resp = resp.form.submit('submit')
5508 5508
    assert 'Check values then click submit.' in resp.body
5509 5509
    resp = resp.form.submit('submit').follow()
5510
    assert '<p>Message OK</p>' in resp.body  # check it's ok in workflow template
5510
    assert '<p>Message CHECK OK</p>' in resp.body  # check it's ok in workflow template
5511 5511

  
5512 5512
def test_field_live_condition(pub):
5513 5513
    FormDef.wipe()
wcs/fields.py
607 607
    key = 'comment'
608 608
    description = N_('Comment')
609 609

  
610
    def add_to_form(self, form, value = None):
611
        tag_attributes = 'data-field-id="%s" class="comment-field %s"' % (
612
                self.id, self.extra_css_class or '')
613

  
614
        label = self.get_html_content()
615

  
610
    def add_to_form(self, form, value=None):
616 611
        import wcs.workflows
612
        label = self.get_html_content()
617 613
        label = wcs.workflows.template_on_html_string(label)
618

  
619
        enclosing_tag = 'p'
620
        for block_level in ('p', 'div', 'ul', 'ol', 'hr'):
621
            if '<' + block_level in label:
622
                enclosing_tag = 'div'
623
                break
624

  
625
        widget = HtmlWidget(htmltext('<%s %s>%s</%s>' % (
626
            enclosing_tag, tag_attributes, label, enclosing_tag)))
614
        widget = CommentWidget(
615
                content=label,
616
                extra_css_class=self.extra_css_class)
627 617
        form.widgets.append(widget)
628 618
        return widget
629 619

  
......
631 621
        pass
632 622

  
633 623
    def get_html_content(self):
634
        if self.label and '\n\n' in self.label and self.label[0] != '<':
635
            return '<p>' + re.sub('\n\n+', '</p>\n<p>', self.label) + '</p>'
636
        return self.label
624
        if not self.label:
625
            return ''
626
        if self.label.startswith('<'):
627
            return self.label
628
        if '\n\n' in self.label:
629
            # blank lines to paragraphs
630
            label = '</p>\n<p>'.join([str(htmlescape(x)) for x in re.split('\n\n+', self.label)])
631
            return '<p>' + label + '</p>'
632
        return '<p>%s</p>' % str(htmlescape(self.label))
637 633

  
638 634
    def fill_admin_form(self, form):
639 635
        if self.label and (self.label[0] != '<' and '[end]' in self.label):
wcs/qommon/form.py
431 431
    def clear_error(self, request=None):
432 432
        pass
433 433

  
434

  
435
class CommentWidget(Widget):
436
    template_name = 'qommon/forms/widgets/comment.html'
437
    name = None
438

  
439
    def __init__(self, content, extra_css_class):
440
        self.content = content
441
        self.extra_css_class = extra_css_class
442

  
443
    def has_error(self, request):
444
        return False
445

  
446
    def parse(self, *args):
447
        pass
448

  
449
    def clear_error(self, request=None):
450
        pass
451

  
452

  
434 453
class CompositeWidget(quixote.form.CompositeWidget):
435 454
    def render_as_thead(self):
436 455
        r = TemplateIO(html=True)
437
-