models.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.db import models
  4. from django.conf import settings
  5. from django.utils import timezone
  6. from .fields import MACAddressField
  7. class ItemType(models.Model):
  8. name = models.CharField(max_length=100, verbose_name='nom')
  9. def __unicode__(self):
  10. return self.name
  11. class Meta:
  12. verbose_name = 'type d’objet'
  13. verbose_name_plural = 'types d’objet'
  14. class ItemQuerySet(models.QuerySet):
  15. def _get_borrowed_pks(self):
  16. return Loan.objects.running().values_list('item', flat=True)
  17. def available(self):
  18. return self.exclude(pk__in=self._get_borrowed_pks())
  19. def borrowed(self):
  20. return self.filter(pk__in=self._get_borrowed_pks())
  21. class Item(models.Model):
  22. type = models.ForeignKey(ItemType, verbose_name='type de matériel',
  23. related_name='items')
  24. designation = models.CharField(max_length=100, verbose_name='désignation')
  25. storage = models.ForeignKey(
  26. 'Storage', related_name='items',
  27. verbose_name='Lieu de stockage',
  28. null=True, blank=True,
  29. help_text='Laisser vide si inconnu')
  30. mac_address = MACAddressField(
  31. verbose_name='addresse MAC',
  32. blank=True, null=True, unique=True,
  33. help_text="préférable au n° de série si possible")
  34. serial = models.CharField(
  35. verbose_name='N° de série',
  36. max_length=250, blank=True,
  37. help_text='ou toute autre référence unique)')
  38. buy_date = models.DateField(verbose_name='date d’achat')
  39. owner = models.ForeignKey(
  40. settings.AUTH_USER_MODEL,
  41. verbose_name='Propriétaire',
  42. related_name='items',
  43. null=True, blank=True,
  44. help_text="dans le cas de matériel n'appartenant pas à l'association")
  45. comment = models.TextField(verbose_name='commentaire', blank=True,
  46. null=True)
  47. objects = ItemQuerySet().as_manager()
  48. def __unicode__(self):
  49. return self.designation
  50. def get_current_loan(self):
  51. """
  52. Returns the current Loan for this Item, if exists, or None.
  53. """
  54. try:
  55. return self.loans.get(loan_date_end__isnull=True)
  56. except Loan.DoesNotExist:
  57. return None
  58. def is_available(self):
  59. """
  60. Returns the status of the Item. If a Loan without an end date exists,
  61. returns False (else True).
  62. """
  63. if self.loans.running().exists():
  64. return False
  65. return True
  66. is_available.boolean = True
  67. is_available.short_description = 'disponible'
  68. class Meta:
  69. verbose_name = 'objet'
  70. def give_back(self, storage=None):
  71. self.storage = storage
  72. self.save()
  73. self.loans.running().update(
  74. loan_date_end=timezone.now())
  75. class LoanQuerySet(models.QuerySet):
  76. @staticmethod
  77. def _running_filter():
  78. return (
  79. models.Q(loan_date_end__gt=timezone.now()) |
  80. models.Q(loan_date_end__isnull=True))
  81. def running(self):
  82. return self.filter(self._running_filter())
  83. def finished(self):
  84. return self.exclude(self._running_filter())
  85. class Loan(models.Model):
  86. item = models.ForeignKey(Item, verbose_name='objet', related_name='loans')
  87. user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='membre',
  88. related_name='loans')
  89. loan_date = models.DateTimeField(verbose_name='date de prêt')
  90. loan_date_end = models.DateTimeField(verbose_name='date de fin de prêt',
  91. null=True, blank=True)
  92. notes = models.TextField(null=True, blank=True)
  93. def __unicode__(self):
  94. return 'prêt de {item} à {user}'.format(
  95. item=self.item, user=self.user)
  96. def user_can_close(self, user):
  97. return (not self.item.is_available()) and (self.user == user)
  98. class Meta:
  99. verbose_name = 'prêt d’objet'
  100. verbose_name_plural = 'prêts d’objets'
  101. objects = LoanQuerySet().as_manager()
  102. class Storage(models.Model):
  103. name = models.CharField(max_length=100, verbose_name='nom')
  104. notes = models.TextField(
  105. blank=True,
  106. help_text='Lisible par tous les adhérents')
  107. def __unicode__(self):
  108. return self.name
  109. def items_count(self):
  110. return self.items.count()
  111. items_count.short_description = 'Nb. items stockés'
  112. class Meta:
  113. verbose_name = 'lieu de stockage'
  114. verbose_name_plural = 'lieux de stockage'