|
@@ -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)
|