admin.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.contrib import admin
  4. from django.contrib.auth import get_user_model
  5. from django.utils import timezone
  6. from .models import ItemType, Item, Loan, Storage
  7. User = get_user_model()
  8. admin.site.register(ItemType)
  9. class OwnerFilter(admin.SimpleListFilter):
  10. title = "Propriétaire"
  11. parameter_name = 'owner'
  12. def lookups(self, request, model_admin):
  13. owners = [
  14. (i.pk, i) for i in User.objects.filter(items__isnull=False)]
  15. return [(None, "L'association")] + owners
  16. def queryset(self, request, queryset):
  17. if self.value():
  18. return queryset.filter(owner__pk=self.value())
  19. else:
  20. return queryset
  21. class AvailabilityFilter(admin.SimpleListFilter):
  22. title = "Disponibilité"
  23. parameter_name = 'availability'
  24. def lookups(self, request, model_admin):
  25. return [
  26. ('available', 'Disponible'),
  27. ('borrowed', 'Emprunté'),
  28. ]
  29. def queryset(self, request, queryset):
  30. if self.value() == 'available':
  31. return queryset.available()
  32. elif self.value() == 'borrowed':
  33. return queryset.borrowed()
  34. else:
  35. return queryset
  36. @admin.register(Item)
  37. class ItemAdmin(admin.ModelAdmin):
  38. list_display = (
  39. 'designation', 'type', 'mac_address', 'serial', 'owner',
  40. 'buy_date', 'is_available')
  41. list_filter = (
  42. AvailabilityFilter, 'type__name', 'storage',
  43. 'buy_date', OwnerFilter)
  44. search_fields = (
  45. 'designation', 'mac_address', 'serial',
  46. 'owner__email', 'owner__nickname',
  47. 'owner__first_name', 'owner__last_name')
  48. actions = ['give_back']
  49. def give_back(self, request, queryset):
  50. for item in queryset.filter(loans__loan_date_end=None):
  51. item.give_back()
  52. give_back.short_description = 'Rendre le matériel'
  53. class StatusFilter(admin.SimpleListFilter):
  54. title = 'Statut'
  55. parameter_name = 'status'
  56. def lookups(self, request, model_admin):
  57. return [
  58. ('all', 'Tout'),
  59. (None, 'En cours'),
  60. ('finished', 'Passés'),
  61. ]
  62. def choices(self, cl):
  63. for lookup, title in self.lookup_choices:
  64. yield {
  65. 'selected': self.value() == lookup,
  66. 'query_string': cl.get_query_string({
  67. self.parameter_name: lookup,
  68. }, []),
  69. 'display': title,
  70. }
  71. def queryset(self, request, queryset):
  72. v = self.value()
  73. if v in (None, 'running'):
  74. return queryset.running()
  75. elif v == 'finished':
  76. return queryset.finished()
  77. else:
  78. return queryset
  79. class BorrowerFilter(admin.SimpleListFilter):
  80. title = 'Adhérent emprunteur'
  81. parameter_name = 'user'
  82. def lookups(self, request, model_admin):
  83. users = set()
  84. for loan in model_admin.get_queryset(request):
  85. users.add((loan.user.pk, loan.user))
  86. return users
  87. def queryset(self, request, queryset):
  88. if self.value():
  89. return queryset.filter(user=self.value())
  90. else:
  91. return queryset
  92. @admin.register(Loan)
  93. class LoanAdmin(admin.ModelAdmin):
  94. list_display = ('item', 'user', 'loan_date', 'loan_date_end')
  95. list_filter = (StatusFilter, BorrowerFilter, 'item__designation')
  96. search_fields = (
  97. 'item__designation',
  98. 'user__nickname', 'user__username',
  99. 'user__first_name', 'user__last_name', )
  100. actions = ['end_loan']
  101. def end_loan(self, request, queryset):
  102. queryset.filter(loan_date_end=None).update(
  103. loan_date_end=datetime.now())
  104. end_loan.short_description = 'Mettre fin au prêt'
  105. @admin.register(Storage)
  106. class StorageAdmin(admin.ModelAdmin):
  107. list_display = ('name', 'truncated_notes', 'items_count')
  108. def truncated_notes(self, obj):
  109. if len(obj.notes) > 50:
  110. return '{}…'.format(obj.notes[:50])
  111. else:
  112. return obj.notes
  113. truncated_notes.short_description = 'notes'