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
7 register = template.Library()
9 class MultiUserMailMessage(template.Node):
10 def __init__(self, nodelist):
11 self.nodelist = nodelist
13 def render(self, context):
14 recipients = context['recipients']
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']))
23 create_and_send_mail_messages(messages)
26 def email(parser, token):
27 nodelist = parser.parse(('endemail',))
28 parser.delete_first_token()
29 return MultiUserMailMessage(nodelist)
33 class EmailPartNode(template.Node):
34 def __init__(self, nodelist, varname):
35 self.nodelist = nodelist
36 self.varname = varname
38 def render(self, context):
39 context[self.varname] = self.nodelist.render(context).strip()
42 def subject(parser, token):
43 nodelist = parser.parse(('endsubject',))
44 parser.delete_first_token()
45 return EmailPartNode(nodelist, 'subject')
47 def content(parser, token):
49 tag_name, base = token.split_contents()
52 tag_name = token.split_contents()[0]
55 raise template.TemplateSyntaxError, "%r tag requires at least two arguments" % token.contents.split()[0]
57 nodelist = parser.parse(('end%s' % tag_name,))
60 base = template.loader.get_template(base)
62 basenodes = base.nodelist
63 content = [i for i,n in enumerate(basenodes) if isinstance(n, template.loader_tags.BlockNode) and n.name == "content"]
66 nodelist = template.NodeList(basenodes[0:index] + nodelist + basenodes[index:])
69 parser.delete_first_token()
70 return EmailPartNode(nodelist, tag_name)
73 register.tag('htmlcontent', content)
74 register.tag('textcontent', content)
77 class EmbedMediaNode(template.Node):
78 def __init__(self, location, alias):
79 self.location = template.Variable(location)
82 def render(self, context):
83 context['embeddedmedia'][self.alias] = self.location.resolve(context)
88 def embedmedia(parser, token):
90 tag_name, location, _, alias = token.split_contents()
92 raise template.TemplateSyntaxError, "%r tag requires exactly four arguments" % token.contents.split()[0]
94 return EmbedMediaNode(location, alias)
97 class FullUrlNode(template.Node):
98 def __init__(self, default_node):
99 self.default_node = default_node
101 def render(self, context):
102 domain = settings.APP_BASE_URL
103 path = self.default_node.render(context)
104 return "%s%s" % (domain, path)
106 @register.tag(name='fullurl')
107 def fullurl(parser, token):
108 default_node = default_url(parser, token)
109 return FullUrlNode(default_node)