annotate email_list/forms.py @ 87:978a08c838d8

Updates for moving to new jaguar server.
author Brian Neal <bgneal@gmail.com>
date Sat, 13 Apr 2013 16:04:09 -0500
parents e2868ad47a1e
children 0a8942306b04
rev   line source
bgneal@51 1 """
bgneal@51 2 Forms for the email_list application.
bgneal@51 3
bgneal@51 4 """
bgneal@51 5 from django import forms
bgneal@51 6 from django.conf import settings
bgneal@51 7 from django.core.urlresolvers import reverse
bgneal@53 8 from django.core.mail import send_mail, send_mass_mail
bgneal@51 9 from django.template.loader import render_to_string
bgneal@51 10
bgneal@51 11 from email_list.models import Subscriber
bgneal@51 12
bgneal@51 13
bgneal@51 14 SUBSCRIBE_OPTS = [('sub', 'Subscribe'), ('unsub', 'Unsubscribe')]
bgneal@51 15
bgneal@51 16 ALREADY_SUBSCRIBED = "This email address is already subscribed."
bgneal@51 17 NOT_SUBSCRIBED = "This email address is not on our list."
bgneal@51 18
bgneal@51 19
bgneal@51 20 class SubscriberForm(forms.Form):
bgneal@51 21 name = forms.CharField(max_length=64, required=False)
bgneal@51 22 email = forms.EmailField()
bgneal@51 23 location = forms.CharField(max_length=64, required=False)
bgneal@51 24 option = forms.ChoiceField(choices=SUBSCRIBE_OPTS)
bgneal@51 25
bgneal@51 26 def clean(self):
bgneal@51 27 """
bgneal@51 28 This method ensures the appropriate action can be carried out and raises
bgneal@51 29 a validation error if not.
bgneal@51 30
bgneal@51 31 """
bgneal@51 32 email = self.cleaned_data['email']
bgneal@51 33
bgneal@51 34 if self.cleaned_data['option'] == 'sub':
bgneal@51 35 # is the user already subscribed (active)?
bgneal@51 36 try:
bgneal@51 37 subscriber = Subscriber.objects.get(email=email)
bgneal@51 38 except Subscriber.DoesNotExist:
bgneal@51 39 subscriber = Subscriber(email=email,
bgneal@51 40 name=self.cleaned_data['name'],
bgneal@51 41 location=self.cleaned_data['location'])
bgneal@51 42 else:
bgneal@51 43 if subscriber.is_active():
bgneal@51 44 raise forms.ValidationError(ALREADY_SUBSCRIBED)
bgneal@51 45 else:
bgneal@51 46 # is the user already unsubscribed or not subscribed?
bgneal@51 47 try:
bgneal@51 48 subscriber = Subscriber.objects.get(email=email)
bgneal@51 49 except Subscriber.DoesNotExist:
bgneal@51 50 raise forms.ValidationError(NOT_SUBSCRIBED)
bgneal@51 51
bgneal@51 52 # save the subscriber away for a future process() call
bgneal@51 53 self.subscriber = subscriber
bgneal@51 54
bgneal@51 55 return self.cleaned_data
bgneal@51 56
bgneal@51 57 def is_subscribe(self):
bgneal@51 58 """
bgneal@51 59 This function can be called after an is_valid() call to determine if the
bgneal@51 60 request was for a subscribe or unsubscribe.
bgneal@51 61
bgneal@51 62 """
bgneal@51 63 return self.cleaned_data['option'] == 'sub'
bgneal@51 64
bgneal@51 65 def process(self):
bgneal@51 66 """
bgneal@51 67 Call this function if is_valid() returns True. It carries out the user's
bgneal@51 68 subscription request.
bgneal@51 69
bgneal@51 70 """
bgneal@51 71 if self.is_subscribe():
bgneal@51 72 self.subscriber.set_pending()
bgneal@51 73 else:
bgneal@51 74 self.subscriber.set_leaving()
bgneal@51 75
bgneal@51 76 self.subscriber.save()
bgneal@51 77 send_email(self.subscriber)
bgneal@51 78
bgneal@51 79
bgneal@53 80 class AdminEmailForm(forms.Form):
bgneal@53 81 subject = forms.CharField(max_length=255, required=True, label='Subject:',
bgneal@53 82 widget=forms.TextInput(attrs={'class': 'vTextField required',
bgneal@53 83 'size': '120'}))
bgneal@53 84 message = forms.CharField(label='Message:',
bgneal@53 85 widget=forms.Textarea(attrs={'class': 'vLargeTextField required'}))
bgneal@53 86
bgneal@53 87 def __init__(self, *args, **kwargs):
bgneal@53 88 initial = kwargs.pop('initial', {})
bgneal@53 89 if 'subject' not in initial:
bgneal@53 90 initial['subject'] = '[%s] ' % settings.BAND_CONFIG['BAND_NAME']
bgneal@53 91 kwargs['initial'] = initial
bgneal@53 92
bgneal@53 93 super(AdminEmailForm, self).__init__(*args, **kwargs)
bgneal@53 94
bgneal@53 95 def save(self):
bgneal@53 96 """
bgneal@53 97 Call this function if is_valid() to send the mass email.
bgneal@53 98 Returns the number of mails sent.
bgneal@53 99
bgneal@53 100 """
bgneal@53 101 subject = self.cleaned_data['subject']
bgneal@53 102 message = self.cleaned_data['message']
bgneal@53 103 return send_mail_to_subscribers(subject, message)
bgneal@53 104
bgneal@53 105
bgneal@51 106 def send_email(subscriber):
bgneal@51 107 """
bgneal@51 108 This function sends out the appropriate email for the given subscriber.
bgneal@51 109
bgneal@51 110 """
bgneal@51 111 config = settings.BAND_CONFIG
bgneal@51 112 band = config['BAND_NAME']
bgneal@51 113 from_email = config['BAND_EMAIL']
bgneal@51 114
bgneal@51 115 url = "http://%s%s" % (config['BAND_DOMAIN'],
bgneal@51 116 reverse('email_list-confirm', kwargs={'key': subscriber.key}))
bgneal@51 117
bgneal@51 118 if subscriber.is_pending():
bgneal@51 119 email_template = 'email_list/email_subscribe.txt'
bgneal@51 120 else:
bgneal@51 121 email_template = 'email_list/email_unsubscribe.txt'
bgneal@51 122
bgneal@51 123 msg = render_to_string(email_template, {
bgneal@51 124 'band': band,
bgneal@51 125 'url': url,
bgneal@51 126 'band_domain': config['BAND_DOMAIN'],
bgneal@51 127 })
bgneal@51 128
bgneal@51 129 subject = "[%s] Mailing List Confirmation" % band
bgneal@51 130
bgneal@51 131 send_mail(subject, msg, from_email, [subscriber.email])
bgneal@53 132
bgneal@53 133
bgneal@53 134 def send_mail_to_subscribers(subject, message):
bgneal@53 135 """
bgneal@53 136 Send an email to each mailing list subscriber with the given subject and
bgneal@53 137 message.
bgneal@53 138 Returns the number of messages sent.
bgneal@53 139
bgneal@53 140 """
bgneal@53 141 config = settings.BAND_CONFIG
bgneal@53 142 unsubscribe_url = "http://%s%s" % (config['BAND_DOMAIN'],
bgneal@53 143 reverse('email_list-main'))
bgneal@53 144
bgneal@53 145 msg = render_to_string('email_list/mailing_list.txt', {
bgneal@53 146 'band': config['BAND_NAME'],
bgneal@53 147 'unsubscribe_url': unsubscribe_url,
bgneal@53 148 'message': message,
bgneal@53 149 })
bgneal@53 150
bgneal@53 151 mail_data = [(subject, msg, config['BAND_EMAIL'], [subscriber]) for
bgneal@53 152 subscriber in Subscriber.objects.values_list('email', flat=True)]
bgneal@53 153
bgneal@53 154 send_mass_mail(mail_data)
bgneal@53 155 return len(mail_data)