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
|
2024-04-10 06:18:51 +00:00
|
|
|
fields = ['email', 'mailout']
|
2010-09-26 21:32:53 +00:00
|
|
|
|
|
|
|
|
2024-04-10 06:18:51 +00:00
|
|
|
@csrf_exempt # Submitted directly by Gmail and similar - no CSRF token.
|
2024-04-09 12:50:06 +00:00
|
|
|
def unsubscribe(request):
|
2024-04-10 06:18:51 +00:00
|
|
|
"""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.
|
|
|
|
"""
|
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-10 06:18:51 +00:00
|
|
|
form = UnsubscribeForm(request.GET)
|
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})
|