models.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.db import models
  4. from django.conf import settings
  5. from .fields import MACAddressField
  6. class ItemType(models.Model):
  7. name = models.CharField(max_length=100, verbose_name='nom')
  8. def __unicode__(self):
  9. return self.name
  10. class Meta:
  11. verbose_name = 'type d’objet'
  12. verbose_name_plural = 'types d’objet'
  13. class Item(models.Model):
  14. type = models.ForeignKey(ItemType, verbose_name='type de matériel',
  15. related_name='items')
  16. designation = models.CharField(max_length=100, verbose_name='désignation')
  17. mac_address = MACAddressField(verbose_name='addresse MAC', blank=True,
  18. null=True)
  19. buy_date = models.DateTimeField(verbose_name='date d’achat')
  20. user_in_charge = models.ForeignKey(settings.AUTH_USER_MODEL,
  21. verbose_name='membre responsable',
  22. related_name='items')
  23. comment = models.TextField(verbose_name='commentaire', blank=True,
  24. null=True)
  25. def __unicode__(self):
  26. return self.designation
  27. def get_current_loan(self):
  28. """
  29. Returns the current Loan for this Item, if exists, or None.
  30. """
  31. try:
  32. return self.loans.get(loan_date_end__isnull=True)
  33. except Loan.DoesNotExist:
  34. return None
  35. def is_available(self):
  36. """
  37. Returns the status of the Item. If a Loan without an end date exists,
  38. returns False (else True).
  39. """
  40. if self.loans.filter(loan_date_end__isnull=True).count():
  41. return False
  42. return True
  43. is_available.boolean = True
  44. is_available.short_description = 'disponible'
  45. class Meta:
  46. verbose_name = 'objet'
  47. class Loan(models.Model):
  48. item = models.ForeignKey(Item, verbose_name='objet', related_name='loans')
  49. user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='membre',
  50. related_name='loans')
  51. loan_date = models.DateTimeField(verbose_name='date de prêt')
  52. loan_date_end = models.DateTimeField(verbose_name='date de fin de prêt',
  53. null=True, blank=True)
  54. location = models.CharField(max_length=100, verbose_name='emplacement',
  55. null=True, blank=True)
  56. def __unicode__(self):
  57. return 'prêt de {item} à {user}'.format(item=self.item,
  58. user=self.user)
  59. class Meta:
  60. verbose_name = 'prêt d’objet'
  61. verbose_name_plural = 'prêts d’objets'