-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Send mail refactor #14
base: master
Are you sure you want to change the base?
Changes from 6 commits
00916e2
27b63f5
f5e6530
a9aa716
a27b563
2b2d839
3264ea3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from django.core.mail.backends.base import BaseEmailBackend | ||
|
||
from mailer.models import Message | ||
|
||
|
||
class DbBackend(BaseEmailBackend): | ||
|
||
def send_messages(self, email_messages): | ||
num_sent = 0 | ||
for email in email_messages: | ||
msg = Message() | ||
msg.email = email | ||
msg.save() | ||
num_sent += 1 | ||
return num_sent |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
from django.conf import settings | ||
from django.core.mail import EmailMessage, EmailMultiAlternatives | ||
from django.db import models | ||
from django_mailer import constants, managers | ||
from django.utils.encoding import force_unicode | ||
|
||
import datetime | ||
|
||
|
||
|
@@ -18,19 +22,13 @@ | |
|
||
class Message(models.Model): | ||
""" | ||
An email message. | ||
|
||
The ``to_address``, ``from_address`` and ``subject`` fields are merely for | ||
easy of access for these common values. The ``encoded_message`` field | ||
contains the entire encoded email message ready to be sent to an SMTP | ||
connection. | ||
|
||
A model to hold email information. | ||
""" | ||
to_address = models.CharField(max_length=200) | ||
from_address = models.CharField(max_length=200) | ||
subject = models.CharField(max_length=255) | ||
|
||
encoded_message = models.TextField() | ||
message = models.TextField() | ||
html_message = models.TextField(blank=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two things here... Firstly, if we're changing the schema then we should probably use south. |
||
date_created = models.DateTimeField(default=datetime.datetime.now) | ||
|
||
class Meta: | ||
|
@@ -39,6 +37,22 @@ class Meta: | |
def __unicode__(self): | ||
return '%s: %s' % (self.to_address, self.subject) | ||
|
||
def email_message(self, connection=None): | ||
""" | ||
Returns a django ``EmailMessage`` or ``EmailMultiAlternatives`` object | ||
from a ``Message`` instance, depending on whether html_message is empty. | ||
""" | ||
subject = force_unicode(self.subject) | ||
if self.html_message: | ||
msg = EmailMultiAlternatives(subject, self.message, | ||
self.from_address, [self.to_address], | ||
connection=connection) | ||
msg.attach_alternative(self.html_message, "text/html") | ||
return msg | ||
else: | ||
return EmailMessage(subject, self.message, self.from_address, | ||
[self.to_address], connection=connection) | ||
|
||
|
||
class QueuedMessage(models.Model): | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
|
||
from django.core.mail.backends.base import BaseEmailBackend | ||
|
||
from django_mailer.constants import PRIORITIES, PRIORITY_EMAIL_NOW | ||
|
||
|
||
class EmailBackend(BaseEmailBackend): | ||
''' | ||
|
@@ -27,7 +29,20 @@ def send_messages(self, email_messages): | |
from django_mailer import queue_email_message | ||
|
||
num_sent = 0 | ||
|
||
''' | ||
Now that email sending actually calls backend's "send" method, | ||
this had to be tweaked to simply append to outbox when priority | ||
is "now". Passing email to queue_email_message with "now" priority | ||
will call this method again, causing infinite loop. | ||
''' | ||
for email_message in email_messages: | ||
queue_email_message(email_message) | ||
priority = email_message.extra_headers.get('X-Mail-Queue-Priority', | ||
None) | ||
if priority and PRIORITIES[priority] is PRIORITY_EMAIL_NOW: | ||
from django.core import mail | ||
mail.outbox.append(email_message) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This smells bad. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi Chris, That's because it's only used during testing (and this is probably a legacy Win~ On Tue, Aug 23, 2011 at 12:03 PM, SmileyChris <
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi Chris, That's because it's only used during testing (and this is probably a legacy thing). Now that django has a TestEmailBackend, we probably should be using that. It doesn't make sense to keep this around either now since we're no longer limited to SMTP. |
||
else: | ||
queue_email_message(email_message) | ||
num_sent += 1 | ||
return num_sent |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from django_mailer.tests.commands import TestCommands | ||
from django_mailer.tests.engine import LockTest #COULD DROP THIS TEST | ||
from django_mailer.tests.backend import TestBackend | ||
from django_mailer.tests.engine import EngineTest, ErrorHandlingTest, LockTest #COULD DROP THIS TEST | ||
from django_mailer.tests.backend import TestBackend | ||
from django_mailer.tests.models import MailerModelTest |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(the imports are kept locally because
setup.py
imports the package to get the version number)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed :)