Commit c947623a authored by jocelyn's avatar jocelyn

Allow to filter by borrower in item admin listing view

parent 34c04868
......@@ -114,6 +114,34 @@ class AddLoanInline(admin.StackedInline):
return False
class BorrowerFilter(admin.SimpleListFilter):
title = 'détenteur actuel'
parameter_name = 'user'
def _filter_loans(self, items_queryset, user_pk=None):
qs = Loan.objects.running().filter(item__in=items_queryset)
if user_pk is not None:
qs.filter(user=user_pk)
return qs
def lookups(self, request, model_admin):
# Get relevant (and authorized) users only
relevant_items = model_admin.get_queryset(request)
users = set()
for loan in self._filter_loans(relevant_items):
users.add((loan.user.pk, loan.user))
return users
def queryset(self, request, queryset):
if self.value():
loans_qs = self._filter_loans(queryset).filter(
user__pk=self.value(),
)
return queryset.filter(loans__in=loans_qs)
else:
return queryset
@admin.register(Item)
class ItemAdmin(admin.ModelAdmin):
list_display = (
......@@ -125,7 +153,7 @@ class ItemAdmin(admin.ModelAdmin):
)
list_filter = (
AvailabilityFilter, 'type', 'storage',
'buy_date', OwnerFilter)
'buy_date', BorrowerFilter, OwnerFilter)
search_fields = (
'designation', 'mac_address', 'serial',
'owner__email', 'owner__nickname',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment