Changeset - 495f841d3938
[Not reviewed]
0 3 1
Ben Sturmfels (bsturmfels) - 20 days ago 2024-04-10 06:18:51
ben@sturm.com.au
contacts: Add field to represent the mailout the unsubscribe came from
4 files changed with 37 insertions and 6 deletions:
0 comments (0 inline, 0 general)
conservancy/contacts/admin.py
Show inline comments
...
 
@@ -5,4 +5,5 @@ from .models import Unsubscription
 

	
 
@admin.register(Unsubscription)
 
class UnsubscriptionAdmin(admin.ModelAdmin):
 
    list_display = ['created', 'email']
 
    list_display = ['created', 'email', 'mailout']
 
    search_fields = ['email', 'mailout']
conservancy/contacts/migrations/0002_unsubscription_mailout.py
Show inline comments
 
new file 100644
 
# Generated by Django 4.2.11 on 2024-04-10 02:02
 

	
 
from django.db import migrations, models
 

	
 

	
 
class Migration(migrations.Migration):
 

	
 
    dependencies = [
 
        ('contacts', '0001_initial'),
 
    ]
 

	
 
    operations = [
 
        migrations.AddField(
 
            model_name='unsubscription',
 
            name='mailout',
 
            field=models.SlugField(default=''),
 
            preserve_default=False,
 
        ),
 
    ]
conservancy/contacts/models.py
Show inline comments
...
 
@@ -4,6 +4,7 @@ from django.db import models
 
class Unsubscription(models.Model):
 
    created = models.DateTimeField(auto_now_add=True, blank=True)
 
    email = models.EmailField()
 
    mailout = models.SlugField()
 

	
 
    class Meta:
 
        ordering = ['created']
conservancy/contacts/views.py
Show inline comments
...
 
@@ -11,17 +11,27 @@ logger = logging.getLogger(__name__)
 
class UnsubscribeForm(ModelForm):
 
    class Meta:
 
        model = Unsubscription
 
        fields = ['email']
 
        fields = ['email', 'mailout']
 

	
 

	
 
# Exempt from CSRF protection so that it can be triggered by Gmail's on-click
 
# unsubscribe.
 
@csrf_exempt
 
@csrf_exempt  # Submitted directly by Gmail and similar - no CSRF token.
 
def unsubscribe(request):
 
    """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.
 
    """
 
    if request.method == 'POST':
 
        logger.debug('Unsubscribe GET: %s', request.GET)
 
        logger.debug('Unsubscribe POST: %s', request.POST)
 
        form = UnsubscribeForm(request.GET | request.POST)
 
        form = UnsubscribeForm(request.GET)
 
        if form.is_valid():
 
            form.save()
 
            logger.info('Unsubscribed %s', form.cleaned_data['email'])
0 comments (0 inline, 0 general)