Skip to content

Instantly share code, notes, and snippets.

@FernandoEscher
Created October 28, 2010 08:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FernandoEscher/650909 to your computer and use it in GitHub Desktop.
Save FernandoEscher/650909 to your computer and use it in GitHub Desktop.
Notification HTML emails
def send_now(users, label, extra_context=None, on_site=True, context=None):
"""
Creates a new notice.
This is intended to be how other apps create new notices.
notification.send(user, 'friends_invite_sent', {
'spam': 'eggs',
'foo': 'bar',
)
You can pass in on_site=False to prevent the notice emitted from being
displayed on the site.
"""
if extra_context is None:
extra_context = {}
#create the ActivityContext object
if context:
ct = ContentType.objects.get_for_model(context)
m = ct.model
context = ActivityContext.objects.get_or_create(content_type = ct,
object_id=context.pk)[0]
notice_type = NoticeType.objects.get(label=label)
current_site = Site.objects.get_current()
notices_url = u"http://%s%s" % (
unicode(current_site),
reverse("notification_notice_settings"))
current_language = get_language()
formats = (
'short.txt',
'full.txt',
'full.html',
) # TODO make formats configurable
if not LAZY_RENDERING and on_site:
formats += ('notice.html',)
email_messages = []
for user in users:
recipients = []
# get user language for user or context from language store defined in
# NOTIFICATION_LANGUAGE_MODULE setting
try:
language = get_notification_language(user, context)
except LanguageStoreNotAvailable:
language = None
if language is not None:
# activate the user or context's language
activate(language)
# update template context with user specific translations
template_context = Context({
"user": user,
"notice": ugettext(notice_type.display),
"notices_url": notices_url,
"current_site": current_site,
})
template_context.update(extra_context)
# get prerendered format messages
messages = get_formatted_messages(formats, label, template_context)
# Strip newlines from subject
subject = ''.join(render_to_string('notification/email_subject.txt', {
'message': messages['short.txt'],
}, template_context).splitlines())
body = render_to_string('notification/email_body.txt', {
'message': messages['full.txt'],
}, template_context)
#TODO: Check when to use html or txt
body = messages['full.html']
if LAZY_RENDERING and on_site:
#re-create the context to avoid including the other rendered templates
ctx =Context({
"user": user,
"notice": ugettext(notice_type.display),
"notices_url": notices_url,
"current_site": current_site,
})
ctx.update(extra_context)
notice = Notice.objects.create(user=user, message=pickle.dumps(ctx).encode("base64"),
notice_type=notice_type, on_site=on_site, context = context)
else:
notice = Notice.objects.create(user=user, message=messages['notice.html'],
notice_type=notice_type, on_site=on_site, context = context)
if should_send(user, notice_type, "1") and user.email: # Email
recipients.append(user.email)
if label in REPLIABLE.keys() and REPLIABLE[label] in extra_context:
#send_mail(subject, body, extra_context[REPLIABLE[label]], recipients)
email_messages += [mail.EmailMessage(subject, body, extra_context[REPLIABLE[label]], recipients)]
else:
#send_mail(subject, body, settings.DEFAULT_FROM_EMAIL, recipients)
email_messages += [mail.EmailMessage(subject, body, settings.DEFAULT_FROM_EMAIL, recipients)]
#facebook
if should_send(user, notice_type, "2") and PROFILES_ACTIVATED:
#try to guess the facebook stuff:
if not 'name' in extra_context:
extra_context['name'] = current_site.domain
if not 'description' in extra_context:
extra_context['description']= subject
if not 'link' in extra_context:
extra_context['link'] = notices_url
if not 'picture' in extra_context and hasattr(settings, 'NOTIFICATION_SITE_PICTURE'):
extra_context['picture'] = settings.NOTIFICATION_SITE_PICTURE
send_to_facebook(user, extra_context)
# reset environment to original language
activate(current_language)
#TODO: Check when to use html or txt
for em in email_messages:
em.content_subtype = 'html'
#send messages:
try:
connection = mail.get_connection()
connection.send_messages(email_messages)
except:
logging.error("SMTP exc", exc_info=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment