models.py 3.3 KB

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