website/conservancy/contacts/views.py
Ben Sturmfels 789d0c8c84
contacts: Remove ContactEntry and add Unsubscription
This change removes the unused `ContactEntry` model and the `subscribe` view and
replaces it with an `Unsubscription` model and an `unsubscribe` view. It works
similarly, but is intended to be used with the `list-unsubscribe` and
`list-unsubscribe-post` headers.
2024-04-09 22:53:24 +10:00

31 lines
961 B
Python

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']
# Exempt from CSRF protection so that it can be triggered by Gmail's on-click
# unsubscribe.
@csrf_exempt
def unsubscribe(request):
if request.method == 'POST':
logger.debug('Unsubscribe GET: %s', request.GET)
logger.debug('Unsubscribe POST: %s', request.POST)
form = UnsubscribeForm(request.POST)
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})