示例#1
0
class TestTemplate(PecanTestCase):
    def setUp(self):
        super(TestTemplate, self).setUp()
        self.rf = RendererFactory()

    def test_available(self):
        self.assertTrue(self.rf.available('json'))
        self.assertFalse(self.rf.available('badrenderer'))

    def test_create_bad(self):
        self.assertEqual(self.rf.get('doesnotexist', '/'), None)

    def test_extra_vars(self):
        extra_vars = self.rf.extra_vars
        self.assertEqual(extra_vars.make_ns({}), {})

        extra_vars.update({'foo': 1})
        self.assertEqual(extra_vars.make_ns({}), {'foo': 1})

    def test_update_extra_vars(self):
        extra_vars = self.rf.extra_vars
        extra_vars.update({'foo': 1})

        self.assertEqual(extra_vars.make_ns({'bar': 2}), {'foo': 1, 'bar': 2})
        self.assertEqual(extra_vars.make_ns({'foo': 2}), {'foo': 2})
class TestTemplate(PecanTestCase):
    def setUp(self):
        super(TestTemplate, self).setUp()
        self.rf = RendererFactory()

    def test_available(self):
        self.assertTrue(self.rf.available('json'))
        self.assertFalse(self.rf.available('badrenderer'))

    def test_create_bad(self):
        self.assertEqual(self.rf.get('doesnotexist', '/'), None)

    def test_extra_vars(self):
        extra_vars = self.rf.extra_vars
        self.assertEqual(extra_vars.make_ns({}), {})

        extra_vars.update({'foo': 1})
        self.assertEqual(extra_vars.make_ns({}), {'foo': 1})

    def test_update_extra_vars(self):
        extra_vars = self.rf.extra_vars
        extra_vars.update({'foo': 1})

        self.assertEqual(extra_vars.make_ns({'bar': 2}), {'foo': 1, 'bar': 2})
        self.assertEqual(extra_vars.make_ns({'foo': 2}), {'foo': 2})
示例#3
0
class EmailTemplate(object):
    """
    A helper used to load file-based email templates and generate their
    text/html and text/plain representations.

    Can be passed a string template path relative to the app's template root,
    e.g., templates/emails/signup

    In this example, EmailTemplate will attempt to find a matching file for:

    * templates/email/signup.html
    * templates/email/signup.txt

    ...for generating both an HTML and plain/text version of the email.
    """

    __html_wrap__ = u'''
        <html>
        <body bgcolor="#ffffff" link="#0099cc" alink="#0099cc" vlink="#0099cc"
        leftmargin="0" topmargin="0" style="padding: 15px; font-family:
        Helvetica, Arial, sans-serif; font-size: 14px; line-height: 1.3em;
        text-align: left;">
            %s
        </body>
        </html>
    '''.strip()

    def __init__(self, template_file, template_path):
        self.template_file = template_file
        self.template_path = template_path
        self.renderers = RendererFactory()

    def __render__(self, fmt, ns):
        template_file = '%s.%s' % (self.template_file, fmt)
        try:
            return self.renderers.get('mako', self.template_path).render(
                template_file,
                ns
            )
        except exceptions.TopLevelLookupException:
            pass  # This will cause the `__render__` call to return `None`

    def html(self, ns):
        body = self.__render__('html', ns)
        if body:
            return (self.__html_wrap__ % body.strip())

    def text(self, ns):
        body = self.__render__('txt', ns)
        if body:
            return body.strip()
示例#4
0
class EmailTemplate(object):
    """
    A helper used to load file-based email templates and generate their
    text/html and text/plain representations.

    Can be passed a string template path relative to the app's template root,
    e.g., templates/emails/signup

    In this example, EmailTemplate will attempt to find a matching file for:

    * templates/email/signup.html
    * templates/email/signup.txt

    ...for generating both an HTML and plain/text version of the email.
    """

    __html_wrap__ = u'''
        <html>
        <body bgcolor="#ffffff" link="#0099cc" alink="#0099cc" vlink="#0099cc"
        leftmargin="0" topmargin="0" style="padding: 15px; font-family:
        Helvetica, Arial, sans-serif; font-size: 14px; line-height: 1.3em;
        text-align: left;">
            %s
        </body>
        </html>
    '''.strip()

    def __init__(self, template_file, template_path):
        self.template_file = template_file
        self.template_path = template_path
        self.renderers = RendererFactory()

    def __render__(self, fmt, ns):
        template_file = '%s.%s' % (self.template_file, fmt)
        try:
            return self.renderers.get('mako', self.template_path).render(
                template_file, ns)
        except exceptions.TopLevelLookupException:
            pass  # This will cause the `__render__` call to return `None`

    def html(self, ns):
        body = self.__render__('html', ns)
        if body:
            return (self.__html_wrap__ % body.strip())

    def text(self, ns):
        body = self.__render__('txt', ns)
        if body:
            return body.strip()
示例#5
0
class TestTemplate(TestCase):
    def setUp(self):
        self.rf = RendererFactory()

    def test_available(self):
        self.assertTrue(self.rf.available("json"))
        self.assertFalse(self.rf.available("badrenderer"))

    def test_create_bad(self):
        self.assertEqual(self.rf.get("doesnotexist", "/"), None)

    def test_extra_vars(self):
        extra_vars = self.rf.extra_vars
        self.assertEqual(extra_vars.make_ns({}), {})

        extra_vars.update({"foo": 1})
        self.assertEqual(extra_vars.make_ns({}), {"foo": 1})

    def test_update_extra_vars(self):
        extra_vars = self.rf.extra_vars
        extra_vars.update({"foo": 1})

        self.assertEqual(extra_vars.make_ns({"bar": 2}), {"foo": 1, "bar": 2})
        self.assertEqual(extra_vars.make_ns({"foo": 2}), {"foo": 2})
示例#6
0
 def setUp(self):
     self.rf = RendererFactory()
示例#7
0
 def __init__(self, template_file, template_path):
     self.template_file = template_file
     self.template_path = template_path
     self.renderers = RendererFactory()
示例#8
0
 def __init__(self, template_file, template_path):
     self.template_file = template_file
     self.template_path = template_path
     self.renderers = RendererFactory()
示例#9
0
 def setUp(self):
     super(TestTemplate, self).setUp()
     self.rf = RendererFactory()
 def setUp(self):
     super(TestTemplate, self).setUp()
     self.rf = RendererFactory()