view mysite/band/admin_views.py @ 23:6ad81d6a86bf django1.3

Initial commit after tweaking with settings files.
author Brian Neal <bgneal@gmail.com>
date Mon, 28 Mar 2011 03:20:29 +0000
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)