Browse Source

Save date of last call for membership fee email sent for each member.
And use it to not send new email if last was to recent (3 weeks)

Fabs 10 years ago
parent
commit
3ee0a348f3

+ 2 - 1
coin/members/admin.py

@@ -59,7 +59,8 @@ class MemberAdmin(UserAdmin):
         ('Authentification', {'fields': (
             ('username', 'password'))}),
         ('Permissions', {'fields': (
-            ('is_active', 'is_staff', 'is_superuser'))})
+            ('is_active', 'is_staff', 'is_superuser'))}),
+        (None, {'fields':('date_last_call_for_membership_fees_email',)})
     )
 
     add_fieldsets = (

+ 20 - 0
coin/members/migrations/0012_member_date_last_call_for_membership_fees_email.py

@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('members', '0011_member_comments'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='member',
+            name='date_last_call_for_membership_fees_email',
+            field=models.DateTimeField(null=True, verbose_name='Date du dernier email de relance de cotisation envoy\xe9', blank=True),
+            preserve_default=True,
+        ),
+    ]

+ 27 - 9
coin/members/models.py

@@ -4,6 +4,7 @@ from __future__ import unicode_literals
 import ldapdb.models
 import unicodedata
 import datetime
+
 from django.db import models
 from django.db.models import Q
 from django.db.models.signals import pre_save
@@ -12,6 +13,7 @@ from django.contrib.auth.models import AbstractUser
 from django.conf import settings
 from django.core.validators import RegexValidator
 from django.core.exceptions import ValidationError
+from django.utils import timezone
 from ldapdb.models.fields import CharField, IntegerField, ListField
 
 from coin.offers.models import OfferSubscription
@@ -70,6 +72,9 @@ class Member(CoinLdapSyncMixin, AbstractUser):
                                 help_text="Commentaires libres (informations"
                                 " spécifiques concernant l'adhésion,"
                                 " raison du départ, etc)")
+    date_last_call_for_membership_fees_email = models.DateTimeField(null=True,
+                        blank=True,
+                        verbose_name="Date du dernier email de relance de cotisation envoyé")
 
     # Following fields are managed by the parent class AbstractUser :
     # username, first_name, last_name, email
@@ -249,20 +254,33 @@ class Member(CoinLdapSyncMixin, AbstractUser):
         from coin.isp_database.models import ISPInfo
 
         utils.send_templated_email(to=self.email,
-                                   subject_template='members/emails/welcome_email_subject.txt',
-                                   body_template='members/emails/welcome_email.html',
-                                   context={'member': self, 'branding':ISPInfo.objects.first()})
+                   subject_template='members/emails/welcome_email_subject.txt',
+                   body_template='members/emails/welcome_email.html',
+                   context={'member': self, 'branding':ISPInfo.objects.first()})
 
     def send_call_for_membership_fees_email(self):
         """ Envoi le courriel d'appel à cotisation du membre """
+        from dateutil.relativedelta import relativedelta
         from coin.isp_database.models import ISPInfo
 
-        utils.send_templated_email(to=self.email,
-                                   subject_template='members/emails/call_for_membership_fees_subject.txt',
-                                   body_template='members/emails/call_for_membership_fees.html',
-                                   context={'member': self, 'branding':ISPInfo.objects.first(),
-                                            'membership_info_url': settings.MEMBER_MEMBERSHIP_INFO_URL,
-                                            'today': datetime.date.today})       
+        # Si le dernier courriel de relance a été envoyé il y a moins de trois
+        # semaines, n'envoi pas un nouveau courriel
+        if (not self.date_last_call_for_membership_fees_email
+            or (self.date_last_call_for_membership_fees_email
+               <= timezone.now() + relativedelta(weeks=-3))):
+            utils.send_templated_email(to=self.email,
+               subject_template='members/emails/call_for_membership_fees_subject.txt',
+               body_template='members/emails/call_for_membership_fees.html',
+               context={'member': self, 'branding':ISPInfo.objects.first(),
+                        'membership_info_url': settings.MEMBER_MEMBERSHIP_INFO_URL,
+                        'today': datetime.date.today})
+            # Sauvegarde en base la date du dernier envoi de mail de relance
+            self.date_last_call_for_membership_fees_email = timezone.now()
+            self.save()
+            return True
+
+        return False
+
 
     class Meta:
         verbose_name = 'membre'