models.py 3.2 KB

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