admin.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from datetime import date
  4. from django.contrib import admin
  5. from .models import ItemType, Item, Loan
  6. admin.site.register(ItemType)
  7. @admin.register(Item)
  8. class ItemAdmin(admin.ModelAdmin):
  9. list_display = ('designation', 'type', 'mac_address', 'user_in_charge',
  10. 'buy_date', 'is_available')
  11. list_filter = ('type__name', 'buy_date')
  12. search_fields = ('designation', 'user_in_charge')
  13. actions = ['give_back']
  14. def give_back(self, request, queryset):
  15. for item in queryset.filter(loans__loan_date_end=None):
  16. item.give_back()
  17. give_back.short_description = 'Rendre le matériel'
  18. @admin.register(Loan)
  19. class LoanAdmin(admin.ModelAdmin):
  20. list_display = ('item', 'user', 'loan_date', 'loan_date_end', 'location')
  21. list_filter = ('item__designation', 'user__username')
  22. search_fields = ('item', 'user')
  23. actions = ['end_loan']
  24. def end_loan(self, request, queryset):
  25. queryset.filter(loan_date_end=None).update(
  26. loan_date_end=date.today())
  27. end_loan.short_description = 'Mettre fin au prêt'