Parcourir la source

liste contacts des routes : emails + sms

Élie Bouttier il y a 7 ans
Parent
commit
34ebecf100
2 fichiers modifiés avec 53 ajouts et 8 suppressions
  1. 52 7
      services/admin.py
  2. 1 1
      services/models.py

+ 52 - 7
services/admin.py

@@ -14,6 +14,7 @@ from django.http import HttpResponse
 from django.db.models.functions import Cast
 from django.contrib.postgres.aggregates import StringAgg
 from django.db import connection
+from django.core.cache import cache
 
 from djadhere.utils import get_active_filter
 from adhesions.models import Adhesion
@@ -262,17 +263,61 @@ class IPResourceAdmin(admin.ModelAdmin):
 
 class RouteAdmin(admin.ModelAdmin):
     list_display = ('name',)
-    fields = ('name', 'routed_ip',)
+    fields = ('name', 'get_emails', 'get_sms', 'get_routed_ip',)
 
     def get_readonly_fields(self, request, obj=None):
         if obj:
-            return ('name', 'routed_ip',)
+            return ('name', 'get_emails', 'get_sms', 'get_routed_ip',)
         else:
-            return ('routed_ip',)
-
-    def routed_ip(self, route):
-        return '\n'.join(route.services.order_by('resource__ip').values_list('resource__ip', flat=True))
-    routed_ip.short_description = 'IP routées'
+            return ('get_emails', 'get_sms', 'get_routed_ip',)
+
+    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])
+        allocations = route.allocations \
+                                .order_by('service__adhesion__pk') \
+                                .distinct('service__adhesion__pk') \
+                                .select_related(
+                                            'service__adhesion__user',
+                                            'service__adhesion__corporation',
+                                )
+        emails, sms = [], []
+        for allocation in allocations:
+            adhesion = allocation.service.adhesion
+            adherent = adhesion.adherent
+            if adherent.email:
+                emails.append('%s <%s>' % (str(adherent), adherent.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))
+            else: # user
+                if adhesion.user.profile.phone_number:
+                    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)
+
+    def get_emails(self, 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)
+        return '\n'.join(sms)
+    get_sms.short_description = 'SMS'
+
+    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)
+    get_routed_ip.short_description = 'IP routées'
 
     def get_actions(self, request):
         actions = super().get_actions(request)

+ 1 - 1
services/models.py

@@ -239,7 +239,7 @@ class ServiceAllocation(Allocation):
     resource = models.ForeignKey(IPResource, verbose_name='Ressource', related_name='service_allocations',
                         related_query_name='service_allocation', limit_choices_to={'category': 0})
     service = models.ForeignKey(Service, related_name='allocations', related_query_name='allocation')
-    route = models.ForeignKey(Route, verbose_name='Route', related_name='services', related_query_name='allocation')
+    route = models.ForeignKey(Route, verbose_name='Route', related_name='allocations', related_query_name='allocation')
 
     class Meta:
         verbose_name = 'allocation'