-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifications.py
54 lines (40 loc) · 1.33 KB
/
notifications.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# coding: utf-8
import os
import re
from email.mime.multipart import MIMEMultipart
from email.mime.message import MIMEMessage
from Cheetah.Template import Template
from devpower.mail import unicode_email_body, mime_message, \
sendmail
class Notification:
"""
This is the base class for all the notifications that wish
to be sent using the Notifier
"""
def __init__(self, title=None, sender=None, recipients=[], body={}):
self.title = title
self.sender = title
self.recipients = recipients
self.body = body
class Notifier:
"""
Base class for notifiers
"""
def send(self, notification):
raise NotImplementedError
class EmailNotifier(Notifier):
"""
This notifier sends Notifications through email
"""
def __init__(self, host="localhost", port=25):
self.host = host
self.port = port
def send(self, notification):
text_body = notification.body["text"]
html_body = None
if "html" in notification.body:
html_body = notification.body["html"]
for recipient in notification.recipients:
msg = mime_message(notification.title, notification.sender, \
recipient, text_body, html_body)
sendmail(msg, self.host, self.port)