/usr/share/pyshared/mailer/models.py is in python-django-mailer 0.2a1.dev3-0ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | import base64
import logging
import pickle
from datetime import datetime
from django.core.mail import EmailMessage
from django.db import models
PRIORITIES = (
("1", "high"),
("2", "medium"),
("3", "low"),
("4", "deferred"),
)
class MessageManager(models.Manager):
def high_priority(self):
"""
the high priority messages in the queue
"""
return self.filter(priority="1")
def medium_priority(self):
"""
the medium priority messages in the queue
"""
return self.filter(priority="2")
def low_priority(self):
"""
the low priority messages in the queue
"""
return self.filter(priority="3")
def non_deferred(self):
"""
the messages in the queue not deferred
"""
return self.filter(priority__lt="4")
def deferred(self):
"""
the deferred messages in the queue
"""
return self.filter(priority="4")
def retry_deferred(self, new_priority=2):
count = 0
for message in self.deferred():
if message.retry(new_priority):
count += 1
return count
def email_to_db(email):
# pickle.dumps returns essentially binary data which we need to encode
# to store in a unicode field.
return base64.encodestring(pickle.dumps(email))
def db_to_email(data):
if data == u"":
return None
else:
try:
return pickle.loads(base64.decodestring(data))
except Exception:
try:
# previous method was to just do pickle.dumps(val)
return pickle.loads(data.encode("ascii"))
except Exception:
return None
class Message(models.Model):
# The actual data - a pickled EmailMessage
message_data = models.TextField()
when_added = models.DateTimeField(default=datetime.now)
priority = models.CharField(max_length=1, choices=PRIORITIES, default="2")
# @@@ campaign?
# @@@ content_type?
objects = MessageManager()
def defer(self):
self.priority = "4"
self.save()
def retry(self, new_priority=2):
if self.priority == "4":
self.priority = new_priority
self.save()
return True
else:
return False
def _get_email(self):
return db_to_email(self.message_data)
def _set_email(self, val):
self.message_data = email_to_db(val)
email = property(_get_email, _set_email, doc=
"""EmailMessage object. If this is mutated, you will need to
set the attribute again to cause the underlying serialised data to be updated.""")
@property
def to_addresses(self):
email = self.email
if email is not None:
return email.to
else:
return []
@property
def subject(self):
email = self.email
if email is not None:
return email.subject
else:
return ""
def filter_recipient_list(lst):
if lst is None:
return None
retval = []
for e in lst:
if DontSendEntry.objects.has_address(e):
logging.info("skipping email to %s as on don't send list " % e.encode("utf-8"))
else:
retval.append(e)
return retval
def make_message(subject="", body="", from_email=None, to=None, bcc=None,
attachments=None, headers=None, priority=None):
"""
Creates a simple message for the email parameters supplied.
The 'to' and 'bcc' lists are filtered using DontSendEntry.
If needed, the 'email' attribute can be set to any instance of EmailMessage
if e-mails with attachments etc. need to be supported.
Call 'save()' on the result when it is ready to be sent, and not before.
"""
to = filter_recipient_list(to)
bcc = filter_recipient_list(bcc)
core_msg = EmailMessage(subject=subject, body=body, from_email=from_email,
to=to, bcc=bcc, attachments=attachments, headers=headers)
db_msg = Message(priority=priority)
db_msg.email = core_msg
return db_msg
class DontSendEntryManager(models.Manager):
def has_address(self, address):
"""
is the given address on the don't send list?
"""
queryset = self.filter(to_address__iexact=address)
try:
# Django 1.2
return queryset.exists()
except AttributeError:
# AttributeError: 'QuerySet' object has no attribute 'exists'
return bool(queryset.count())
class DontSendEntry(models.Model):
to_address = models.EmailField()
when_added = models.DateTimeField()
# @@@ who added?
# @@@ comment field?
objects = DontSendEntryManager()
class Meta:
verbose_name = "don't send entry"
verbose_name_plural = "don't send entries"
RESULT_CODES = (
("1", "success"),
("2", "don't send"),
("3", "failure"),
# @@@ other types of failure?
)
class MessageLogManager(models.Manager):
def log(self, message, result_code, log_message=""):
"""
create a log entry for an attempt to send the given message and
record the given result and (optionally) a log message
"""
return self.create(
message_data = message.message_data,
when_added = message.when_added,
priority = message.priority,
# @@@ other fields from Message
result = result_code,
log_message = log_message,
)
class MessageLog(models.Model):
# fields from Message
message_data = models.TextField()
when_added = models.DateTimeField()
priority = models.CharField(max_length=1, choices=PRIORITIES)
# @@@ campaign?
# additional logging fields
when_attempted = models.DateTimeField(default=datetime.now)
result = models.CharField(max_length=1, choices=RESULT_CODES)
log_message = models.TextField()
objects = MessageLogManager()
@property
def email(self):
return db_to_email(self.message_data)
@property
def to_addresses(self):
email = self.email
if email is not None:
return email.to
else:
return []
@property
def subject(self):
email = self.email
if email is not None:
return email.subject
else:
return ""
|