view mysite/band/admin_views.py @ 88:7245c769e31e django1.3

Close this branch. I'm not sure if I merged it correctly to the default branch, because the graphlog doesn't look right. But the changes were made to default somehow. So closing this off to prevent future confusion.
author Brian Neal <bgneal@gmail.com>
date Sat, 13 Apr 2013 18:08:19 -0500
parents 3e22a6fde2d2
children 25e00d1b99bf
line wrap: on
line source
#######################################################################
#
# PyBand Copyright (C) 2008 by Brian Neal
#
#######################################################################
from django import forms
from django.core.urlresolvers import reverse
from django.core.mail import EmailMessage
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.contrib.admin.views.decorators import staff_member_required

from mysite.band.models import SiteConfig
from mysite.band.models import Fan

#######################################################################

unsubscribeText = '''

----
You are receiving this message because you are subscribed to our mailing list.
If you would like to unsubscribe please visit %s.
'''

#######################################################################

class EmailForm(forms.Form):
   subject = forms.CharField(max_length = 255, required = True, label = 'Subject:',
         widget = forms.TextInput(attrs = {'class' : 'vTextField required', 'size' : '60'}))
   message = forms.CharField(label = 'Message:',
         widget = forms.Textarea(attrs = {'class' : 'vLargeTextField required'}))

#######################################################################

def email_sent(request):
   return render_to_response('admin/band/email_sent.html',
         {},
         context_instance = RequestContext(request))

#######################################################################

def email(request):

   config = SiteConfig.objects.get(pk = 1)
   bandTag = '[%s] ' % (config.band_name, )

   if request.method == 'POST':
      form = EmailForm(request.POST)
      if form.is_valid():
         subject = form.cleaned_data['subject']
         message = form.cleaned_data['message']

         unsubscribeUrl = config.url
         if unsubscribeUrl[-1] != '/':
            unsubscribeUrl += '/'
         unsubscribeUrl += 'mail'

         footer = unsubscribeText % (unsubscribeUrl, )
         message += footer
         
         fans = Fan.objects.all()
         bcc = [fan.email for fan in fans]

         email = EmailMessage(subject, message, config.contact_email,
               [config.contact_email], bcc)
         email.send()
         return HttpResponseRedirect(reverse(email_sent))

   else:
      form = EmailForm(initial = { 'subject' : bandTag })

   return render_to_response('admin/band/email.html',
         { 'form' : form },
         context_instance = RequestContext(request))

email = staff_member_required(email)