Parcourir la source

Only display relevant users in items admin

Jocelyn Delande il y a 9 ans
Parent
commit
d30d0bde81
1 fichiers modifiés avec 21 ajouts et 1 suppressions
  1. 21 1
      hardware_provisioning/admin.py

+ 21 - 1
hardware_provisioning/admin.py

@@ -5,18 +5,38 @@ from __future__ import unicode_literals
 from datetime import date
 
 from django.contrib import admin
+from django.contrib.auth import get_user_model
 from .models import ItemType, Item, Loan
 
 
+User = get_user_model()
+
 admin.site.register(ItemType)
 
 
+class OwnerFilter(admin.SimpleListFilter):
+    title = "Propriétaire"
+    parameter_name = 'owner'
+
+    def lookups(self, request, model_admin):
+        owners = [
+            (i.pk, i) for i in User.objects.filter(items__isnull=False)]
+
+        return [(None, "L'association")] + owners
+
+    def queryset(self, request, queryset):
+        if self.value():
+            return queryset.filter(owner__pk=self.value())
+        else:
+            return queryset
+
+
 @admin.register(Item)
 class ItemAdmin(admin.ModelAdmin):
     list_display = (
         'designation', 'type', 'mac_address', 'serial', 'owner',
         'buy_date', 'is_available')
-    list_filter = ('type__name', 'buy_date', 'owner')
+    list_filter = ('type__name', 'buy_date', OwnerFilter)
     search_fields = (
         'designation', 'mac_address', 'serial',
         'owner__email', 'owner__nickname',