From 22a9abd9db8e94df91a134e1f929de83b20f3c51 Mon Sep 17 00:00:00 2001 From: hernani Date: Tue, 1 Jun 2010 12:48:36 +0000 Subject: [PATCH] Update to the email system git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@349 0cfe37f9-358a-4d5e-be75-b63607b5c754 --- forum/settings/basic.py | 2 +- forum/templatetags/email_tags.py | 122 ++++++++++++++++++++++++++++--- forum/templatetags/extra_tags.py | 13 +++- 3 files changed, 122 insertions(+), 15 deletions(-) diff --git a/forum/settings/basic.py b/forum/settings/basic.py index ec4d141..a4efd6f 100644 --- a/forum/settings/basic.py +++ b/forum/settings/basic.py @@ -8,7 +8,7 @@ from django.forms.widgets import Textarea BASIC_SET = SettingSet('basic', _('Basic settings'), _("The basic settings for your application"), 1) -APP_LOGO = Setting('APP_LOGO', '/m/default/media/images/logo.png', BASIC_SET, dict( +APP_LOGO = Setting('APP_LOGO', '/upfiles/logo.png', BASIC_SET, dict( label = _("Application logo"), help_text = _("Your site main logo."), widget=ImageFormWidget)) diff --git a/forum/templatetags/email_tags.py b/forum/templatetags/email_tags.py index 3597112..bb21973 100644 --- a/forum/templatetags/email_tags.py +++ b/forum/templatetags/email_tags.py @@ -1,4 +1,5 @@ from django import template +from forum import settings register = template.Library() @@ -11,11 +12,12 @@ class MultiUserMailMessage(template.Node): messages = list() for recipient in recipients: + context['embeddedmedia'] = {} context['recipient'] = recipient self.nodelist.render(context) - messages.append((recipient, context['subject'], context['html_content'], context['text_content'])) + messages.append((recipient, context['subject'], context['htmlcontent'], context['textcontent'], context['embeddedmedia'])) - print messages + create_mail_messages(messages) @register.tag def email(parser, token): @@ -39,16 +41,116 @@ def subject(parser, token): parser.delete_first_token() return EmailPartNode(nodelist, 'subject') -@register.tag -def htmlcontent(parser, token): - nodelist = parser.parse(('endhtmlcontent',)) +def content(parser, token): + try: + tag_name, base = token.split_contents() + except ValueError: + try: + tag_name = token.split_contents()[0] + base = None + except: + raise template.TemplateSyntaxError, "%r tag requires at least two arguments" % token.contents.split()[0] + + nodelist = parser.parse(('end%s' % tag_name,)) + + if base: + base = template.loader.get_template(base) + + basenodes = base.nodelist + content = [i for i,n in enumerate(basenodes) if isinstance(n, template.loader_tags.BlockNode) and n.name == "content"] + if len(content): + index = content[0] + nodelist = template.NodeList(basenodes[0:index] + nodelist + basenodes[index:]) + + parser.delete_first_token() - return EmailPartNode(nodelist, 'html_content') + return EmailPartNode(nodelist, tag_name) + + +register.tag('htmlcontent', content) +register.tag('textcontent', content) + + +class EmbedMediaNode(template.Node): + def __init__(self, location, alias): + self.location = template.Variable(location) + self.alias = alias + + def render(self, context): + context['embeddedmedia'][self.alias] = self.location.resolve(context) + pass + @register.tag -def textcontent(parser, token): - nodelist = parser.parse(('endtextcontent',)) - parser.delete_first_token() - return EmailPartNode(nodelist, 'text_content') +def embedmedia(parser, token): + try: + tag_name, location, _, alias = token.split_contents() + except ValueError: + raise template.TemplateSyntaxError, "%r tag requires exactly four arguments" % token.contents.split()[0] + + return EmbedMediaNode(location, alias) + +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText +from email.mime.image import MIMEImage + +from django.core.mail import DNS_NAME +from smtplib import SMTP +import email.Charset +import socket + + +def create_mail_messages(messages): + sender = '%s <%s>' % (unicode(settings.APP_SHORT_NAME), unicode(settings.DEFAULT_FROM_EMAIL)) + + connection = SMTP(str(settings.EMAIL_HOST), str(settings.EMAIL_PORT), + local_hostname=DNS_NAME.get_fqdn()) + + try: + if (bool(settings.EMAIL_USE_TLS)): + connection.ehlo() + connection.starttls() + connection.ehlo() + + if settings.EMAIL_HOST_USER and settings.EMAIL_HOST_PASSWORD: + connection.login(str(settings.EMAIL_HOST_USER), str(settings.EMAIL_HOST_PASSWORD)) + + if sender is None: + sender = str(settings.DEFAULT_FROM_EMAIL) + + for recipient, subject, html, text, media in messages: + msgRoot = MIMEMultipart('related') + msgRoot['Subject'] = subject + msgRoot['From'] = sender + msgRoot['To'] = '%s <%s>' % (recipient.username, recipient.email) + msgRoot.preamble = 'This is a multi-part message from %s.' % unicode(settings.APP_SHORT_NAME).encode('utf8') + + msgAlternative = MIMEMultipart('alternative') + msgRoot.attach(msgAlternative) + + msgAlternative.attach(MIMEText(text, _charset='utf-8')) + msgAlternative.attach(MIMEText(html, 'html', _charset='utf-8')) + + for alias, location in media.items(): + fp = open(location, 'rb') + msgImage = MIMEImage(fp.read()) + fp.close() + msgImage.add_header('Content-ID', '<'+alias+'>') + msgRoot.attach(msgImage) + + try: + connection.sendmail(sender, [recipient.email], msgRoot.as_string()) + except Exception, e: + pass + + try: + connection.quit() + except socket.sslerror: + connection.close() + except Exception, e: + print e + + + diff --git a/forum/templatetags/extra_tags.py b/forum/templatetags/extra_tags.py index 9f215b5..9499a3f 100644 --- a/forum/templatetags/extra_tags.py +++ b/forum/templatetags/extra_tags.py @@ -419,10 +419,15 @@ class DeclareNode(template.Node): for line in source.splitlines(): m = self.dec_re.search(line) - if m and m.group(2) == '=': - context[m.group(1).strip()] = m.group(3).strip() - elif m and m.group(2) == ':=': - context[m.group(1).strip()] = template.Variable(m.group(3).strip()).resolve(context) + if m: + clist = list(context) + clist.reverse() + d = {} + d['_'] = _ + d['os'] = os + for c in clist: + d.update(c) + context[m.group(1).strip()] = eval(m.group(3).strip(), d) return '' @register.tag(name='declare') -- 2.39.5