Keep some interval between emails.
authorRadek Czajka <rczajka@rczajka.pl>
Wed, 12 Feb 2020 15:10:10 +0000 (16:10 +0100)
committerRadek Czajka <rczajka@rczajka.pl>
Wed, 12 Feb 2020 15:10:10 +0000 (16:10 +0100)
src/messaging/management/commands/messaging_send.py
src/messaging/models.py
src/wolnelektury/settings/custom.py

index b85957f..82743f2 100644 (file)
@@ -9,6 +9,6 @@ class Command(BaseCommand):
         parser.add_argument('--dry-run', action='store_true', help='Dry run')
 
     def handle(self, *args, **options):
         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'])
 
             et.run(verbose=True, dry_run=options['dry_run'])
 
index b7a87b3..c929228 100644 (file)
@@ -43,7 +43,8 @@ class EmailTemplate(models.Model):
     
         contacts = contacts.exclude(emailsent__template=self)
         for contact in contacts:
     
         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:
 
     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)
 
             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])
 
     def get_optout_url(self):
         return reverse('messaging_optout', args=[self.key])
 
index 280f539..51453ba 100644 (file)
@@ -42,3 +42,5 @@ CLUB_APP_HOST = None
 AB_TESTS = {
     'PAYLOGO': 2,
 }
 AB_TESTS = {
     'PAYLOGO': 2,
 }
+
+MESSAGING_MIN_DAYS = 2