Skip to content

Loading…

Admin Message edit form #30

Closed
wants to merge 7 commits into from

1 participant

@treyhunner

I created a custom admin form for the Message model so that e-mails can actually be edited. It works for plain text e-mails and HTML e-mails.

The only things I added besides the form itself are:

  • to_addresses property in Message and MessageLog returns a comma-delimited list instead of a list of e-mails
  • added from_address, body, and body_html properties to Message
  • make_message can accept a Message instance to modify through the optional keyword argument db_msg
@treyhunner treyhunner closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Showing with 85 additions and 6 deletions.
  1. +3 −1 mailer/admin.py
  2. +1 −1 mailer/engine.py
  3. +50 −0 mailer/forms.py
  4. +31 −4 mailer/models.py
View
4 mailer/admin.py
@@ -1,10 +1,12 @@
from django.contrib import admin
from mailer.models import Message, DontSendEntry, MessageLog
+from mailer.forms import MessageForm
class MessageAdmin(admin.ModelAdmin):
list_display = ["id", "to_addresses", "subject", "when_added", "priority"]
+ form = MessageForm
class DontSendEntryAdmin(admin.ModelAdmin):
@@ -17,4 +19,4 @@ class MessageLogAdmin(admin.ModelAdmin):
admin.site.register(Message, MessageAdmin)
admin.site.register(DontSendEntry, DontSendEntryAdmin)
-admin.site.register(MessageLog, MessageLogAdmin)
+admin.site.register(MessageLog, MessageLogAdmin)
View
2 mailer/engine.py
@@ -77,7 +77,7 @@ def send_all():
try:
if connection is None:
connection = get_connection(backend=EMAIL_BACKEND)
- logging.info("sending message '%s' to %s" % (message.subject.encode("utf-8"), u", ".join(message.to_addresses).encode("utf-8")))
+ logging.info("sending message '%s' to %s" % (message.subject.encode("utf-8"), message.to_addresses.encode("utf-8")))
email = message.email
email.connection = connection
email.send()
View
50 mailer/forms.py
@@ -0,0 +1,50 @@
+import re
+
+from django import forms
+from django.core.mail import EmailMultiAlternatives
+from django.contrib.admin.widgets import AdminTextInputWidget, AdminTextareaWidget
+
+from mailer.models import Message, make_message
+
+
+class MessageForm(forms.ModelForm):
+ from_email = forms.CharField(widget=AdminTextInputWidget())
+ to = forms.CharField(widget=AdminTextInputWidget())
+ subject = forms.CharField(widget=AdminTextInputWidget())
+ body = forms.CharField(widget=AdminTextareaWidget)
+ body_html = forms.CharField(required=False, widget=AdminTextareaWidget)
+
+ def __init__(self, *args, **kwargs):
+ instance = kwargs.get('instance')
+ if instance:
+ if not kwargs.get('initial'):
+ kwargs['initial'] = {}
+ kwargs['initial']['from_email'] = instance.from_address
+ kwargs['initial']['to'] = instance.to_addresses
+ kwargs['initial']['subject'] = instance.subject
+ kwargs['initial']['body'] = instance.body
+ kwargs['initial']['body_html'] = instance.body_html
+ return super(MessageForm, self).__init__(*args, **kwargs)
+
+ def save(self, commit=True):
+ instance = super(MessageForm, self).save(commit=False)
+ instance = make_message(db_msg=instance,
+ from_email=self.cleaned_data['from_email'],
+ to=re.split(", *", self.cleaned_data['to']),
+ subject=self.cleaned_data['subject'],
+ body=self.cleaned_data['body'])
+
+ body_html = self.cleaned_data['body_html']
+ if body_html:
+ email = instance.email
+ email = EmailMultiAlternatives(email.subject, email.body, email.from_email, email.to)
+ email.attach_alternative(body_html, "text/html")
+ instance.email = email
+
+ if commit:
+ instance.save()
+ return instance
+
+ class Meta:
+ model = Message
+ fields = ('from_email', 'to', 'subject', 'body', 'body_html', 'when_added', 'priority')
View
35 mailer/models.py
@@ -115,10 +115,18 @@ def _set_email(self, val):
set the attribute again to cause the underlying serialised data to be updated.""")
@property
+ def from_address(self):
+ email = self.email
+ if email is not None:
+ return email.from_email
+ else:
+ return []
+
+ @property
def to_addresses(self):
email = self.email
if email is not None:
- return email.to
+ return u", ".join(email.to)
else:
return []
@@ -129,6 +137,24 @@ def subject(self):
return email.subject
else:
return ""
+
+ @property
+ def body(self):
+ email = self.email
+ if email is not None:
+ return email.body
+ else:
+ return ""
+
+ @property
+ def body_html(self):
+ email = self.email
+ try:
+ for alternative in email.alternatives:
+ if alternative[1] == 'text/html':
+ return alternative[0]
+ except AttributeError:
+ return ""
def filter_recipient_list(lst):
@@ -144,7 +170,7 @@ def filter_recipient_list(lst):
def make_message(subject="", body="", from_email=None, to=None, bcc=None,
- attachments=None, headers=None, priority=None):
+ attachments=None, headers=None, priority=None, db_msg=None):
"""
Creates a simple message for the email parameters supplied.
The 'to' and 'bcc' lists are filtered using DontSendEntry.
@@ -159,7 +185,8 @@ def make_message(subject="", body="", from_email=None, to=None, bcc=None,
core_msg = EmailMessage(subject=subject, body=body, from_email=from_email,
to=to, bcc=bcc, attachments=attachments, headers=headers)
- db_msg = Message(priority=priority)
+ if not db_msg:
+ db_msg = Message(priority=priority)
db_msg.email = core_msg
return db_msg
@@ -243,7 +270,7 @@ def email(self):
def to_addresses(self):
email = self.email
if email is not None:
- return email.to
+ return u", ".join(email.to)
else:
return []
Something went wrong with that request. Please try again.