changeset 53:9b40a8d300a4

Add the ability for the admin to send mass email to the mailing list. This is for issue #7.
author Brian Neal <bgneal@gmail.com>
date Sat, 31 Mar 2012 14:52:07 -0500
parents 7f9e76e7eb4d
children d958e59d6fc6
files madeira/email_list/admin.py madeira/email_list/forms.py madeira/templates/email_list/admin_mail.html madeira/templates/email_list/mailing_list.txt
diffstat 4 files changed, 122 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/madeira/email_list/admin.py	Thu Mar 29 20:01:13 2012 -0500
+++ b/madeira/email_list/admin.py	Sat Mar 31 14:52:07 2012 -0500
@@ -3,14 +3,39 @@
 
 """
 from django.contrib import admin
+from django.conf.urls.defaults import patterns, url
+from django.shortcuts import render, redirect
 
 from email_list.models import Subscriber
+from email_list.forms import AdminEmailForm
 
 
 class SubscriberAdmin(admin.ModelAdmin):
-   list_display = ['__unicode__', 'location', 'status']
-   list_filter = ['status']
-   search_fields = ['name', 'email']
+    list_display = ['__unicode__', 'location', 'status']
+    list_filter = ['status']
+    search_fields = ['name', 'email']
+
+    def get_urls(self):
+        urls = super(SubscriberAdmin, self).get_urls()
+        my_urls = patterns('',
+            url(r'^send_mail/$',
+                self.admin_site.admin_view(self.send_mail),
+                name='email_list-admin_mail'),
+        )
+        return my_urls + urls
+
+    def send_mail(self, request):
+        if request.method == 'POST':
+            form = AdminEmailForm(request.POST)
+            if form.is_valid():
+                n = form.save()
+                msg = '%d mailing list emails sent' % n
+                self.message_user(request, msg)
+                return redirect('admin:index')
+        else:
+            form = AdminEmailForm()
+
+        return render(request, 'email_list/admin_mail.html', {'form': form})
 
 
 admin.site.register(Subscriber, SubscriberAdmin)
--- a/madeira/email_list/forms.py	Thu Mar 29 20:01:13 2012 -0500
+++ b/madeira/email_list/forms.py	Sat Mar 31 14:52:07 2012 -0500
@@ -5,7 +5,7 @@
 from django import forms
 from django.conf import settings
 from django.core.urlresolvers import reverse
-from django.core.mail import send_mail
+from django.core.mail import send_mail, send_mass_mail
 from django.template.loader import render_to_string
 
 from email_list.models import Subscriber
@@ -77,6 +77,32 @@
         send_email(self.subscriber)
 
 
+class AdminEmailForm(forms.Form):
+    subject = forms.CharField(max_length=255, required=True, label='Subject:',
+        widget=forms.TextInput(attrs={'class': 'vTextField required',
+            'size': '120'}))
+    message = forms.CharField(label='Message:',
+        widget=forms.Textarea(attrs={'class': 'vLargeTextField required'}))
+
+    def __init__(self, *args, **kwargs):
+        initial = kwargs.pop('initial', {})
+        if 'subject' not in initial:
+            initial['subject'] = '[%s] ' % settings.BAND_CONFIG['BAND_NAME']
+            kwargs['initial'] = initial
+
+        super(AdminEmailForm, self).__init__(*args, **kwargs)
+
+    def save(self):
+        """
+        Call this function if is_valid() to send the mass email.
+        Returns the number of mails sent.
+
+        """
+        subject = self.cleaned_data['subject']
+        message = self.cleaned_data['message']
+        return send_mail_to_subscribers(subject, message)
+
+
 def send_email(subscriber):
     """
     This function sends out the appropriate email for the given subscriber.
@@ -103,3 +129,27 @@
     subject = "[%s] Mailing List Confirmation" % band
 
     send_mail(subject, msg, from_email, [subscriber.email])
+
+
+def send_mail_to_subscribers(subject, message):
+    """
+    Send an email to each mailing list subscriber with the given subject and
+    message.
+    Returns the number of messages sent.
+
+    """
+    config = settings.BAND_CONFIG
+    unsubscribe_url = "http://%s%s" % (config['BAND_DOMAIN'],
+              reverse('email_list-main'))
+
+    msg = render_to_string('email_list/mailing_list.txt', {
+        'band': config['BAND_NAME'],
+        'unsubscribe_url': unsubscribe_url,
+        'message': message,
+        })
+
+    mail_data = [(subject, msg, config['BAND_EMAIL'], [subscriber]) for
+            subscriber in Subscriber.objects.values_list('email', flat=True)]
+
+    send_mass_mail(mail_data)
+    return len(mail_data)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/madeira/templates/email_list/admin_mail.html	Sat Mar 31 14:52:07 2012 -0500
@@ -0,0 +1,37 @@
+{% extends "admin/base_site.html" %}
+{% load url from future %}
+{% load adminmedia %}
+
+{% block title %}Send mail to mailing list{% endblock %}
+
+{% block extrastyle %}
+<link rel="stylesheet" type="text/css" href="{% admin_media_prefix %}css/forms.css" />
+{% endblock %}
+
+{% block breadcrumbs %}
+<div class="breadcrumbs">
+   <a href="../../../">Home</a> &rsaquo; <a href="../../">Email List</a> &rsaquo; 
+      <a href="../">Subscribers</a> &rsaquo; Send mail
+</div>
+{% endblock %}
+
+{% block content %}
+<div id="content-main">
+   <h1>Send mail to the mailing list</h1>
+   <p>Use this form to send an email to all subscribers of the Madeira mailing list.</p>
+   <form method="post" action=".">{% csrf_token %}
+      <fieldset class="module aligned">
+      {% for field in form %}
+      <div class="form-row {% if field.field.required %}required{% endif %} {% if field.errors %}errors{% endif %}">
+         {% if field.errors %}{{ field.errors }}{% endif %}
+         {{ field.label_tag }}
+         {{ field }}
+         </div>
+      {% endfor %}
+      </fieldset>
+      <div class="submit-row">
+         <input type="submit" name="Send" value="Send" class="default" />
+      </div>
+   </form>
+</div>
+{% endblock %}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/madeira/templates/email_list/mailing_list.txt	Sat Mar 31 14:52:07 2012 -0500
@@ -0,0 +1,6 @@
+{{ message }}
+
+----
+You are receiving this message because you are subscribed to the mailing list
+for {{ band }}. To unsubscribe from these mailings please visit:
+{{ unsubscribe_url }}