Skip to content

Commit

Permalink
Merge remote branch 'spookylukey/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
brosner committed Nov 1, 2010
2 parents 5f87554 + a2f67c4 commit a0825b0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/usage.txt
Expand Up @@ -79,7 +79,7 @@ your settings file, you first have to set EMAIL_BACKEND::
EMAIL_BACKEND = "mailer.backend.DbBackend"

If you were previously using a non-default EMAIL_BACKEND, you need to configure
the MAILER setting_EMAIL_BACKEND, so that django-mailer knows how to actually send
the MAILER_EMAIL_BACKEND setting, so that django-mailer knows how to actually send
the mail::

MAILER_EMAIL_BACKEND = "your.actual.EmailBackend"
8 changes: 7 additions & 1 deletion mailer/engine.py
Expand Up @@ -7,7 +7,13 @@

from django.conf import settings
from django.core.mail import send_mail as core_send_mail
from django.core.mail import get_connection
try:
# Django 1.2
from django.core.mail import get_connection
except ImportError:
# ImportError: cannot import name get_connection
from django.core.mail import SMTPConnection
get_connection = lambda backend=None, fail_silently=False, **kwds: SMTPConnection(fail_silently=fail_silently)

from mailer.models import Message, DontSendEntry, MessageLog

Expand Down
46 changes: 32 additions & 14 deletions mailer/models.py
@@ -1,3 +1,4 @@
import base64
import logging
import pickle

Expand Down Expand Up @@ -60,6 +61,26 @@ def retry_deferred(self, new_priority=2):
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
Expand All @@ -84,14 +105,11 @@ def retry(self, new_priority=2):
return False

def _get_email(self):
if self.message_data == "":
return None
else:
return pickle.loads(self.message_data.encode("ascii"))
return db_to_email(self.message_data)

def _set_email(self, val):
self.message_data = pickle.dumps(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.""")
Expand Down Expand Up @@ -153,10 +171,13 @@ def has_address(self, address):
is the given address on the don't send list?
"""

if self.filter(to_address__iexact=address).exists():
return True
else:
return False
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):
Expand Down Expand Up @@ -216,10 +237,7 @@ class MessageLog(models.Model):

@property
def email(self):
if self.message_data == "":
return None
else:
return pickle.loads(self.message_data.encode("ascii"))
return db_to_email(self.message_data)

@property
def to_addresses(self):
Expand Down

0 comments on commit a0825b0

Please sign in to comment.