|
@@ -0,0 +1,78 @@
|
|
|
+from django.contrib import admin
|
|
|
+from django.db.models import Q
|
|
|
+from django.utils import timezone
|
|
|
+
|
|
|
+from .models import Service, ServiceType, IPResource
|
|
|
+
|
|
|
+
|
|
|
+class ServiceTypeFilter(admin.SimpleListFilter):
|
|
|
+ title = 'type de service'
|
|
|
+ parameter_name = 'type'
|
|
|
+
|
|
|
+ def lookups(self, request, model_admin):
|
|
|
+ return ServiceType.objects.all().values_list('pk', 'name')
|
|
|
+
|
|
|
+ def queryset(self, request, queryset):
|
|
|
+ if self.value():
|
|
|
+ return queryset.filter(service_type__pk=self.value())
|
|
|
+
|
|
|
+
|
|
|
+class ServiceStatusFilter(admin.SimpleListFilter):
|
|
|
+ title = 'status du service'
|
|
|
+ parameter_name = 'status'
|
|
|
+
|
|
|
+ def lookups(self, request, model_admin):
|
|
|
+ return (
|
|
|
+ ('ongoing', 'En cours'),
|
|
|
+ ('finished', 'Terminé'),
|
|
|
+ )
|
|
|
+
|
|
|
+ def queryset(self, request, queryset):
|
|
|
+ if self.value() == 'ongoing':
|
|
|
+ return queryset.filter(end__isnull=True)
|
|
|
+ if self.value() == 'finished':
|
|
|
+ return queryset.filter(end__isnull=False)
|
|
|
+
|
|
|
+
|
|
|
+class ServiceAdmin(admin.ModelAdmin):
|
|
|
+ list_display = ('id', 'adherent', 'service_type', 'start', 'end_view',)
|
|
|
+ list_filter = (ServiceStatusFilter, ServiceTypeFilter,)
|
|
|
+
|
|
|
+ def end_view(self, obj):
|
|
|
+ return obj.end
|
|
|
+ end_view.short_description = 'Fin du service'
|
|
|
+ end_view.empty_value_display = 'Service en cours'
|
|
|
+
|
|
|
+
|
|
|
+class InUseFilter(admin.SimpleListFilter):
|
|
|
+ title = 'disponibilité'
|
|
|
+ parameter_name = 'available'
|
|
|
+
|
|
|
+ def lookups(self, request, model_admin):
|
|
|
+ return (
|
|
|
+ (1, 'Disponible'),
|
|
|
+ (0, 'Non disponible'),
|
|
|
+ )
|
|
|
+
|
|
|
+ def queryset(self, request, queryset):
|
|
|
+ in_use_filter = Q(service__start__gte=timezone.now()) \
|
|
|
+ | Q(service__end__isnull=True)
|
|
|
+ if self.value() == '0':
|
|
|
+ return queryset.filter(in_use_filter)
|
|
|
+ if self.value() == '1':
|
|
|
+ return queryset.exclude(in_use_filter)
|
|
|
+
|
|
|
+
|
|
|
+class IPResourceAdmin(admin.ModelAdmin):
|
|
|
+ list_display = ('__str__', 'in_use_view')
|
|
|
+ list_filter = (InUseFilter,)
|
|
|
+
|
|
|
+ def in_use_view(self, obj):
|
|
|
+ return not obj.in_use
|
|
|
+ in_use_view.short_description = 'Disponible'
|
|
|
+ in_use_view.boolean = True
|
|
|
+
|
|
|
+
|
|
|
+admin.site.register(Service, ServiceAdmin)
|
|
|
+admin.site.register(ServiceType)
|
|
|
+admin.site.register(IPResource, IPResourceAdmin)
|