website/conservancy/contacts/views.py

32 lines
975 B
Python
Raw Normal View History

import logging
from django.forms import ModelForm
from django.views.decorators.csrf import csrf_exempt
2023-10-19 22:52:39 +00:00
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})