def test_replace_object(self): class Foo(object): def __init__(self): self.x = 2 result = replace_fields('test {foo.x}!', {'foo': Foo()}, errors='raise') self.assertEqual("test 2!", result)
def render_text(self, request, instance, context): if instance.text: # When a custom text is provided, use that. text = instance.text text = replace_fields(text, context, autoescape=False) return text else: # Otherwise, let the default implementation do it's work. # It will base the text version off the HTML code. return super(EmailTextPlugin, self).render_text(request, instance, context)
def render_html(self, request, instance, context): # Included in a DIV, so the next item will be displayed below it. html = mark_safe(u"<div>{0}</div>".format(instance.html)) html = replace_fields(html, context) # Do manually because render_html() was overwritten return html
def test_replace_unicode(self): result = replace_fields(u'Hello {aa} \xe9', {'aa': u'\xf6'}, errors='inline') self.assertEqual(u"Hello \xf6 \xe9", result)
def test_replace_invalid(self): result = replace_fields('Hello {aa} or {bb} and others', {'aa': 11}, errors='inline') self.assertEqual("Hello 11 or !!missing bb!! and others", result)
def test_replace_scalar_format(self): result = replace_fields('Hello {subject:s}!', {'subject': "TEST"}, errors='raise') self.assertEqual("Hello TEST!", result) result = replace_fields('Hello {aa:.02f}', {'aa': 1.5}, errors='raise') self.assertEqual("Hello 1.50", result)
def test_replace_scalar(self): result = replace_fields('Hello {subject}!', {'subject': "TEST"}, errors='raise') self.assertEqual("Hello TEST!", result) result = replace_fields('Hello {aa} or {bb} and others', {'aa': 11, 'bb': 22}, errors='raise') self.assertEqual("Hello 11 or 22 and others", result)