SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit 85bbf66f authored by George Katsikas's avatar George Katsikas :goat:
Browse files

add mailinglist model

fixes #242
parent c489591d
No related branches found
No related tags found
No related merge requests found
...@@ -4,7 +4,7 @@ __license__ = "AGPL v3" ...@@ -4,7 +4,7 @@ __license__ = "AGPL v3"
from django.contrib import admin from django.contrib import admin
from .models import MailchimpList from .models import *
@admin.register(MailchimpList) @admin.register(MailchimpList)
...@@ -16,3 +16,19 @@ class MailchimpListAdmin(admin.ModelAdmin): ...@@ -16,3 +16,19 @@ class MailchimpListAdmin(admin.ModelAdmin):
return False return False
@admin.register(MailingList)
class MailingListAdmin(admin.ModelAdmin):
list_display = ["__str__", "is_opt_in", "eligible_count", "subscribed_count"]
list_filter = ["is_opt_in"]
autocomplete_fields = ["eligible_subscribers", "subscribed"]
readonly_fields = ["_email_list"]
def eligible_count(self, obj):
return obj.eligible_subscribers.count()
def subscribed_count(self, obj):
return obj.subscribed.count()
def _email_list(self, obj):
return ", ".join(obj.email_list)
# Generated by Django 4.2.10 on 2024-04-17 10:05
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
(
"scipost",
"0041_alter_remark_contributor_alter_remark_recommendation_and_more",
),
("mailing_lists", "0002_auto_20171229_1435"),
]
operations = [
migrations.CreateModel(
name="MailingList",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=255)),
("slug", models.SlugField(max_length=255, unique=True)),
("is_opt_in", models.BooleanField(default=False)),
(
"eligible_subscribers",
models.ManyToManyField(
blank=True,
related_name="eligible_mailing_lists",
to="scipost.contributor",
),
),
(
"subscribed",
models.ManyToManyField(
blank=True,
related_name="subscribed_mailing_lists",
to="scipost.contributor",
),
),
],
),
]
...@@ -20,7 +20,7 @@ from .constants import ( ...@@ -20,7 +20,7 @@ from .constants import (
) )
from .managers import MailListManager from .managers import MailListManager
from profiles.models import Profile from profiles.models import Profile, ProfileEmail
from scipost.behaviors import TimeStampedModel from scipost.behaviors import TimeStampedModel
from scipost.constants import NORMAL_CONTRIBUTOR from scipost.constants import NORMAL_CONTRIBUTOR
from scipost.models import Contributor from scipost.models import Contributor
...@@ -160,3 +160,58 @@ class MailchimpSubscription(TimeStampedModel): ...@@ -160,3 +160,58 @@ class MailchimpSubscription(TimeStampedModel):
"active_list", "active_list",
"contributor", "contributor",
) )
class MailingList(models.Model):
name = models.CharField(max_length=255)
slug = models.SlugField(max_length=255, unique=True)
is_opt_in = models.BooleanField(default=False)
eligible_subscribers = models.ManyToManyField(
Contributor,
blank=True,
related_name="eligible_mailing_lists",
)
subscribed = models.ManyToManyField(
Contributor,
blank=True,
related_name="subscribed_mailing_lists",
)
@property
def email_list(self):
"""
Returns a list of email addresses of all eligible subscribers who should receive emails from this list.
This is calculated as the set of eligible subscribers minus the set of unsubscribed subscribers.
"""
return list(
self.subscribed.annotate(
primary_email=models.Subquery(
ProfileEmail.objects.filter(
profile=models.OuterRef("profile"), primary=True
).values("email")[:1]
)
).values_list("primary_email", flat=True)
)
def add_eligible_subscriber(self, contributor):
"""Adds the contributor to the list of eligible subscribers."""
self.eligible_subscribers.add(contributor)
# If the list is not opt-in, automatically subscribe the contributor
if not self.is_opt_in:
self.subscribe(contributor)
def subscribe(self, contributor):
"""Subscribes the contributor to the list."""
if contributor not in self.eligible_subscribers.all():
raise ValueError("Contributor is not eligible to subscribe to this list.")
self.subscribed.add(contributor)
def unsubscribe(self, contributor):
"""Unsubscribes the contributor from the list."""
if contributor not in self.subscribed.all():
raise ValueError("Contributor is not subscribed to this list.")
self.subscribed.remove(contributor)
def __str__(self):
return self.name
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment