Projet

Général

Profil

0001-templates-fix-encoding-handling-in-django-templates-.patch

Frédéric Péters, 20 décembre 2017 15:58

Télécharger (2,03 ko)

Voir les différences:

Subject: [PATCH] templates: fix encoding handling in django templates (#20831)

 tests/test_templates.py |  5 +++++
 wcs/qommon/template.py  | 10 +++++++++-
 2 files changed, 14 insertions(+), 1 deletion(-)
tests/test_templates.py
66 66
    assert tmpl.render({'foo': 'fou'}) == 'fou à vélo'
67 67
    assert tmpl.render({'foo': 'félé'}) == 'félé à vélo'
68 68

  
69
    tmpl = Template("{% if foo == 'félé' %}à vélo{% endif %}")
70
    assert tmpl.render() == ''
71
    assert tmpl.render({'foo': 'fou'}) == ''
72
    assert tmpl.render({'foo': 'félé'}) == 'à vélo'
73

  
69 74
    # ezt
70 75
    tmpl = Template('[foo] à vélo')
71 76
    assert tmpl.render() == '[foo] à vélo'
wcs/qommon/template.py
449 449
    raise TemplateError(message % ' '.join(parts))
450 450

  
451 451

  
452
class UnicodeDjangoContext(DjangoContext):
453
    def __getitem__(self, key):
454
        s = super(UnicodeDjangoContext, self).__getitem__(key)
455
        if isinstance(s, str):
456
            return unicode(s, 'utf-8')
457
        return s
458

  
459

  
452 460
class Template(object):
453 461
    def __init__(self, value, raises=False, ezt_format=ezt.FORMAT_RAW, ezt_only=False):
454 462
        '''Guess kind of template (Django or ezt), and parse it'''
......
482 490
        return str(self.value)
483 491

  
484 492
    def django_render(self, context={}):
485
        context = DjangoContext(context)
493
        context = UnicodeDjangoContext(context)
486 494
        try:
487 495
            rendered = self.template.render(context)
488 496
        except (DjangoTemplateSyntaxError, DjangoVariableDoesNotExist) as e:
489
-