diff email_list/views.py @ 71:e2868ad47a1e

For Django 1.4, using the new manage.py.
author Brian Neal <bgneal@gmail.com>
date Sat, 14 Apr 2012 16:40:29 -0500
parents madeira/email_list/views.py@7f9e76e7eb4d
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/email_list/views.py	Sat Apr 14 16:40:29 2012 -0500
@@ -0,0 +1,56 @@
+"""
+Views for the email_list application.
+
+"""
+import logging
+
+from django.http import HttpResponseServerError
+from django.shortcuts import render, redirect, get_object_or_404
+
+from email_list.forms import SubscriberForm
+from email_list.models import Subscriber
+
+
+logger = logging.getLogger(__name__)
+
+
+def mailing_list(request):
+    """
+    The main view for handling email list actions (subscribe or unsubscribe).
+
+    """
+    if request.method == 'POST':
+        form = SubscriberForm(request.POST)
+        if form.is_valid():
+            form.process()
+
+            if form.is_subscribe():
+                return redirect('email_list-request_subscribe')
+            else:
+                return redirect('email_list-request_unsubscribe')
+
+    else:
+        form = SubscriberForm()
+
+    return render(request, 'email_list/subscribe_form.html', {'form': form})
+
+
+def confirm(request, key):
+    """
+    This view handles the confirmation of a subscribe or unsubscribe action.
+
+    """
+    subscriber = get_object_or_404(Subscriber, key=key)
+
+    if subscriber.is_pending():
+        subscriber.set_active()
+        subscriber.save()
+        return redirect('email_list-subscribed')
+    elif subscriber.is_leaving():
+        subscriber.delete()
+        return redirect('email_list-unsubscribed')
+
+    # This should not happen
+    logger.error("Trying to confirm subscriber %d, but status is %s",
+            subscriber.pk, subscriber.status)
+    return HttpResponseServerError()