bgneal@51: """ bgneal@51: Views for the email_list application. bgneal@51: bgneal@51: """ bgneal@51: import logging bgneal@51: bgneal@51: from django.http import HttpResponseServerError bgneal@51: from django.shortcuts import render, redirect, get_object_or_404 bgneal@51: bgneal@51: from email_list.forms import SubscriberForm bgneal@51: from email_list.models import Subscriber bgneal@51: bgneal@51: bgneal@51: logger = logging.getLogger(__name__) bgneal@51: bgneal@51: bgneal@51: def mailing_list(request): bgneal@51: """ bgneal@51: The main view for handling email list actions (subscribe or unsubscribe). bgneal@51: bgneal@51: """ bgneal@51: if request.method == 'POST': bgneal@51: form = SubscriberForm(request.POST) bgneal@51: if form.is_valid(): bgneal@51: form.process() bgneal@51: bgneal@51: if form.is_subscribe(): bgneal@51: return redirect('email_list-request_subscribe') bgneal@51: else: bgneal@51: return redirect('email_list-request_unsubscribe') bgneal@51: bgneal@51: else: bgneal@51: form = SubscriberForm() bgneal@51: bgneal@51: return render(request, 'email_list/subscribe_form.html', {'form': form}) bgneal@51: bgneal@51: bgneal@51: def confirm(request, key): bgneal@51: """ bgneal@51: This view handles the confirmation of a subscribe or unsubscribe action. bgneal@51: bgneal@51: """ bgneal@51: subscriber = get_object_or_404(Subscriber, key=key) bgneal@51: bgneal@51: if subscriber.is_pending(): bgneal@51: subscriber.set_active() bgneal@51: subscriber.save() bgneal@51: return redirect('email_list-subscribed') bgneal@51: elif subscriber.is_leaving(): bgneal@51: subscriber.delete() bgneal@51: return redirect('email_list-unsubscribed') bgneal@51: bgneal@51: # This should not happen bgneal@51: logger.error("Trying to confirm subscriber %d, but status is %s", bgneal@51: subscriber.pk, subscriber.status) bgneal@51: return HttpResponseServerError()