Browse Source

suppresion services & adhésions

Élie Bouttier 7 years ago
parent
commit
3004827412
2 changed files with 17 additions and 10 deletions
  1. 6 9
      adhesions/admin.py
  2. 11 1
      services/admin.py

+ 6 - 9
adhesions/admin.py

@@ -245,19 +245,16 @@ class AdhesionAdmin(AdtSearchMixin, admin.ModelAdmin):
     def has_delete_permission(self, request, obj=None):
         if not obj:
             return False
-        membership = obj.membership.current
         one_year_ago = timezone.now() - timedelta(days=365)
-        # si l’adhérent a un cotisation qui n’est pas arrêté depuis plus d’un an
-        if membership and (membership.payment_method != PaymentUpdate.STOP or membership.start > one_year_ago):
+        membership = obj.membership.updates.filter(validated=True).first()
+        # si l’adhésion n’a pas été résilié il y a plus d’un an
+        if not membership or membership.payment_method != PaymentUpdate.STOP or membership.start > one_year_ago:
             return False
         # si l’adhérent est référent pour une antenne
-        if obj.antenna_set.all():
+        if obj.antenna_set.exists():
             return False
-        # si l’adherent a un service qui a une IP alloué depuis moins d’un an
-        if any(map(lambda s: any(map(lambda a: a.end is None or a.end > one_year_ago, s.allocations.all())), obj.services.all())):
-            return False
-        # si l’adhérent a un service qui a une contribution qui n’est pas arrêté depuis plus d’un an
-        if any(map(lambda s: s.contribution.current and (s.contribution.current.payment_method != PaymentUpdate.STOP or s.contribution.current.start > one_year_ago), obj.services.all())):
+        # si l’adherent a un service
+        if obj.services.exists():
             return False
         return True
 

+ 11 - 1
services/admin.py

@@ -255,7 +255,17 @@ class ServiceAdmin(admin.ModelAdmin):
         return actions
 
     def has_delete_permission(self, request, obj=None):
-        return False
+        if not obj:
+            return False
+        one_year_ago = timezone.now() - timedelta(days=365)
+        contribution = obj.contribution.updates.filter(validated=True).first()
+        # s’il y avait un paiement actif il y a moins d’un an
+        if not contribution or contribution.payment_method != PaymentUpdate.STOP or contribution.start > one_year_ago:
+            return False
+        # s’il y avait une allocation active il y a moins d’un an
+        if any(map(lambda a: a.end is None or a.end > one_year_ago, obj.allocations.all())):
+            return False
+        return True
 
 
 class IPPrefixAdmin(admin.ModelAdmin):