Files @ 956f8c6fdaad
Branch filter:

Location: website/conservancy/contacts/views.py

bsturmfels
podjango: Add "Podcast" model to support multiple podcasts

Each Cast (episode) can belong to one or more Podcast, allowing episodes to be
shared between podcasts. This enables us introductory episodes to be delivered
in their own feed, but also included in the main "The Corresponding Source"
feed.

This required adding an additional `podcast_slug` argument to most views. The
date archive views were dropped because they're not linked to from anywhere.

Added a `podcasts` view as an index of all available Podcasts.
import logging

from django.forms import ModelForm
from django.views.decorators.csrf import csrf_exempt
from django.shortcuts import render

from .models import Unsubscription

logger = logging.getLogger(__name__)

class UnsubscribeForm(ModelForm):
    class Meta:
        model = Unsubscription
        fields = ['email', 'mailout']


@csrf_exempt  # Submitted directly by Gmail and similar - no CSRF token.
def unsubscribe(request):
    """Endpoint for use with Gmail one-click unsubscribe or similar.

    Gmail now requires "List-Unsubscribe" headers for senders over a certain
    monthly volume (currently 5000 emails). Add the following headers to your
    mailout:

        List-Unsubscribe: <https://sfconservancy.org/contacts/unsubscribe/?email=foo@bar.com&mailout=jan2024-news>
        List-Unsubscribe-Post: List-Unsubscribe=One-Click

    Interfaces like Gmail will then provide a user interface to unsubscribe
    which will hit this endpoint.
    """
    if request.method == 'POST':
        logger.debug('Unsubscribe GET: %s', request.GET)
        logger.debug('Unsubscribe POST: %s', request.POST)
        form = UnsubscribeForm(request.GET)
        if form.is_valid():
            form.save()
            logger.info('Unsubscribed %s', form.cleaned_data['email'])
            return render(request, 'contacts/unsubscribe_success.html')
    else:
        form = UnsubscribeForm()
    return render(request, 'contacts/unsubscribe.html', {'form': form})