1 """
2 These are helper functions that make it easier to work with either
3 Jinja2 or Mako templates. You MUST configure it by setting
4 lamson.view.LOADER to one of the template loaders in your config.boot
5 or config.testing.
6
7 After that these functions should just work.
8 """
9
10 from lamson import mail
11 import email
12 import warnings
13
14 LOADER = None
15
17 """
18 Uses the registered loader to load the template you ask for.
19 It assumes that your loader works like Jinja2 or Mako in that
20 it has a LOADER.get_template() method that returns the template.
21 """
22 assert LOADER, "You haven't set lamson.view.LOADER to a loader yet."
23 return LOADER.get_template(template)
24
25
26 -def render(variables, template):
27 """
28 Takes the variables given and renders the template for you.
29 Assumes the template returned by load() will have a .render()
30 method that takes the variables as a dict.
31
32 Use this if you just want to render a single template and don't
33 want it to be a message. Use render_message if the contents
34 of the template are to be interpreted as a message with headers
35 and a body.
36 """
37 return load(template).render(variables)
38
39
40 -def respond(variables, Body=None, Html=None, **kwd):
41 """
42 Does the grunt work of cooking up a MailResponse that's based
43 on a template. The only difference from the lamson.mail.MailResponse
44 class and this (apart from variables passed to a template) are that
45 instead of giving actual Body or Html parameters with contents,
46 you give the name of a template to render. The kwd variables are
47 the remaining keyword arguments to MailResponse of From/To/Subject.
48
49 For example, to render a template for the body and a .html for the Html
50 attachment, and to indicate the From/To/Subject do this:
51
52 msg = view.respond(locals(), Body='template.txt',
53 Html='template.html',
54 From='test@test.com',
55 To='receiver@test.com',
56 Subject='Test body from "%(dude)s".')
57
58 In this case you're using locals() to gather the variables needed for
59 the 'template.txt' and 'template.html' templates. Each template is
60 setup to be a text/plain or text/html attachment. The From/To/Subject
61 are setup as needed. Finally, the locals() are also available as
62 simple Python keyword templates in the From/To/Subject so you can pass
63 in variables to modify those when needed (as in the %(dude)s in Subject).
64 """
65
66 assert Body or Html, "You need to give either the Body or Html template of the mail."
67
68 for key in kwd:
69 kwd[key] = kwd[key] % variables
70
71 msg = mail.MailResponse(**kwd)
72
73 if Body:
74 msg.Body = render(variables, Body)
75
76 if Html:
77 msg.Html = render(variables, Html)
78
79 return msg
80
81
82 -def attach(msg, variables, template, filename=None, content_type=None,
83 disposition=None):
84 """
85 Useful for rendering an attachment and then attaching it to the message
86 given. All the parameters that are in lamson.mail.MailResponse.attach
87 are there as usual.
88 """
89 data = render(variables, template)
90
91 msg.attach(filename=filename, data=data, content_type=content_type,
92 disposition=disposition)
93