models.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from datetime import date
  4. from django.db import models
  5. from django.conf import settings
  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,
  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=date.today())
  75. class LoanQuerySet(models.QuerySet):
  76. running_filter = (
  77. models.Q(loan_date_end__gt=date.today()) |
  78. models.Q(loan_date_end__isnull=True))
  79. def running(self):
  80. return self.filter(self.running_filter)
  81. def finished(self):
  82. return self.exclude(self.running_filter)
  83. class Loan(models.Model):
  84. item = models.ForeignKey(Item, verbose_name='objet', related_name='loans')
  85. user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='membre',
  86. related_name='loans')
  87. loan_date = models.DateField(verbose_name='date de prêt')
  88. loan_date_end = models.DateField(verbose_name='date de fin de prêt',
  89. null=True, blank=True)
  90. notes = models.TextField(null=True, blank=True)
  91. def __unicode__(self):
  92. return 'prêt de {item} à {user}'.format(
  93. item=self.item, user=self.user)
  94. def user_can_close(self, user):
  95. return (not self.item.is_available()) and (self.user == user)
  96. class Meta:
  97. verbose_name = 'prêt d’objet'
  98. verbose_name_plural = 'prêts d’objets'
  99. objects = LoanQuerySet().as_manager()
  100. class Storage(models.Model):
  101. name = models.CharField(max_length=100, verbose_name='nom')
  102. notes = models.TextField(
  103. blank=True,
  104. help_text='Lisible par tous les adhérents')
  105. def __unicode__(self):
  106. return self.name
  107. def items_count(self):
  108. return self.items.count()
  109. items_count.short_description = 'Nb. items stockés'
  110. class Meta:
  111. verbose_name = 'lieu de stockage'
  112. verbose_name_plural = 'lieux de stockage'