Browse Source

notification des allocations via inlines

Élie Bouttier 8 years ago
parent
commit
c19f770223
2 changed files with 67 additions and 41 deletions
  1. 26 41
      services/admin.py
  2. 41 0
      services/utils.py

+ 26 - 41
services/admin.py

@@ -1,14 +1,15 @@
 from django.contrib import admin
 from django.db import models
-from django.forms import ModelForm
+from django.forms import ModelForm, BaseInlineFormSet
 from django.utils import timezone
 from django.core.urlresolvers import reverse
 from django.utils.html import format_html
 from django.core.mail import mail_managers
 
 from adhesions.models import Adhesion
-from .models import Service, ServiceType, IPResource, Route, ResourceAllocation
 from djadhere.utils import get_active_filter
+from .models import Service, ServiceType, IPResource, Route, ResourceAllocation
+from .utils import notify_allocation
 
 
 ### Filters
@@ -65,17 +66,36 @@ def get_foreignkey_link_func(field):
 
 ### Inlines
 
+class ResourceAllocationInlineFormSet(BaseInlineFormSet):
+    def save_new(self, form, commit=True):
+        obj = super().save_new(form, commit)
+        notify_allocation(self.request, obj)
+        return obj
+
+    def save_existing(self, form, instance, commit=True):
+        old = ResourceAllocation.objects.get(pk=instance.pk)
+        notify_allocation(self.request, instance, old)
+        return super().save_existing(form, instance, commit)
+
+
 class AllocationInline(admin.TabularInline):
     model = ResourceAllocation
+    formset = ResourceAllocationInlineFormSet
     extra = 0
     fields = ('id', 'service', 'resource', 'route', 'start', 'end')
     raw_id_fields = ('service', 'resource',)
     verbose_name_plural = 'Allocations'
     show_change_link = True
 
+    def get_formset(self, request, obj=None, **kwargs):
+        formset = super().get_formset(request, obj, **kwargs)
+        formset.request = request
+        return formset
+
     def get_max_num(self, request, obj=None, **kwargs):
         existing = obj.allocations.count() if obj else 0
         # pour simplifier la validation, on ajoute qu’une allocation à la fois
+        # il faudrait surcharger la méthode clean du formset pour supprimer cette limite
         return existing + 1
 
     def has_delete_permission(self, request, obj=None):
@@ -87,6 +107,7 @@ class AllocationInline(admin.TabularInline):
 def ends_resource(resource, request, queryset):
     now = timezone.now()
     queryset.exclude(start__lte=now, end__isnull=False).update(end=now)
+    # TODO: send mail
 ends_resource.short_description = 'Terminer les allocations sélectionnées'
 
 
@@ -167,7 +188,7 @@ class RouteAdmin(admin.ModelAdmin):
 class ResourceAllocationAdmin(admin.ModelAdmin):
     list_display = ('id', 'resource_link', 'service_link', 'start', 'end',)
     list_filter = (AllocationStatusFilter,)
-    actions = (ends_resource,)
+    #actions = (ends_resource,)
     raw_id_fields = ('resource', 'service',)
     search_fields = ('resource__ip', 'service__id', 'service__service_type__name', 'service__label')
 
@@ -187,48 +208,12 @@ class ResourceAllocationAdmin(admin.ModelAdmin):
         return False
 
     def save_model(self, request, obj, form, change):
-        fields = ['resource', 'service', 'route', 'start', 'end', 'active', 'notes']
-
-        new_alloc = obj
         if change:
-            old_alloc = ResourceAllocation.objects.get(pk=new_alloc.pk)
+            old_alloc = ResourceAllocation.objects.get(pk=obj.pk)
         else:
             old_alloc = None
-
         super().save_model(request, obj, form, change)
-
-        message = 'Bénévole : ' + str(request.user.profile)
-        message += '\n\nAllocation :'
-
-        diff = False
-        for field in fields:
-            new_attr = getattr(new_alloc, field)
-            if new_attr == '' or new_attr is None:
-                new_attr = '-'
-            if old_alloc:
-                old_attr = getattr(old_alloc, field)
-                if old_attr == '' or old_attr is None:
-                    old_attr = '-'
-            if change and old_attr != new_attr:
-                message += '\n-%12s: %s\n+%12s: %s' % (field, old_attr, field, new_attr)
-                diff = True
-            else:
-                message += '\n %12s: %s' % (field, new_attr)
-
-        url = 'https' if request.is_secure() else 'http'
-        url += '://' + request.get_host()
-        url += reverse('admin:services_resourceallocation_change', args=(obj.pk,))
-        message += '\n\nVoir : ' + url
-
-        if old_alloc and diff:
-            sujet = 'Modification d’une allocation'
-        elif not old_alloc:
-            sujet = 'Nouvelle allocation'
-        else:
-            sujet = None
-
-        if sujet:
-            mail_managers(sujet, message)
+        notify_allocation(request, obj, old_alloc)
 
 
 class ServiceTypeAdmin(admin.ModelAdmin):

+ 41 - 0
services/utils.py

@@ -0,0 +1,41 @@
+from django.core.mail import mail_managers
+from django.core.urlresolvers import reverse
+
+from .models import ResourceAllocation
+
+
+def notify_allocation(request, new_alloc, old_alloc=None):
+    fields = ['resource', 'service', 'route', 'start', 'end', 'active', 'notes']
+
+    message = 'Bénévole : ' + str(request.user.profile)
+    message += '\n\nAllocation :'
+
+    diff = False
+    for field in fields:
+        new_attr = getattr(new_alloc, field)
+        if new_attr == '' or new_attr is None:
+            new_attr = '-'
+        if old_alloc:
+            old_attr = getattr(old_alloc, field)
+            if old_attr == '' or old_attr is None:
+                old_attr = '-'
+        if old_alloc and old_attr != new_attr:
+            message += '\n-%12s: %s\n+%12s: %s' % (field, old_attr, field, new_attr)
+            diff = True
+        else:
+            message += '\n %12s: %s' % (field, new_attr)
+
+    url = 'https' if request.is_secure() else 'http'
+    url += '://' + request.get_host()
+    url += reverse('admin:services_resourceallocation_change', args=(new_alloc.pk,))
+    message += '\n\nVoir : ' + url
+
+    if old_alloc and diff:
+        sujet = 'Modification d’une allocation'
+    elif not old_alloc:
+        sujet = 'Nouvelle allocation'
+    else:
+        sujet = None
+
+    if sujet:
+        mail_managers(sujet, message)