|
@@ -1,9 +1,17 @@
|
|
|
from django.contrib import admin
|
|
|
+from django.db.models import Q
|
|
|
+from django.utils import timezone
|
|
|
|
|
|
-from .models import Service, ServiceType, IPResource
|
|
|
+from .models import Service, ServiceType, IPResource, ResourceAllocation
|
|
|
from banking.admin import PaymentInline, ValidatedPaymentInline, PendingOrNewPaymentInline
|
|
|
|
|
|
|
|
|
+class AllocationInline(admin.TabularInline):
|
|
|
+ model = ResourceAllocation
|
|
|
+ extra = 1
|
|
|
+ can_delete = False
|
|
|
+
|
|
|
+
|
|
|
class ServiceTypeFilter(admin.SimpleListFilter):
|
|
|
title = 'type de service'
|
|
|
parameter_name = 'type'
|
|
@@ -19,34 +27,9 @@ class ServiceTypeFilter(admin.SimpleListFilter):
|
|
|
return queryset.filter(service_type__pk=self.value())
|
|
|
|
|
|
|
|
|
-class ServiceStatusFilter(admin.SimpleListFilter):
|
|
|
- title = 'statut'
|
|
|
- parameter_name = 'status'
|
|
|
-
|
|
|
- def lookups(self, request, model_admin):
|
|
|
- return (
|
|
|
- ('forthcoming', 'Non commencé'),
|
|
|
- ('ongoing', 'En cours'),
|
|
|
- ('finished', 'Terminé'),
|
|
|
- )
|
|
|
-
|
|
|
- def queryset(self, request, queryset):
|
|
|
- if self.value() == 'forthcoming':
|
|
|
- return queryset.filter(Service.get_forthcoming_filter())
|
|
|
- if self.value() == 'ongoing':
|
|
|
- return queryset.filter(Service.get_ongoing_filter())
|
|
|
- if self.value() == 'finished':
|
|
|
- return queryset.filter(Service.get_finished_filter())
|
|
|
-
|
|
|
-
|
|
|
class ServiceAdmin(admin.ModelAdmin):
|
|
|
- list_display = ('id', 'adherent', 'service_type', 'label', 'start', 'end', 'status')
|
|
|
- list_filter = (ServiceStatusFilter, ServiceTypeFilter,)
|
|
|
-
|
|
|
- def status(self, obj):
|
|
|
- return obj.is_ongoing
|
|
|
- status.short_description = 'En cours'
|
|
|
- status.boolean = True
|
|
|
+ list_display = ('id', 'adherent', 'service_type', 'label', 'active')
|
|
|
+ list_filter = ('active', ServiceTypeFilter,)
|
|
|
|
|
|
def get_form(self, request, obj=None, **kwargs):
|
|
|
# get_inlines does not exists :-(
|
|
@@ -54,6 +37,7 @@ class ServiceAdmin(admin.ModelAdmin):
|
|
|
self.inlines = (PaymentInline,)
|
|
|
else:
|
|
|
self.inlines = (ValidatedPaymentInline, PendingOrNewPaymentInline,)
|
|
|
+ self.inlines = self.inlines + (AllocationInline,)
|
|
|
return super().get_form(request, obj, **kwargs)
|
|
|
|
|
|
def get_queryset(self, request):
|
|
@@ -80,16 +64,20 @@ class InUseFilter(admin.SimpleListFilter):
|
|
|
)
|
|
|
|
|
|
def queryset(self, request, queryset):
|
|
|
- ongoing_filter = Service.get_ongoing_filter()
|
|
|
- if self.value() == '0':
|
|
|
- return queryset.filter(ongoing_filter)
|
|
|
- if self.value() == '1':
|
|
|
- return queryset.exclude(ongoing_filter)
|
|
|
+ # On utilise le même filtre que get_active_filter(), mais préfixé par « allocation__ » …
|
|
|
+ # Ce billet donne une technique avec une classe PrefixedQ pour éviter cette duplication de code :
|
|
|
+ # https://hackerluddite.wordpress.com/2012/07/07/making-django-orm-more-dry-with-prefixes-and-qs/
|
|
|
+ now = timezone.now()
|
|
|
+ if self.value() == '0': # non disponible
|
|
|
+ return queryset.filter(Q(allocation__start__lte=now) & (Q(allocation__end__isnull=True) | Q(allocation__end__gte=now)))
|
|
|
+ if self.value() == '1': # disponible
|
|
|
+ return queryset.exclude(Q(allocation__start__lte=now) & (Q(allocation__end__isnull=True) | Q(allocation__end__gte=now)))
|
|
|
|
|
|
|
|
|
class IPResourceAdmin(admin.ModelAdmin):
|
|
|
list_display = ('__str__', 'in_use_view')
|
|
|
list_filter = (InUseFilter,)
|
|
|
+ inlines = (AllocationInline,)
|
|
|
|
|
|
def in_use_view(self, obj):
|
|
|
return not obj.in_use
|
|
@@ -97,6 +85,7 @@ class IPResourceAdmin(admin.ModelAdmin):
|
|
|
in_use_view.boolean = True
|
|
|
|
|
|
|
|
|
-admin.site.register(Service, ServiceAdmin)
|
|
|
admin.site.register(ServiceType)
|
|
|
+admin.site.register(Service, ServiceAdmin)
|
|
|
admin.site.register(IPResource, IPResourceAdmin)
|
|
|
+admin.site.register(ResourceAllocation)
|