Browse Source

contacter les responsables d’une liste d’IP

Élie Bouttier 6 years ago
parent
commit
64266add7f
3 changed files with 76 additions and 0 deletions
  1. 22 0
      adhesions/admin.py
  2. 39 0
      adhesions/templates/adhesions/contact.html
  3. 15 0
      services/admin.py

+ 22 - 0
adhesions/admin.py

@@ -8,6 +8,8 @@ from django.http import HttpResponseRedirect
 from django.utils.html import format_html, mark_safe
 from django.urls import reverse
 from django.utils import timezone
+from django.conf.urls import url
+from django.template.response import TemplateResponse
 
 from .forms import UserCreationForm
 from .models import User, Corporation, Adhesion
@@ -272,6 +274,26 @@ class AdhesionAdmin(AdtSearchMixin, admin.ModelAdmin):
             return False
         return True
 
+    def contact_view(self, request):
+        adhesions = request.GET.get('pk').split(',')
+        adhesions = Adhesion.objects.filter(pk__in=adhesions)
+        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))
+        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))
+        sms_filter = lambda x: x[:2] == '06' or x[:2] == '07' or x[:3] == '+336' or x[:3] == '+337'
+        return TemplateResponse(request, 'adhesions/contact.html', dict(
+            adhesions = adhesions,
+            emails = set(user_email) | set(corp_email),
+            sms = filter(sms_filter, set(user_tel) | set(corp_tel))
+        ))
+
+    def get_urls(self):
+        my_urls = [
+            url(r'^contact/$', self.admin_site.admin_view(self.contact_view, cacheable=True), name='contact-adherents'),
+        ]
+        return my_urls + super().get_urls()
+
 
 admin.site.unregister(AuthUser)
 admin.site.register(User, UserAdmin)

+ 39 - 0
adhesions/templates/adhesions/contact.html

@@ -0,0 +1,39 @@
+{% extends "admin/base_site.html" %}
+{% load i18n admin_urls staticfiles %}
+
+{% block content %}
+
+<table>
+  <tr>
+    <th>Adhérents</th>
+  </tr>
+  {% for adhesion in adhesions %}
+  <tr>
+    <td>{{ adhesion }}</td><td>{{ adhesion.adherent }}</td>
+  </tr>
+  {% endfor %}
+</table>
+
+<table>
+  <tr>
+    <th>E-mails</th>
+  </tr>
+  {% for email in emails %}
+  <tr>
+    <td><a href="mailto:{{ email }}">{{ email }}</a></td>
+  </tr>
+  {% endfor %}
+</table>
+
+<table>
+  <tr>
+    <th>SMS</th>
+  </tr>
+  {% for num in sms %}
+  <tr>
+    <td><a href="tel:{{ num }}">{{ num }}</a></td>
+  </tr>
+  {% endfor %}
+</table>
+
+{% endblock %}

+ 15 - 0
services/admin.py

@@ -14,6 +14,8 @@ from django.contrib.postgres.aggregates import StringAgg
 from django.db import connection
 from django.core.cache import cache
 from django.contrib.humanize.templatetags.humanize import naturaltime
+from django.contrib.contenttypes.models import ContentType
+from django.http import HttpResponseRedirect
 
 from djgeojson.views import GeoJSONLayerView
 from urllib.parse import urlencode
@@ -382,6 +384,7 @@ class IPResourceAdmin(admin.ModelAdmin):
         RouteFilter,
     )
     search_fields = ('=ip', 'notes',)
+    actions = ['contact_ip_owners']
 
     def get_fields(self, request, obj=None):
         return self.get_readonly_fields(request, obj)
@@ -461,6 +464,18 @@ class IPResourceAdmin(admin.ModelAdmin):
         return format_html('<a href="{}">{}</a>', obj.checkmk_url, 'voir')
     checkmk.short_description = 'CheckMK'
 
+    def contact_ip_owners(self, request, queryset):
+        selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
+        services = ServiceAllocation.objects.filter(resource__ip__in=selected) \
+                                            .filter(get_active_filter()) \
+                                            .values_list('service__adhesion', flat=True)
+        antennas = AntennaAllocation.objects.filter(resource__ip__in=selected) \
+                                            .filter(get_active_filter()) \
+                                            .values_list('antenna__contact', flat=True)
+        pk = ",".join(map(str, set(services) | set(antennas)))
+        return HttpResponseRedirect(reverse('admin:contact-adherents') + "?pk=%s" % pk)
+    contact_ip_owners.short_description = 'Contacter les adhérents'
+
     def get_actions(self, request):
         actions = super().get_actions(request)
         if 'delete_selected' in actions: