models.py 3.5 KB

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