Browse Source

correction problème ORM

Élie Bouttier 7 years ago
parent
commit
27cdc59b58
2 changed files with 7 additions and 7 deletions
  1. 2 2
      services/admin.py
  2. 5 5
      services/models.py

+ 2 - 2
services/admin.py

@@ -59,9 +59,9 @@ class ActiveServiceFilter(admin.SimpleListFilter):
 
     def queryset(self, request, queryset):
         if self.value() == '0': # inactif
-            return queryset.filter(active=False)
+            return queryset.filter(has_active_allocation=False)
         if self.value() == '1': # actif
-            return queryset.filter(active=True)
+            return queryset.filter(has_active_allocation=True)
 
 
 class RouteFilter(admin.SimpleListFilter):

+ 5 - 5
services/models.py

@@ -44,10 +44,10 @@ class IPResourceManager(models.Manager):
         # On rajoute une super annotation « in_use » pour savoir si l’IP est dispo ou non :-)
         qs = qs.annotate(
                     in_use_by_service=models.Exists(
-                        ServiceAllocation.objects.filter(resource=models.OuterRef('pk'), active=True)
+                        ServiceAllocation.objects.filter(Q(resource=models.OuterRef('pk')) & get_active_filter())
                     ),
                     in_use_by_antenna=models.Exists(
-                        ServiceAllocation.objects.filter(resource=models.OuterRef('pk'), active=True)
+                        ServiceAllocation.objects.filter(Q(resource=models.OuterRef('pk')) & get_active_filter())
                     )
         )
         qs = qs.annotate(
@@ -77,13 +77,13 @@ class ActiveServiceManager(models.Manager):
     def get_queryset(self):
         qs = super().get_queryset()
         qs = qs.annotate(
-                    active=models.Case(
+                    has_active_allocation=models.Case(
                         models.When(get_active_filter('allocation'), then=True),
                         default=False,
                         output_field=models.BooleanField()
                     )
         )
-        qs = qs.order_by('pk', '-active').distinct() # complicated things here, do not touch if you're not sure
+        qs = qs.order_by('pk', '-has_active_allocation').distinct() # complicated things here, do not touch if you're not sure
         return qs
 
 
@@ -154,7 +154,7 @@ class Service(models.Model):
             raise ValidationError("Un service du même type existe déjà avec ce label.")
 
     def is_active(self):
-        return self.active
+        return self.has_active_allocation
     is_active.boolean = True
     is_active.short_description = 'Actif'