Browse Source

infos route

Élie Bouttier 7 years ago
parent
commit
0d25fbd379
3 changed files with 77 additions and 59 deletions
  1. 24 59
      services/admin.py
  2. 33 0
      services/management/commands/route.py
  3. 20 0
      services/models.py

+ 24 - 59
services/admin.py

@@ -471,79 +471,44 @@ class IPResourceAdmin(admin.ModelAdmin):
 
 class RouteAdmin(admin.ModelAdmin):
     list_display = ('name',)
+    search_fields = ('name',)
 
-    def get_fields(self, request, obj=None):
+    def get_fieldsets(self, request, obj=None):
         if obj:
-            return ('name', 'get_emails', 'get_sms', 'get_routed_ip', 'get_adh',)
+            return (
+                (None, {'fields': ['name']}),
+                ('Adhérent·e·s', {'fields': ['get_adh'], 'classes': ['collapse']}),
+                ('E-mails', {'fields': ['get_email'], 'classes': ['collapse']}),
+                ('SMS', {'fields': ['get_sms'], 'classes': ['collapse']}),
+                ('IP', {'fields': ['get_ip'], 'classes': ['collapse']}),
+            )
         else:
-            return ('name',)
+            return (
+                (None, {'fields': ['name']}),
+            )
 
     def get_readonly_fields(self, request, obj=None):
         if obj:
-            return ('get_emails', 'get_sms', 'get_routed_ip', 'get_adh',)
+            return ('get_email', 'get_sms', 'get_ip', 'get_adh',)
         else:
             return ()
 
-    def get_contacts(self, route):
-        cache_emails_key = 'route-%d-emails' % route.pk
-        cache_sms_key = 'route-%d-sms' % route.pk
-        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') \
-                                .select_related(
-                                            'service__adhesion__user',
-                                            'service__adhesion__corporation',
-                                )
-        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:
-                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:
-                        email = '%s <%s>' % (str(member), member.email)
-                        if email not in emails:
-                            emails.append(email)
-            else: # user
-                if 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, cache_adh_key: adh}, timeout=3600)
-        return (emails, sms, adh)
-
-    def get_emails(self, route):
-        emails, _, _ = self.get_contacts(route)
-        return '\n'.join(emails)
-    get_emails.short_description = 'Contacts'
+    def get_email(self, route):
+        return '\n'.join(route.get_email())
+    get_email.short_description = 'E-mails'
 
     def get_sms(self, route):
-        _, sms, _ = self.get_contacts(route)
-        return '\n'.join(sms)
+        sms_filter = lambda x: x[:2] == '06' or x[:2] == '07' or x[:3] == '+336' or x[:3] == '+337'
+        return '\n'.join(filter(sms_filter, route.get_tel()))
     get_sms.short_description = 'SMS'
 
+    def get_ip(self, route):
+        return '\n'.join(route.get_ip())
+    get_ip.short_description = 'IP'
+
     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)
-    get_routed_ip.short_description = 'IP routées'
+        return '\n'.join(map(lambda adh: '%s %s' % (adh, adh.adherent), route.get_adh()))
+    get_adh.short_description = 'Adhérent·e·s'
 
     def get_actions(self, request):
         actions = super().get_actions(request)

+ 33 - 0
services/management/commands/route.py

@@ -0,0 +1,33 @@
+from django.core.management.base import BaseCommand, CommandError
+
+from services.models import Route
+
+
+class Command(BaseCommand):
+    help = 'Informations sur les routes'
+
+    def add_arguments(self, parser):
+        parser.add_argument('route')
+        group = parser.add_mutually_exclusive_group(required=True)
+        group.add_argument('--ip', action='store_true')
+        group.add_argument('--adh', action='store_true')
+        group.add_argument('--tel', action='store_true')
+        group.add_argument('--email', action='store_true')
+
+    def handle(self, *args, **options):
+        try:
+            route = Route.objects.get(name=options['route'])
+        except Route.DoesNotExist:
+            raise CommandError('La route « %s » n’existe pas.' % options['route'])
+        if options['ip']:
+            for ip in route.get_ip():
+                print(ip)
+        elif options['adh']:
+            for adh in route.get_adh():
+                print("%s (%s)" % (adh, adh.adherent))
+        elif options['tel']:
+            for tel in route.get_tel():
+                print(tel)
+        elif options['email']:
+            for tel in route.get_email():
+                print(tel)

+ 20 - 0
services/models.py

@@ -236,6 +236,26 @@ class Antenna(models.Model):
 class Route(models.Model):
     name = models.CharField(max_length=64, unique=True)
 
+    def get_ip(self):
+        allocations = self.allocations.filter(get_active_filter())
+        return allocations.values_list('resource', flat=True)
+
+    def get_adh(self):
+        allocations = self.allocations.filter(get_active_filter())
+        return Adhesion.objects.filter(pk__in=allocations.values_list('service__adhesion', flat=True))
+
+    def get_tel(self):
+        adhesions = self.get_adh()
+        user_tel = filter(lambda x: x, adhesions.values_list('user__profile__phone_number', flat=True))
+        corp_tel = filter(lambda x: x, adhesions.values_list('corporation__phone_number', flat=True))
+        return set(user_tel) | set(corp_tel)
+
+    def get_email(self):
+        adhesions = self.get_adh()
+        user_email = filter(lambda x: x, adhesions.values_list('user__email', flat=True))
+        corp_email = filter(lambda x: x, adhesions.values_list('corporation__email', flat=True))
+        return set(user_email) | set(corp_email)
+
     def __str__(self):
         return self.name