Project

General

Profile

0001-workflows-factorize-context-building-in-template_on_.patch

Benjamin Dauvergne, 14 October 2015 01:23 PM

Download (3.4 KB)

View differences:

Subject: [PATCH 1/3] workflows: factorize context building in
 template_on_formdata (#4292)

 wcs/workflows.py | 60 +++++++++++++++++++++++++++++++-------------------------
 1 file changed, 33 insertions(+), 27 deletions(-)
wcs/workflows.py
1398 1398
register_item_class(SendmailWorkflowStatusItem)
1399 1399

  
1400 1400

  
1401
def template_context_on_formdata(formdata=None, process=None):
1402
    ctx = {}
1403
    ctx.update(get_publisher().substitutions.get_context_variables())
1404
    if formdata:
1405
        ctx['url'] = formdata.get_url()
1406
        ctx['url_status'] = '%sstatus' % formdata.get_url()
1407
        ctx['details'] = formdata.formdef.get_detailed_email_form(formdata, ctx['url'])
1408
        ctx['name'] = formdata.formdef.name
1409
        ctx['number'] = formdata.id
1410
        if formdata.evolution and formdata.evolution[-1].comment:
1411
            ctx['comment'] = formdata.evolution[-1].comment
1412
        else:
1413
            ctx['comment'] = ''
1414
        ctx.update(formdata.get_as_dict())
1415

  
1416
        # compatibility vars
1417
        ctx['before'] = ctx.get('form_previous_status')
1418
        ctx['after'] = ctx.get('form_status')
1419
        ctx['evolution'] = ctx.get('form_evolution')
1420

  
1421
    if process:
1422
        for key in ctx:
1423
            ctx[key] = process(ctx[key])
1424

  
1425
    charset = get_publisher().site_charset
1426
    for k, v in ctx.items():
1427
        if isinstance(v, unicode):
1428
            ctx[k] = v.encode(charset, 'ignore')
1429
    return ctx
1430

  
1431

  
1401 1432
def template_on_html_string(template, process=None):
1402 1433
    return template_on_formdata(None, template, process=process,
1403 1434
            base_format=ezt.FORMAT_HTML)
......
1408 1439
    assert template is not None
1409 1440
    if not '[' in template:
1410 1441
        return template
1411
    dict = {}
1412
    dict.update(get_publisher().substitutions.get_context_variables())
1413
    if formdata:
1414
        dict['url'] = formdata.get_url()
1415
        dict['url_status'] = '%sstatus' % formdata.get_url()
1416
        dict['details'] = formdata.formdef.get_detailed_email_form(formdata, dict['url'])
1417
        dict['name'] = formdata.formdef.name
1418
        dict['number'] = formdata.id
1419
        if formdata.evolution and formdata.evolution[-1].comment:
1420
            dict['comment'] = formdata.evolution[-1].comment
1421
        else:
1422
            dict['comment'] = ''
1423
        dict.update(formdata.get_as_dict())
1424

  
1425
        # compatibility vars
1426
        dict['before'] = dict.get('form_previous_status')
1427
        dict['after'] = dict.get('form_status')
1428
        dict['evolution'] = dict.get('form_evolution')
1429

  
1430
    if process:
1431
        for key in dict:
1432
            dict[key] = process(dict[key])
1433 1442

  
1434
    charset = get_publisher().site_charset
1435
    for k, v in dict.items():
1436
        if isinstance(v, unicode):
1437
            dict[k] = v.encode(charset, 'ignore')
1443
    ctx = template_context_on_formdata(formdata, process=process)
1438 1444

  
1439 1445
    processor = ezt.Template(compress_whitespace=False)
1440 1446
    processor.parse(template or '', base_format=base_format)
1441 1447

  
1442 1448
    fd = StringIO()
1443
    processor.generate(fd, dict)
1449
    processor.generate(fd, ctx)
1444 1450

  
1445 1451
    return fd.getvalue()
1446 1452

  
1447
-