# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.contrib import admin from django.contrib.auth import get_user_model from django.utils import timezone from .models import ItemType, Item, Loan, Storage 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 class AvailabilityFilter(admin.SimpleListFilter): title = "Disponibilité" parameter_name = 'availability' def lookups(self, request, model_admin): return [ ('available', 'Disponible'), ('borrowed', 'Emprunté'), ] def queryset(self, request, queryset): if self.value() == 'available': return queryset.available() elif self.value() == 'borrowed': return queryset.borrowed() else: return queryset @admin.register(Item) class ItemAdmin(admin.ModelAdmin): list_display = ( 'designation', 'type', 'mac_address', 'serial', 'owner', 'buy_date', 'is_available') list_filter = ( AvailabilityFilter, 'type__name', 'storage', 'buy_date', OwnerFilter) search_fields = ( 'designation', 'mac_address', 'serial', 'owner__email', 'owner__nickname', 'owner__first_name', 'owner__last_name') actions = ['give_back'] def give_back(self, request, queryset): for item in queryset.filter(loans__loan_date_end=None): item.give_back() give_back.short_description = 'Rendre le matériel' class StatusFilter(admin.SimpleListFilter): title = 'Statut' parameter_name = 'status' def lookups(self, request, model_admin): return [ ('all', 'Tout'), (None, 'En cours'), ('finished', 'Passés'), ] def choices(self, cl): for lookup, title in self.lookup_choices: yield { 'selected': self.value() == lookup, 'query_string': cl.get_query_string({ self.parameter_name: lookup, }, []), 'display': title, } def queryset(self, request, queryset): v = self.value() if v in (None, 'running'): return queryset.running() elif v == 'finished': return queryset.finished() else: return queryset class BorrowerFilter(admin.SimpleListFilter): title = 'Adhérent emprunteur' parameter_name = 'user' def lookups(self, request, model_admin): users = set() for loan in model_admin.get_queryset(request): users.add((loan.user.pk, loan.user)) return users def queryset(self, request, queryset): if self.value(): return queryset.filter(user=self.value()) else: return queryset @admin.register(Loan) class LoanAdmin(admin.ModelAdmin): list_display = ('item', 'user', 'loan_date', 'loan_date_end') list_filter = (StatusFilter, BorrowerFilter, 'item__designation') search_fields = ( 'item__designation', 'user__nickname', 'user__username', 'user__first_name', 'user__last_name', ) actions = ['end_loan'] def end_loan(self, request, queryset): queryset.filter(loan_date_end=None).update( loan_date_end=datetime.now()) end_loan.short_description = 'Mettre fin au prêt' @admin.register(Storage) class StorageAdmin(admin.ModelAdmin): list_display = ('name', 'truncated_notes', 'items_count') def truncated_notes(self, obj): if len(obj.notes) > 50: return '{}…'.format(obj.notes[:50]) else: return obj.notes truncated_notes.short_description = 'notes'