admin.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. from coin.members.admin import MemberAdmin
  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', '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. @admin.register(Loan)
  94. class LoanAdmin(admin.ModelAdmin):
  95. list_display = ('item', 'get_mac_or_serial', 'user', 'loan_date', 'loan_date_end')
  96. list_filter = (StatusFilter, BorrowerFilter, 'item__designation')
  97. search_fields = (
  98. 'item__designation',
  99. 'user__nickname', 'user__username',
  100. 'user__first_name', 'user__last_name', )
  101. actions = ['end_loan']
  102. def end_loan(self, request, queryset):
  103. queryset.filter(loan_date_end=None).update(
  104. loan_date_end=datetime.now())
  105. end_loan.short_description = 'Mettre fin au prêt'
  106. @admin.register(Storage)
  107. class StorageAdmin(admin.ModelAdmin):
  108. list_display = ('name', 'truncated_notes', 'items_count')
  109. def truncated_notes(self, obj):
  110. if len(obj.notes) > 50:
  111. return '{}…'.format(obj.notes[:50])
  112. else:
  113. return obj.notes
  114. truncated_notes.short_description = 'notes'
  115. class LoanInline(admin.TabularInline):
  116. model = Loan
  117. extra = 0
  118. exclude = ('notes',)
  119. readonly_fields = ('item', 'get_mac_or_serial', 'loan_date', 'loan_date_end', 'is_running')
  120. show_change_link = True
  121. def get_queryset(self, request):
  122. qs = super(LoanInline, self).get_queryset(request)
  123. return qs.order_by('-loan_date_end')
  124. def has_add_permission(self, request, obj=None):
  125. return False
  126. def has_delete_permission(self, request, obj=None):
  127. return False
  128. # Avoid to add LoanInline twice in case the file is loaded more than
  129. # once.
  130. if LoanInline not in MemberAdmin.inlines:
  131. MemberAdmin.inlines.append(LoanInline)