admin.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. save_as = True
  50. actions = ['give_back']
  51. def give_back(self, request, queryset):
  52. for item in queryset.filter(loans__loan_date_end=None):
  53. item.give_back()
  54. give_back.short_description = 'Rendre le matériel'
  55. class StatusFilter(admin.SimpleListFilter):
  56. title = 'Statut'
  57. parameter_name = 'status'
  58. def lookups(self, request, model_admin):
  59. return [
  60. ('all', 'Tout'),
  61. (None, 'En cours'),
  62. ('finished', 'Passés'),
  63. ]
  64. def choices(self, cl):
  65. for lookup, title in self.lookup_choices:
  66. yield {
  67. 'selected': self.value() == lookup,
  68. 'query_string': cl.get_query_string({
  69. self.parameter_name: lookup,
  70. }, []),
  71. 'display': title,
  72. }
  73. def queryset(self, request, queryset):
  74. v = self.value()
  75. if v in (None, 'running'):
  76. return queryset.running()
  77. elif v == 'finished':
  78. return queryset.finished()
  79. else:
  80. return queryset
  81. class BorrowerFilter(admin.SimpleListFilter):
  82. title = 'Adhérent emprunteur'
  83. parameter_name = 'user'
  84. def lookups(self, request, model_admin):
  85. users = set()
  86. for loan in model_admin.get_queryset(request):
  87. users.add((loan.user.pk, loan.user))
  88. return users
  89. def queryset(self, request, queryset):
  90. if self.value():
  91. return queryset.filter(user=self.value())
  92. else:
  93. return queryset
  94. @admin.register(Loan)
  95. class LoanAdmin(admin.ModelAdmin):
  96. list_display = ('item', 'user', 'loan_date', 'loan_date_end')
  97. list_filter = (StatusFilter, BorrowerFilter, 'item__designation')
  98. search_fields = (
  99. 'item__designation',
  100. 'user__nickname', 'user__username',
  101. 'user__first_name', 'user__last_name', )
  102. actions = ['end_loan']
  103. def end_loan(self, request, queryset):
  104. queryset.filter(loan_date_end=None).update(
  105. loan_date_end=datetime.now())
  106. end_loan.short_description = 'Mettre fin au prêt'
  107. @admin.register(Storage)
  108. class StorageAdmin(admin.ModelAdmin):
  109. list_display = ('name', 'truncated_notes', 'items_count')
  110. def truncated_notes(self, obj):
  111. if len(obj.notes) > 50:
  112. return '{}…'.format(obj.notes[:50])
  113. else:
  114. return obj.notes
  115. truncated_notes.short_description = 'notes'
  116. class LoanInline(admin.TabularInline):
  117. model = Loan
  118. extra = 0
  119. exclude = ('notes',)
  120. readonly_fields = ('item', 'get_mac_or_serial', 'loan_date', 'loan_date_end', 'is_running')
  121. show_change_link = True
  122. def get_queryset(self, request):
  123. qs = super(LoanInline, self).get_queryset(request)
  124. return qs.order_by('-loan_date_end')
  125. def has_add_permission(self, request, obj=None):
  126. return False
  127. def has_delete_permission(self, request, obj=None):
  128. return False
  129. # Avoid to add LoanInline twice in case the file is loaded more than
  130. # once.
  131. if LoanInline not in MemberAdmin.inlines:
  132. MemberAdmin.inlines.append(LoanInline)