view email_list/views.py @ 179:574cdd0241af

Added what people are saying about Ancient Winds to home.
author Brian Neal <bgneal@gmail.com>
date Tue, 20 Oct 2015 20:38:34 -0500
parents e2868ad47a1e
children
line wrap: on
line source
"""
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()