bgneal@1: ####################################################################### bgneal@1: # bgneal@1: # PyBand Copyright (C) 2008 by Brian Neal bgneal@1: # bgneal@1: ####################################################################### bgneal@1: from django import forms bgneal@1: from django.core.urlresolvers import reverse bgneal@1: from django.core.mail import EmailMessage bgneal@1: from django.template import RequestContext bgneal@1: from django.http import HttpResponseRedirect bgneal@1: from django.shortcuts import render_to_response bgneal@1: from django.contrib.admin.views.decorators import staff_member_required bgneal@1: bgneal@1: from mysite.band.models import SiteConfig bgneal@1: from mysite.band.models import Fan bgneal@1: bgneal@1: ####################################################################### bgneal@1: bgneal@1: unsubscribeText = ''' bgneal@1: bgneal@1: ---- bgneal@1: You are receiving this message because you are subscribed to our mailing list. bgneal@1: If you would like to unsubscribe please visit %s. bgneal@1: ''' bgneal@1: bgneal@1: ####################################################################### bgneal@1: bgneal@1: class EmailForm(forms.Form): bgneal@1: subject = forms.CharField(max_length = 255, required = True, label = 'Subject:', bgneal@1: widget = forms.TextInput(attrs = {'class' : 'vTextField required', 'size' : '60'})) bgneal@1: message = forms.CharField(label = 'Message:', bgneal@1: widget = forms.Textarea(attrs = {'class' : 'vLargeTextField required'})) bgneal@1: bgneal@1: ####################################################################### bgneal@1: bgneal@1: def email_sent(request): bgneal@1: return render_to_response('admin/band/email_sent.html', bgneal@1: {}, bgneal@1: context_instance = RequestContext(request)) bgneal@1: bgneal@1: ####################################################################### bgneal@1: bgneal@1: def email(request): bgneal@1: bgneal@1: config = SiteConfig.objects.get(pk = 1) bgneal@1: bandTag = '[%s] ' % (config.band_name, ) bgneal@1: bgneal@1: if request.method == 'POST': bgneal@1: form = EmailForm(request.POST) bgneal@1: if form.is_valid(): bgneal@1: subject = form.cleaned_data['subject'] bgneal@1: message = form.cleaned_data['message'] bgneal@1: bgneal@1: unsubscribeUrl = config.url bgneal@1: if unsubscribeUrl[-1] != '/': bgneal@1: unsubscribeUrl += '/' bgneal@1: unsubscribeUrl += 'contact' bgneal@1: bgneal@1: footer = unsubscribeText % (unsubscribeUrl, ) bgneal@1: message += footer bgneal@1: bgneal@1: fans = Fan.objects.all() bgneal@1: bcc = [fan.email for fan in fans] bgneal@1: bgneal@1: email = EmailMessage(subject, message, config.contact_email, bgneal@1: [config.contact_email], bcc) bgneal@1: email.send() bgneal@1: return HttpResponseRedirect(reverse(email_sent)) bgneal@1: bgneal@1: else: bgneal@1: form = EmailForm(initial = { 'subject' : bandTag }) bgneal@1: bgneal@1: return render_to_response('admin/band/email.html', bgneal@1: { 'form' : form }, bgneal@1: context_instance = RequestContext(request)) bgneal@1: bgneal@1: email = staff_member_required(email)