Browse Source

ajout liste des adhérents par route

Élie Bouttier 7 years ago
parent
commit
adc9ae8a35
1 changed files with 27 additions and 14 deletions
  1. 27 14
      services/admin.py

+ 27 - 14
services/admin.py

@@ -263,20 +263,21 @@ class IPResourceAdmin(admin.ModelAdmin):
 
 class RouteAdmin(admin.ModelAdmin):
     list_display = ('name',)
-    fields = ('name', 'get_emails', 'get_sms', 'get_routed_ip',)
+    fields = ('name', 'get_emails', 'get_sms', 'get_routed_ip', 'get_adh',)
 
     def get_readonly_fields(self, request, obj=None):
         if obj:
-            return ('name', 'get_emails', 'get_sms', 'get_routed_ip',)
+            return ('name', 'get_emails', 'get_sms', 'get_routed_ip', 'get_adh',)
         else:
-            return ('get_emails', 'get_sms', 'get_routed_ip',)
+            return ('get_emails', 'get_sms', 'get_routed_ip', 'get_adh',)
 
     def get_contacts(self, route):
         cache_emails_key = 'route-%d-emails' % route.pk
         cache_sms_key = 'route-%d-sms' % route.pk
-        cache_results = cache.get_many([cache_emails_key, cache_sms_key])
-        if len(cache_results) == 2:
-            return (cache_results[cache_emails_key], cache_results[cache_sms_key])
+        cache_adh_key = 'route-%d-adh' % route.pk
+        cache_results = cache.get_many([cache_emails_key, cache_sms_key, cache_adh_key])
+        if len(cache_results) == 3:
+            return (cache_results[cache_emails_key], cache_results[cache_sms_key], cache_results[cache_adh_key])
         allocations = route.allocations \
                                 .order_by('service__adhesion__pk') \
                                 .distinct('service__adhesion__pk') \
@@ -284,36 +285,48 @@ class RouteAdmin(admin.ModelAdmin):
                                             'service__adhesion__user',
                                             'service__adhesion__corporation',
                                 )
-        emails, sms = [], []
+        emails, sms, adh = [], [], []
         for allocation in allocations:
             adhesion = allocation.service.adhesion
             adherent = adhesion.adherent
+            if adhesion.pk not in adh:
+                adh.append(adhesion.pk)
             if adherent.email:
-                emails.append('%s <%s>' % (str(adherent), adherent.email))
+                email = '%s <%s>' % (str(adherent), adherent.email)
+                if email not in emails:
+                    emails.append(email)
             # S’il s’agit d’une raison sociale, on contact aussi les gestionnaires
             if adhesion.corporation:
                 if adhesion.corporation.phone_number:
                     sms.append(adhesion.corporation.phone_number)
                 for member in adhesion.corporation.members.all():
                     if member.email:
-                        emails.append('%s <%s>' % (str(member), member.email))
+                        email = '%s <%s>' % (str(member), member.email)
+                        if email not in emails:
+                            emails.append(email)
             else: # user
                 if adhesion.user.profile.phone_number:
-                    sms.append(adhesion.user.profile.phone_number)
+                    if adhesion.user.profile.phone_number not in sms:
+                        sms.append(adhesion.user.profile.phone_number)
         sms = list(filter(lambda x: x[:2] == '06' or x[:2] == '07' or x[:3] == '+336' or x[:3] == '+337', sms))
-        cache.set_many({cache_emails_key: emails, cache_sms_key: sms}, timeout=3600)
-        return (emails, sms)
+        cache.set_many({cache_emails_key: emails, cache_sms_key: sms, cache_adh_key: adh}, timeout=3600)
+        return (emails, sms, adh)
 
     def get_emails(self, route):
-        emails, _ = self.get_contacts(route)
+        emails, _, _ = self.get_contacts(route)
         return '\n'.join(emails)
     get_emails.short_description = 'Contacts'
 
     def get_sms(self, route):
-        _, sms = self.get_contacts(route)
+        _, sms, _ = self.get_contacts(route)
         return '\n'.join(sms)
     get_sms.short_description = 'SMS'
 
+    def get_adh(self, route):
+        _, _, adh = self.get_contacts(route)
+        return '\n'.join(map(lambda a: 'ADT%d' % a, sorted(adh)))
+    get_adh.short_description = 'Adhérents'
+
     def get_routed_ip(self, route):
         routed_ip = route.allocations.order_by('resource__ip').values_list('resource__ip', flat=True)
         return '\n'.join(routed_ip)