models.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. from django.conf import settings
  2. from django.core.exceptions import ValidationError
  3. from django.core.urlresolvers import reverse
  4. from django.db import models
  5. from .validators import less_than_one
  6. class AbstractItem(models.Model):
  7. name = models.CharField(max_length=130)
  8. description = models.TextField(blank=True)
  9. def __str__(self):
  10. return self.name
  11. def get_use_class(self):
  12. raise NotImplemented
  13. def used(self, except_by=None):
  14. """ Return the used fraction of an item
  15. :type: Service
  16. :param except_by: exclude this service from the math
  17. :rtype: float
  18. """
  19. sharing_costs = self.get_use_class().objects.filter(resource=self)
  20. if except_by:
  21. sharing_costs = sharing_costs.exclude(service=except_by)
  22. existing_uses_sum = sum(
  23. sharing_costs.values_list('share', flat=True))
  24. return existing_uses_sum
  25. class Meta:
  26. abstract = True
  27. class Cost(AbstractItem):
  28. """ A monthtly cost we have to pay
  29. """
  30. price = models.FloatField(help_text="Coût mensuel")
  31. def get_use_class(self):
  32. return CostUse
  33. class Meta:
  34. verbose_name = 'Coût'
  35. class Good(AbstractItem):
  36. """ A good, which replacement is provisioned
  37. """
  38. price = models.FloatField()
  39. provisioning_duration = models.DurationField(
  40. choices=settings.PROVISIONING_DURATIONS)
  41. def get_use_class(self):
  42. return GoodUse
  43. def monthly_provision(self):
  44. return self.price/self.provisioning_duration.days*(365.25/12)
  45. class Meta:
  46. verbose_name = 'Bien'
  47. class AbstractUse(models.Model):
  48. share = models.FloatField(validators=[less_than_one])
  49. service = models.ForeignKey('Service')
  50. class Meta:
  51. abstract = True
  52. def clean(self):
  53. if hasattr(self, 'resource'):
  54. if (self.resource.used(except_by=self.service) + self.share) > 1:
  55. raise ValidationError(
  56. "Cannot use more than 100% of {})".format(self.resource))
  57. class CostUse(AbstractUse):
  58. resource = models.ForeignKey(Cost)
  59. def cost_share(self):
  60. return self.share*self.resource.price
  61. class GoodUse(AbstractUse):
  62. resource = models.ForeignKey(Good)
  63. def monthly_provision_share(self):
  64. return self.real_share()*self.resource.monthly_provision()
  65. class Service(AbstractItem):
  66. """ A service we sell
  67. (considered monthly)
  68. """
  69. costs = models.ManyToManyField(
  70. Cost,
  71. through=CostUse,
  72. related_name='using_services')
  73. goods = models.ManyToManyField(
  74. Good,
  75. through=GoodUse,
  76. related_name='using_services')
  77. # services = models.ManyToMany('Service') #TODO
  78. def get_absolute_url(self):
  79. return reverse('detail-service', kwargs={'pk': self.pk})