Commit 607a420c authored by Adrián Ribao's avatar Adrián Ribao
Browse files

changed the way arguments are passed to send

parent ce5e410b
......@@ -3,7 +3,7 @@
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to {{ app_name }} documentation!
Welcome to Notifintime documentation!
========================================
Contents:
......
......@@ -6,7 +6,7 @@ class NotificationBackendBase(object):
def __init__(self, *args, **kwargs):
pass
def send(self, data, *args, **kwargs):
def send(self, *args, **kwargs):
raise NotImplementedError
def receive(self, *args, **kwargs):
......
......@@ -3,7 +3,12 @@ from notifintime.backends.base import NotificationBackendBase
from django.core.mail import EmailMultiAlternatives
from django.template import Context, loader
import html2text
from django.contrib.sites.models import Site
import json
from lxml import etree
from django.conf import settings
from django.contrib.staticfiles.finders import find
from StringIO import StringIO
#import pynliner
from premailer import Premailer
#from django.template.loader import get_template
......@@ -23,30 +28,70 @@ class EmailBackend(NotificationBackendBase):
self.subject = subject
self.template_name = template_name
def render_template_email(self, data):
def get_tree_from_html(self, html):
parser = etree.HTMLParser()
tree = etree.parse(StringIO(html), parser)
return tree
def compile_css(self, tree):
# Insert css from external files
links = tree.xpath('//link')
for link in links:
href= link.attrib.get('href')
if href:
# Open that file
rel_path = href.replace(settings.STATIC_URL, '')
css_path = find(rel_path)
#print "HREF:", href
#print "REL_PATH:", rel_path
#print "CSS_PATH:", css_path
with open(css_path, 'r') as f:
css = f.read()
css = css.replace('"', '\'')
head = tree.find("head")
style = etree.SubElement(head, "style")
style.text = css
# Remove the link
head.remove(link)
result = etree.tostring(tree.getroot(), pretty_print=True, method="html")
# CSS INLINER
#p = Premailer(result, remove_classes=False, include_star_selectors=True, strip_important=False)
p = Premailer(result)
html = p.transform()
return html
def render_template_email(self, context):
template = loader.get_template(self.template_name)
render = template.render(Context(data))
#output = pynliner.fromString(render)
p = Premailer(render)
output = p.transform()
render = template.render(Context(context))
tree = self.get_tree_from_html(render)
#p = Premailer(render)
#output = p.transform()
output = self.compile_css(tree)
return output
def convert_email_to_text(self, html):
def convert_email_to_text(self, html, ignore_links=False,
inline_links=False):
h = html2text.HTML2Text()
#h.ignore_links = True
#h.inline_links = True
h.ignore_links = ignore_links
h.inline_links = inline_links
text = h.handle(html)
return text
def send(self, data, *args, **kwargs):
def send(self, *args, **kwargs):
"""
Sends an email with data as a context for the template.
"""
recipients = kwargs.get('to', [])
json_data = json.loads(data)[0]
html_content = self.render_template_email(json_data)
#json_data = json.loads(data)
context = {
'site': Site.objects.get_current()
}
context.update(kwargs)
html_content = self.render_template_email(context)
text_content = self.convert_email_to_text(html_content)
subject = self.subject.format(**json_data)
subject = self.subject.format(**kwargs)
msg = EmailMultiAlternatives(
subject=subject,
body=text_content,
......
......@@ -16,7 +16,7 @@ class ZeroMQBackend(NotificationBackendBase):
self.receiver_url = u'tcp://localhost:5556'
self.channels = []
def send(self, data, *args, **kwargs):
def send(self, *args, **kwargs):
"""
Sends the data through the channels if they are defined. It broadcast
the data if the channel list is empty.
......@@ -26,6 +26,11 @@ class ZeroMQBackend(NotificationBackendBase):
pub = context.socket(zmq.PUB)
pub.connect(self.pub_url)
data = kwargs.get('data')
if not data:
raise ValueError('When using ZeroMQ backend a data keyword\
argument must be given.')
#print "Sending", self.channels + [str(data), ]
pub.send_multipart(self.channels + [str(data), ])
#if self.channels:
......
......@@ -56,9 +56,9 @@ class NotificationBase(object):
backend = item(**backend_kwargs)
self.backend_instances[backend.name] = backend
def send(self, data, *args, **kwargs):
def send(self, *args, **kwargs):
for backend_name, backend in self.backend_instances.iteritems():
backend.send(data, *args, **kwargs)
backend.send(*args, **kwargs)
def process_received_message(self, message):
"""
......
......@@ -48,3 +48,11 @@ Required methods:
* One
Parsing data
============
It's possible to parse the data sent with the method `parse_json_data` in
the class.
The method receives the json and should return a dictionary.
......@@ -48,5 +48,6 @@ setup(
#'cssutils',
'premailer',
'cssselect',
'html2text',
],
)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment