# HG changeset patch # User Brian Neal # Date 1333223527 18000 # Node ID 9b40a8d300a41b51c5086cd565704b30a49eed18 # Parent 7f9e76e7eb4d0e4c6ec57320de10fc7c6eb9cd07 Add the ability for the admin to send mass email to the mailing list. This is for issue #7. diff -r 7f9e76e7eb4d -r 9b40a8d300a4 madeira/email_list/admin.py --- 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) diff -r 7f9e76e7eb4d -r 9b40a8d300a4 madeira/email_list/forms.py --- 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) diff -r 7f9e76e7eb4d -r 9b40a8d300a4 madeira/templates/email_list/admin_mail.html --- /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 %} + +{% endblock %} + +{% block breadcrumbs %} + +{% endblock %} + +{% block content %} +
+

Send mail to the mailing list

+

Use this form to send an email to all subscribers of the Madeira mailing list.

+
{% csrf_token %} +
+ {% for field in form %} +
+ {% if field.errors %}{{ field.errors }}{% endif %} + {{ field.label_tag }} + {{ field }} +
+ {% endfor %} +
+
+ +
+
+
+{% endblock %} diff -r 7f9e76e7eb4d -r 9b40a8d300a4 madeira/templates/email_list/mailing_list.txt --- /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 }}