models.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. class Meta:
  44. verbose_name = 'objet'
  45. class Loan(models.Model):
  46. item = models.ForeignKey(Item, verbose_name='objet', related_name='loans')
  47. user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='membre',
  48. related_name='loans')
  49. loan_date = models.DateTimeField(verbose_name='date de prêt')
  50. loan_date_end = models.DateTimeField(verbose_name='date de fin de prêt',
  51. null=True, blank=True)
  52. location = models.CharField(max_length=100, verbose_name='emplacement',
  53. null=True, blank=True)
  54. def __unicode__(self):
  55. return 'prêt de {item} à {user}'.format(item=self.item,
  56. user=self.user)
  57. class Meta:
  58. verbose_name = 'prêt d’objet'
  59. verbose_name_plural = 'prêts d’objets'