|
@@ -0,0 +1,71 @@
|
|
|
+from django.contrib import admin
|
|
|
+
|
|
|
+from .models import Equipment, Category, Location
|
|
|
+
|
|
|
+
|
|
|
+class CategoryFilter(admin.SimpleListFilter):
|
|
|
+ title = 'Catégorie'
|
|
|
+ parameter_name = 'category'
|
|
|
+
|
|
|
+ def lookups(self, request, model_admin):
|
|
|
+ return Category.objects.values_list('pk', 'name')
|
|
|
+
|
|
|
+ def queryset(self, request, queryset):
|
|
|
+ try:
|
|
|
+ category = Category.objects.get(pk=int(self.value()))
|
|
|
+ except (TypeError, ValueError, Category.DoesNotExist):
|
|
|
+ pass
|
|
|
+ else:
|
|
|
+ categories = category.get_descendants(include_self=True)
|
|
|
+ queryset = queryset.filter(category__in=categories)
|
|
|
+ return queryset
|
|
|
+
|
|
|
+
|
|
|
+class CategoryAdmin(admin.ModelAdmin):
|
|
|
+ list_display = ('__str__', 'parent',)
|
|
|
+
|
|
|
+ def get_actions(self, request):
|
|
|
+ actions = super().get_actions(request)
|
|
|
+ if 'delete_selected' in actions:
|
|
|
+ del actions['delete_selected']
|
|
|
+ return actions
|
|
|
+
|
|
|
+ def has_delete_permission(self, request, obj=None):
|
|
|
+ # Interdiction de supprimer la catégorie si elle contient des équipements
|
|
|
+ return obj and not Equipment.objects.filter(category__in=obj.get_descendants(include_self=True)).exists()
|
|
|
+
|
|
|
+
|
|
|
+class LocationAdmin(admin.ModelAdmin):
|
|
|
+ def get_actions(self, request):
|
|
|
+ actions = super().get_actions(request)
|
|
|
+ if 'delete_selected' in actions:
|
|
|
+ del actions['delete_selected']
|
|
|
+ return actions
|
|
|
+
|
|
|
+ def has_delete_permission(self, request, obj=None):
|
|
|
+ # Interdiction de supprimer l’emplacement si il contient des équipements
|
|
|
+ return obj and not obj.equipment_set.exists()
|
|
|
+
|
|
|
+
|
|
|
+class EquipmentAdmin(admin.ModelAdmin):
|
|
|
+ list_display = ('location', 'category', 'quantity',)
|
|
|
+ list_display_links = ('location', 'category', 'quantity',)
|
|
|
+ list_filter = (
|
|
|
+ ('location', admin.RelatedOnlyFieldListFilter),
|
|
|
+ CategoryFilter,
|
|
|
+ )
|
|
|
+
|
|
|
+ def get_actions(self, request):
|
|
|
+ actions = super().get_actions(request)
|
|
|
+ if 'delete_selected' in actions:
|
|
|
+ del actions['delete_selected']
|
|
|
+ return actions
|
|
|
+
|
|
|
+ def has_delete_permission(self, request, obj=None):
|
|
|
+ # Interdiction de supprimer le stock d’équipements s’il n’est pas vide
|
|
|
+ return obj and obj.quantity == 0
|
|
|
+
|
|
|
+
|
|
|
+admin.site.register(Category, CategoryAdmin)
|
|
|
+admin.site.register(Location, LocationAdmin)
|
|
|
+admin.site.register(Equipment, EquipmentAdmin)
|