bgneal@1
|
1 #######################################################################
|
bgneal@1
|
2 #
|
bgneal@1
|
3 # PyBand Copyright (C) 2008 by Brian Neal
|
bgneal@1
|
4 #
|
bgneal@1
|
5 #######################################################################
|
bgneal@1
|
6 from django import forms
|
bgneal@1
|
7 from django.core.urlresolvers import reverse
|
bgneal@1
|
8 from django.core.mail import EmailMessage
|
bgneal@1
|
9 from django.template import RequestContext
|
bgneal@1
|
10 from django.http import HttpResponseRedirect
|
bgneal@1
|
11 from django.shortcuts import render_to_response
|
bgneal@1
|
12 from django.contrib.admin.views.decorators import staff_member_required
|
bgneal@1
|
13
|
bgneal@1
|
14 from mysite.band.models import SiteConfig
|
bgneal@1
|
15 from mysite.band.models import Fan
|
bgneal@1
|
16
|
bgneal@1
|
17 #######################################################################
|
bgneal@1
|
18
|
bgneal@1
|
19 unsubscribeText = '''
|
bgneal@1
|
20
|
bgneal@1
|
21 ----
|
bgneal@1
|
22 You are receiving this message because you are subscribed to our mailing list.
|
bgneal@1
|
23 If you would like to unsubscribe please visit %s.
|
bgneal@1
|
24 '''
|
bgneal@1
|
25
|
bgneal@1
|
26 #######################################################################
|
bgneal@1
|
27
|
bgneal@1
|
28 class EmailForm(forms.Form):
|
bgneal@1
|
29 subject = forms.CharField(max_length = 255, required = True, label = 'Subject:',
|
bgneal@1
|
30 widget = forms.TextInput(attrs = {'class' : 'vTextField required', 'size' : '60'}))
|
bgneal@1
|
31 message = forms.CharField(label = 'Message:',
|
bgneal@1
|
32 widget = forms.Textarea(attrs = {'class' : 'vLargeTextField required'}))
|
bgneal@1
|
33
|
bgneal@1
|
34 #######################################################################
|
bgneal@1
|
35
|
bgneal@1
|
36 def email_sent(request):
|
bgneal@1
|
37 return render_to_response('admin/band/email_sent.html',
|
bgneal@1
|
38 {},
|
bgneal@1
|
39 context_instance = RequestContext(request))
|
bgneal@1
|
40
|
bgneal@1
|
41 #######################################################################
|
bgneal@1
|
42
|
bgneal@1
|
43 def email(request):
|
bgneal@1
|
44
|
bgneal@1
|
45 config = SiteConfig.objects.get(pk = 1)
|
bgneal@1
|
46 bandTag = '[%s] ' % (config.band_name, )
|
bgneal@1
|
47
|
bgneal@1
|
48 if request.method == 'POST':
|
bgneal@1
|
49 form = EmailForm(request.POST)
|
bgneal@1
|
50 if form.is_valid():
|
bgneal@1
|
51 subject = form.cleaned_data['subject']
|
bgneal@1
|
52 message = form.cleaned_data['message']
|
bgneal@1
|
53
|
bgneal@1
|
54 unsubscribeUrl = config.url
|
bgneal@1
|
55 if unsubscribeUrl[-1] != '/':
|
bgneal@1
|
56 unsubscribeUrl += '/'
|
bgneal@1
|
57 unsubscribeUrl += 'contact'
|
bgneal@1
|
58
|
bgneal@1
|
59 footer = unsubscribeText % (unsubscribeUrl, )
|
bgneal@1
|
60 message += footer
|
bgneal@1
|
61
|
bgneal@1
|
62 fans = Fan.objects.all()
|
bgneal@1
|
63 bcc = [fan.email for fan in fans]
|
bgneal@1
|
64
|
bgneal@1
|
65 email = EmailMessage(subject, message, config.contact_email,
|
bgneal@1
|
66 [config.contact_email], bcc)
|
bgneal@1
|
67 email.send()
|
bgneal@1
|
68 return HttpResponseRedirect(reverse(email_sent))
|
bgneal@1
|
69
|
bgneal@1
|
70 else:
|
bgneal@1
|
71 form = EmailForm(initial = { 'subject' : bandTag })
|
bgneal@1
|
72
|
bgneal@1
|
73 return render_to_response('admin/band/email.html',
|
bgneal@1
|
74 { 'form' : form },
|
bgneal@1
|
75 context_instance = RequestContext(request))
|
bgneal@1
|
76
|
bgneal@1
|
77 email = staff_member_required(email)
|