Changeset - 789d0c8c843a
[Not reviewed]
0 6 4
Ben Sturmfels (bsturmfels) - 21 days ago 2024-04-09 12:53:24
ben@sturm.com.au
contacts: Remove ContactEntry and add Unsubscription

This change removes the unused `ContactEntry` model and the `subscribe` view and
replaces it with an `Unsubscription` model and an `unsubscribe` view. It works
similarly, but is intended to be used with the `list-unsubscribe` and
`list-unsubscribe-post` headers.
10 files changed with 84 insertions and 31 deletions:
0 comments (0 inline, 0 general)
conservancy/contacts/admin.py
Show inline comments
 
from django.contrib import admin
 

	
 
from .models import ContactEntry
 

	
 

	
 
@admin.register(ContactEntry)
 
class ContactEntryAdmin(admin.ModelAdmin):
 
    list_display = ('email', 'subscribe_conservancy')
 
from .models import Unsubscription
 

	
 

	
 
@admin.register(Unsubscription)
 
class UnsubscriptionAdmin(admin.ModelAdmin):
 
    list_display = ['created', 'email']
conservancy/contacts/migrations/0001_initial.py
Show inline comments
 
new file 100644
 
# Generated by Django 4.2.11 on 2024-04-09 08:01
 

	
 
from django.db import migrations, models
 

	
 

	
 
class Migration(migrations.Migration):
 

	
 
    initial = True
 

	
 
    dependencies = []
 

	
 
    operations = [
 
        migrations.CreateModel(
 
            name='Unsubscription',
 
            fields=[
 
                (
 
                    'id',
 
                    models.AutoField(
 
                        auto_created=True,
 
                        primary_key=True,
 
                        serialize=False,
 
                        verbose_name='ID',
 
                    ),
 
                ),
 
                ('created', models.DateTimeField(auto_now_add=True)),
 
                ('email', models.EmailField(max_length=254)),
 
            ],
 
            options={
 
                'ordering': ['created'],
 
            },
 
        ),
 
    ]
conservancy/contacts/migrations/__init__.py
Show inline comments
 
new file 100644
conservancy/contacts/models.py
Show inline comments
 
from django.db import models
 

	
 

	
 
class ContactEntry(models.Model):
 
    """Conservancy contact system
 

	
 
    Hopefully this will be deprecated soon"""
 

	
 
    email = models.EmailField() # should make it unique, but we really cannot
 
    subscribe_conservancy = models.BooleanField(default=False)
 
class Unsubscription(models.Model):
 
    created = models.DateTimeField(auto_now_add=True, blank=True)
 
    email = models.EmailField()
 

	
 
    class Meta:
 
        ordering = ('email',)
 

	
 
        ordering = ['created']
conservancy/contacts/templates/contacts/unsubscribe.html
Show inline comments
 
new file 100644
 
{% extends "base_conservancy.html" %}
 
{% block outercontent %}
 
  <div class="mw8 center ph2 ph3 mb4">
 
    <h1>Unsubscribe</h1>
 
    <form action="." method="post">
 
      {{ form.as_p }}
 
      <p><button type="submit" class="ph3 pv2">Submit</button></p>
 
    </form>
 
  </div>
 
{% endblock %}
conservancy/contacts/templates/contacts/unsubscribe_success.html
Show inline comments
 
new file 100644
 
{% extends "base_conservancy.html" %}
 
{% block outercontent %}
 
  <div class="mw8 center ph2 ph3 mb4">
 
    <h1>Unsubscribe successful</h1>
 
  </div>
 
{% endblock %}
conservancy/contacts/urls.py
Show inline comments
 
from django.urls import path
 

	
 
from .views import subscribe
 
from .views import unsubscribe
 

	
 
urlpatterns = [
 
    path('', subscribe),
 
    path('unsubscribe/', unsubscribe),
 
]
conservancy/contacts/views.py
Show inline comments
 
import logging
 

	
 
from django.forms import ModelForm
 
from django.views.decorators.csrf import csrf_exempt
 
from django.shortcuts import render
 

	
 
from .models import ContactEntry
 

	
 
from .models import Unsubscription
 

	
 
def subscribe(request):
 
    """Mailing list subscription form
 
    """
 
logger = logging.getLogger(__name__)
 

	
 
    class ContactEntryForm(ModelForm):
 
        class Meta:
 
            model = ContactEntry
 
class UnsubscribeForm(ModelForm):
 
    class Meta:
 
        model = Unsubscription
 
        fields = ['email']
 

	
 
    ContactEntryForm.base_fields['subscribe_conservancy'].label = 'Receive Software Freedom Conservancy updates'
 

	
 
# 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':
 
        form = ContactEntryForm(request.POST)
 
        logger.debug('Unsubscribe GET: %s', request.GET)
 
        logger.debug('Unsubscribe POST: %s', request.POST)
 
        form = UnsubscribeForm(request.POST)
 
        if form.is_valid():
 
            form.save()
 
            return render(request, 'contacts/subscribe_success.html', {'form': form.cleaned_data})
 
            logger.info('Unsubscribed %s', form.cleaned_data['email'])
 
            return render(request, 'contacts/unsubscribe_success.html')
 
    else:
 
        form = ContactEntryForm()
 

	
 
    return render(request, 'contacts/subscribe.html', {'form': form})
 
        form = UnsubscribeForm()
 
    return render(request, 'contacts/unsubscribe.html', {'form': form})
conservancy/settings/base.py
Show inline comments
...
 
@@ -59,6 +59,11 @@ LOGGING = {
 
            'handlers': ['console'],
 
            'propagate': False,
 
        },
 
        'conservancy.contacts': {
 
            'handlers': ['console'],
 
            'level': 'DEBUG',
 
            'propagate': False,
 
        }
 
    },
 
    'root': {
 
        'handlers': ['console'],
conservancy/urls.py
Show inline comments
...
 
@@ -30,6 +30,7 @@ urlpatterns = [
 
    path('assignment/', include('conservancy.assignment.urls')),
 
    path('blog/', include('conservancy.blog.urls')),
 
    path('casts/the-corresponding-source/', include('conservancy.podjango.urls')),
 
    path('contacts/', include('conservancy.contacts.urls')),
 
    path('contractpatch/', include('conservancy.contractpatch.urls')),
 
    path('feeds/', feeds.view),
 
    path('feeds/blog/', feeds.BlogFeed()),
0 comments (0 inline, 0 general)