Last active
April 5, 2017 02:21
-
-
Save victorono/8029051 to your computer and use it in GitHub Desktop.
Extender la clase EmailMessage para utilizar las plantillas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from django.template.loader import get_template | |
from django.template.loader_tags import BlockNode | |
from django.template import Context | |
from django.core import mail | |
class EmailMessage(mail.EmailMultiAlternatives): | |
""" | |
Extender la clase EmailMessage para utilizar las plantillas | |
{% block subject %} | |
Hola {{ user.name }} | |
{% endblock %} | |
{% block body %} | |
Mensaje plano. | |
{% endblock %} | |
{% block html %} | |
mensaje <strong>html</strong>. | |
{% endblock %} | |
# from_email = "Me <sender@email.com>" | |
# context = {} | |
# message = EmailMessage('email/basic.html', context, None, None, from_email, to=[email]) | |
# message.attach_file('/home/vic/pgadmin.log') | |
# message.send() | |
""" | |
def __init__(self, template_name, context, *args, **kwargs): | |
self._subject = None | |
self._body = None | |
self._html = None | |
self.template_name = template_name | |
self.context = context | |
super(mail.EmailMultiAlternatives, self).__init__(*args, **kwargs) | |
self.alternatives = [] | |
@property | |
def template_name(self): | |
return self._template_name | |
@template_name.setter | |
def template_name(self, value): | |
self._template_name = value | |
# Load the template. | |
self.template = get_template(self._template_name) | |
@property | |
def template(self): | |
return self._template | |
@template.setter | |
def template(self, value): | |
self._template = value | |
for block in self._template.nodelist: | |
if isinstance(block, BlockNode): | |
if block.name == 'subject': | |
self._subject = block | |
elif block.name == 'body': | |
self._body = block | |
if block.name == 'html': | |
self._html = block | |
def send(self, *args, **kwargs): | |
context = Context(self.context) | |
if self._subject is not None: | |
self.subject = self._subject.render(context).strip('\n\r') | |
if self._body is not None: | |
self.body = self._body.render(context).strip('\n\r') | |
if self._html is not None: | |
html = self._html.render(context).strip('\n\r') | |
if html: | |
if not self.body: | |
self.body = html | |
self.content_subtype = 'html' | |
else: | |
self.attach_alternative(html, 'text/html') | |
return super(mail.EmailMultiAlternatives, self).send(*args, **kwargs) | |
def __getstate__(self): | |
return dict((k, v) for k, v in self.__dict__.iteritems() | |
if not k in ('_body', '_html', '_subject', '_template')) | |
def __setstate__(self, state): | |
self.__dict__ = state | |
self.template_name = self._template_name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment