view email_list/views.py @ 151:762e46d0bb4a

urlquote photologue filenames when building URLs.
author Brian Neal <bgneal@gmail.com>
date Wed, 30 Jul 2014 20:07:21 -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()