2024-04-09 12:50:06 +00:00
|
|
|
import logging
|
|
|
|
|
2010-09-26 21:32:53 +00:00
|
|
|
from django.forms import ModelForm
|
2024-04-09 12:50:06 +00:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2023-10-19 22:52:39 +00:00
|
|
|
from django.shortcuts import render
|
2010-09-26 21:32:53 +00:00
|
|
|
|
2024-04-09 12:50:06 +00:00
|
|
|
from .models import Unsubscription
|
2023-10-19 22:44:24 +00:00
|
|
|
|
2024-04-09 12:50:06 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2010-09-26 21:32:53 +00:00
|
|
|
|
2024-04-09 12:50:06 +00:00
|
|
|
class UnsubscribeForm(ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Unsubscription
|
|
|
|
fields = ['email']
|
2010-09-26 21:32:53 +00:00
|
|
|
|
|
|
|
|
2024-04-09 12:50:06 +00:00
|
|
|
# Exempt from CSRF protection so that it can be triggered by Gmail's on-click
|
|
|
|
# unsubscribe.
|
|
|
|
@csrf_exempt
|
|
|
|
def unsubscribe(request):
|
2010-09-26 21:32:53 +00:00
|
|
|
if request.method == 'POST':
|
2024-04-09 12:50:06 +00:00
|
|
|
logger.debug('Unsubscribe GET: %s', request.GET)
|
|
|
|
logger.debug('Unsubscribe POST: %s', request.POST)
|
2024-04-09 13:06:52 +00:00
|
|
|
form = UnsubscribeForm(request.GET | request.POST)
|
2010-09-26 21:32:53 +00:00
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
2024-04-09 12:50:06 +00:00
|
|
|
logger.info('Unsubscribed %s', form.cleaned_data['email'])
|
|
|
|
return render(request, 'contacts/unsubscribe_success.html')
|
2010-09-26 21:32:53 +00:00
|
|
|
else:
|
2024-04-09 12:50:06 +00:00
|
|
|
form = UnsubscribeForm()
|
|
|
|
return render(request, 'contacts/unsubscribe.html', {'form': form})
|