admin.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from datetime import date
  4. from django.contrib import admin
  5. from django.contrib.auth import get_user_model
  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. @admin.register(Item)
  22. class ItemAdmin(admin.ModelAdmin):
  23. list_display = (
  24. 'designation', 'type', 'mac_address', 'serial', 'owner',
  25. 'buy_date', 'is_available')
  26. list_filter = ('type__name', 'storage', 'buy_date', OwnerFilter)
  27. search_fields = (
  28. 'designation', 'mac_address', 'serial',
  29. 'owner__email', 'owner__nickname',
  30. 'owner__first_name', 'owner__last_name')
  31. actions = ['give_back']
  32. def give_back(self, request, queryset):
  33. for item in queryset.filter(loans__loan_date_end=None):
  34. item.give_back()
  35. give_back.short_description = 'Rendre le matériel'
  36. class StatusFilter(admin.SimpleListFilter):
  37. title = 'Statut'
  38. parameter_name = 'status'
  39. def lookups(self, request, model_admin):
  40. return [
  41. ('all', 'Tout'),
  42. (None, 'En cours'),
  43. ('finished', 'Passés'),
  44. ]
  45. def choices(self, cl):
  46. for lookup, title in self.lookup_choices:
  47. yield {
  48. 'selected': self.value() == lookup,
  49. 'query_string': cl.get_query_string({
  50. self.parameter_name: lookup,
  51. }, []),
  52. 'display': title,
  53. }
  54. def queryset(self, request, queryset):
  55. v = self.value()
  56. if v in (None, 'running'):
  57. return queryset.running()
  58. elif v == 'finished':
  59. return queryset.finished()
  60. else:
  61. return queryset
  62. class BorrowerFilter(admin.SimpleListFilter):
  63. title = 'Adhérent emprunteur'
  64. parameter_name = 'user'
  65. def lookups(self, request, model_admin):
  66. users = set()
  67. for loan in model_admin.get_queryset(request):
  68. users.add((loan.user.pk, loan.user))
  69. return users
  70. def queryset(self, request, queryset):
  71. if self.value():
  72. return queryset.filter(user=self.value())
  73. else:
  74. return queryset
  75. @admin.register(Loan)
  76. class LoanAdmin(admin.ModelAdmin):
  77. list_display = ('item', 'user', 'loan_date', 'loan_date_end')
  78. list_filter = (StatusFilter, BorrowerFilter, 'item__designation')
  79. search_fields = (
  80. 'item__designation',
  81. 'user__nickname', 'user__username',
  82. 'user__first_name', 'user__last_name', )
  83. actions = ['end_loan']
  84. def end_loan(self, request, queryset):
  85. queryset.filter(loan_date_end=None).update(
  86. loan_date_end=date.today())
  87. end_loan.short_description = 'Mettre fin au prêt'
  88. @admin.register(Storage)
  89. class StorageAdmin(admin.ModelAdmin):
  90. list_display = ('name', 'truncated_notes', 'items_count')
  91. def truncated_notes(self, obj):
  92. if len(obj.notes) > 50:
  93. return '{}…'.format(obj.notes[:50])
  94. else:
  95. return obj.note
  96. truncated_notes.short_description = 'notes'