From df4e3c8b0d4dfa9ec6d16340465dc4ed0182a297 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Wed, 12 Feb 2020 16:10:10 +0100 Subject: [PATCH] Keep some interval between emails. --- src/messaging/management/commands/messaging_send.py | 2 +- src/messaging/models.py | 8 +++++++- src/wolnelektury/settings/custom.py | 2 ++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/messaging/management/commands/messaging_send.py b/src/messaging/management/commands/messaging_send.py index b85957fdc..82743f253 100644 --- a/src/messaging/management/commands/messaging_send.py +++ b/src/messaging/management/commands/messaging_send.py @@ -9,6 +9,6 @@ class Command(BaseCommand): parser.add_argument('--dry-run', action='store_true', help='Dry run') def handle(self, *args, **options): - for et in EmailTemplate.objects.filter(is_active=True): + for et in EmailTemplate.objects.order_by('min_days_since').filter(is_active=True): et.run(verbose=True, dry_run=options['dry_run']) diff --git a/src/messaging/models.py b/src/messaging/models.py index b7a87b35d..c929228f2 100644 --- a/src/messaging/models.py +++ b/src/messaging/models.py @@ -43,7 +43,8 @@ class EmailTemplate(models.Model): contacts = contacts.exclude(emailsent__template=self) for contact in contacts: - self.send(contact, verbose=verbose, dry_run=dry_run) + if not contact.is_annoyed: + self.send(contact, verbose=verbose, dry_run=dry_run) def get_state(self, time=None, test=False): for s in states: @@ -115,6 +116,11 @@ class Contact(models.Model): self.key = get_random_hash(self.email) super().save(*args, **kwargs) + @property + def is_annoyed(self): + cutoff = now() - timedelta(settings.MESSAGING_MIN_DAYS) + return self.emailsent_set.filter(timestamp__gte=cutoff).exists() + def get_optout_url(self): return reverse('messaging_optout', args=[self.key]) diff --git a/src/wolnelektury/settings/custom.py b/src/wolnelektury/settings/custom.py index 280f53955..51453baca 100644 --- a/src/wolnelektury/settings/custom.py +++ b/src/wolnelektury/settings/custom.py @@ -42,3 +42,5 @@ CLUB_APP_HOST = None AB_TESTS = { 'PAYLOGO': 2, } + +MESSAGING_MIN_DAYS = 2 -- 2.20.1