bgneal@51
|
1 """
|
bgneal@51
|
2 Views for the email_list application.
|
bgneal@51
|
3
|
bgneal@51
|
4 """
|
bgneal@51
|
5 import logging
|
bgneal@51
|
6
|
bgneal@51
|
7 from django.http import HttpResponseServerError
|
bgneal@51
|
8 from django.shortcuts import render, redirect, get_object_or_404
|
bgneal@51
|
9
|
bgneal@51
|
10 from email_list.forms import SubscriberForm
|
bgneal@51
|
11 from email_list.models import Subscriber
|
bgneal@51
|
12
|
bgneal@51
|
13
|
bgneal@51
|
14 logger = logging.getLogger(__name__)
|
bgneal@51
|
15
|
bgneal@51
|
16
|
bgneal@51
|
17 def mailing_list(request):
|
bgneal@51
|
18 """
|
bgneal@51
|
19 The main view for handling email list actions (subscribe or unsubscribe).
|
bgneal@51
|
20
|
bgneal@51
|
21 """
|
bgneal@51
|
22 if request.method == 'POST':
|
bgneal@51
|
23 form = SubscriberForm(request.POST)
|
bgneal@51
|
24 if form.is_valid():
|
bgneal@51
|
25 form.process()
|
bgneal@51
|
26
|
bgneal@51
|
27 if form.is_subscribe():
|
bgneal@51
|
28 return redirect('email_list-request_subscribe')
|
bgneal@51
|
29 else:
|
bgneal@51
|
30 return redirect('email_list-request_unsubscribe')
|
bgneal@51
|
31
|
bgneal@51
|
32 else:
|
bgneal@51
|
33 form = SubscriberForm()
|
bgneal@51
|
34
|
bgneal@51
|
35 return render(request, 'email_list/subscribe_form.html', {'form': form})
|
bgneal@51
|
36
|
bgneal@51
|
37
|
bgneal@51
|
38 def confirm(request, key):
|
bgneal@51
|
39 """
|
bgneal@51
|
40 This view handles the confirmation of a subscribe or unsubscribe action.
|
bgneal@51
|
41
|
bgneal@51
|
42 """
|
bgneal@51
|
43 subscriber = get_object_or_404(Subscriber, key=key)
|
bgneal@51
|
44
|
bgneal@51
|
45 if subscriber.is_pending():
|
bgneal@51
|
46 subscriber.set_active()
|
bgneal@51
|
47 subscriber.save()
|
bgneal@51
|
48 return redirect('email_list-subscribed')
|
bgneal@51
|
49 elif subscriber.is_leaving():
|
bgneal@51
|
50 subscriber.delete()
|
bgneal@51
|
51 return redirect('email_list-unsubscribed')
|
bgneal@51
|
52
|
bgneal@51
|
53 # This should not happen
|
bgneal@51
|
54 logger.error("Trying to confirm subscriber %d, but status is %s",
|
bgneal@51
|
55 subscriber.pk, subscriber.status)
|
bgneal@51
|
56 return HttpResponseServerError()
|