admin.py 4.7 KB

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