Browse Source

Improve members request for call for membership fees

Fabs 10 years ago
parent
commit
1bf71df33d
1 changed files with 13 additions and 7 deletions
  1. 13 7
      coin/members/management/commands/call_for_membership_fees.py

+ 13 - 7
coin/members/management/commands/call_for_membership_fees.py

@@ -5,10 +5,12 @@ import datetime
 from dateutil.relativedelta import relativedelta
 from django.core.management.base import BaseCommand, CommandError
 from django.conf import settings
+from django.db.models import Max
 
 from coin.utils import respect_language
 from coin.members.models import Member, MembershipFee
 
+
 class Command(BaseCommand):
     args = '[date=2011-07-04]'
     help = """Send a call for membership email to members.
@@ -27,7 +29,8 @@ class Command(BaseCommand):
             raise CommandError(
                 'Please enter a valid date : YYYY-mm-dd (ex: 2011-07-04)')
 
-        # Get membership_fees filtered by end date of membership at specific date relative to today
+        # Get membership_fees filtered by end date of membership at specific
+        # date relative to today
         call_dates = [date + relativedelta(months=-3),
                       date + relativedelta(months=-2),
                       date + relativedelta(months=-1),
@@ -37,12 +40,15 @@ class Command(BaseCommand):
         self.stdout.write(
             'Select membership fees for following end dates : %s' % call_dates)
 
-        fees = MembershipFee.objects.filter(end_date__in=call_dates)
+        members = Member.objects.annotate(end_date_membership=Max(
+            'membership_fees__end_date')).filter(end_date_membership__in=call_dates)
 
+        cpt = 0
         with respect_language(settings.LANGUAGE_CODE):
-            for fee in fees:
-                # Don't send if member is paid up 1 month and 1 day after today.
-                if not fee.member.is_paid_up(date + relativedelta(months=+1, days=+1)):
+            for member in members:
+                if member.send_call_for_membership_fees_email():
                     self.stdout.write(
-                    'Send call for membership fees email to %s' % fee.member)
-                    fee.member.send_call_for_membership_fees_email()
+                        'Call for membership fees email was sent to %s' % member)
+                    cpt=cpt+1
+
+        self.stdout.write('%d call for membership fees emails were sent' % cpt)