website/conservancy/contacts/views.py

31 lines
975 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.GET | 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})