models.py 4.0 KB

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