]> git.openstreetmap.org Git - osqa.git/blob - forum/templatetags/email_tags.py
0699f15bfda7d6df1a89f025a0f112a12dc7ae2c
[osqa.git] / forum / templatetags / email_tags.py
1 from django import template
2 from forum import settings
3 from forum.utils.mail import create_and_send_mail_messages
4 from django.template.defaulttags import url as default_url
5 import logging
6
7 register = template.Library()
8
9 class MultiUserMailMessage(template.Node):
10     def __init__(self, nodelist):
11         self.nodelist = nodelist
12
13     def render(self, context):
14         recipients = context['recipients']
15         messages = list()
16
17         for recipient in recipients:
18             context['embeddedmedia'] = {}
19             context['recipient'] = recipient
20             self.nodelist.render(context)
21             messages.append((recipient, context['subject'], context['htmlcontent'], context['textcontent'], context['embeddedmedia']))
22
23         create_and_send_mail_messages(messages)
24
25 @register.tag
26 def email(parser, token):
27     nodelist = parser.parse(('endemail',))
28     parser.delete_first_token()
29     return MultiUserMailMessage(nodelist)
30
31
32
33 class EmailPartNode(template.Node):
34     def __init__(self, nodelist, varname):
35         self.nodelist = nodelist
36         self.varname = varname
37
38     def render(self, context):
39         context[self.varname] = self.nodelist.render(context).strip()
40
41 @register.tag
42 def subject(parser, token):
43     nodelist = parser.parse(('endsubject',))
44     parser.delete_first_token()
45     return EmailPartNode(nodelist, 'subject')
46
47 def content(parser, token):
48     try:
49         tag_name, base = token.split_contents()
50     except ValueError:
51         try:
52             tag_name = token.split_contents()[0]
53             base = None
54         except:
55             raise template.TemplateSyntaxError, "%r tag requires at least two arguments" % token.contents.split()[0]
56
57     nodelist = parser.parse(('end%s' % tag_name,))
58
59     if base:
60         base = template.loader.get_template(base)
61
62         basenodes = base.nodelist
63         content = [i for i,n in enumerate(basenodes) if isinstance(n, template.loader_tags.BlockNode) and n.name == "content"]
64         if len(content):
65             index = content[0]
66             nodelist = template.NodeList(basenodes[0:index] + nodelist + basenodes[index:])
67         
68
69     parser.delete_first_token()
70     return EmailPartNode(nodelist, tag_name)
71
72
73 register.tag('htmlcontent', content)
74 register.tag('textcontent', content)
75
76
77 class EmbedMediaNode(template.Node):
78     def __init__(self, location, alias):
79         self.location = template.Variable(location)
80         self.alias = alias
81
82     def render(self, context):
83         context['embeddedmedia'][self.alias] = self.location.resolve(context)
84         pass
85
86
87 @register.tag
88 def embedmedia(parser, token):
89     try:
90         tag_name, location, _, alias = token.split_contents()
91     except ValueError:
92         raise template.TemplateSyntaxError, "%r tag requires exactly four arguments" % token.contents.split()[0]
93
94     return EmbedMediaNode(location, alias)
95
96
97 class FullUrlNode(template.Node):
98     def __init__(self, default_node):
99         self.default_node = default_node
100
101     def render(self, context):
102         domain = settings.APP_BASE_URL
103         path = self.default_node.render(context)
104         return "%s%s" % (domain, path)
105
106 @register.tag(name='fullurl')
107 def fullurl(parser, token):
108     default_node = default_url(parser, token)
109     return FullUrlNode(default_node)
110
111
112
113
114
115     
116     
117
118