123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- from django.conf import settings
- from django.core.exceptions import ValidationError
- from django.db import models
- from .validators import less_than_one
- class AbstractItem(models.Model):
- name = models.CharField(max_length=130)
- description = models.TextField(blank=True)
- def __str__(self):
- return self.name
- class Meta:
- abstract = True
- class Cost(AbstractItem):
- """ A monthtly cost we have to pay
- """
- price = models.PositiveIntegerField(help_text="Coût mensuel")
- class Meta:
- verbose_name = 'Coût'
- def used(self):
- sharing_costs = CostUse.objects.filter(resource=self)
- existing_uses_sum = sum(
- sharing_costs.values_list('share', flat=True))
- return existing_uses_sum
- class Good(AbstractItem):
- """ A good, which replacement is provisioned
- """
- price = models.PositiveIntegerField()
- provisioning_duration = models.DurationField(
- choices=settings.PROVISIONING_DURATIONS)
- def get_use_class(self):
- return GoodUse
- def monthly_provision(self):
- return self.price/self.provisioning_duration.days*(365.25/12)
- class Meta:
- verbose_name = 'Bien'
- class AbstractUse(models.Model):
- share = models.FloatField(validators=[less_than_one])
- service = models.ForeignKey('Service')
- class Meta:
- abstract = True
- def clean(self):
- if hasattr(self, 'resource'):
- if (self.resource.used() + self.share) > 1:
- raise ValidationError(
- "Cannot use more than 100% of {})".format(self.resource))
- class CostUse(AbstractUse):
- resource = models.ForeignKey(Cost)
- class GoodUse(AbstractUse):
- resource = models.ForeignKey(Good)
- def monthly_provision_share(self):
- return self.real_share()*self.resource.monthly_provision()
- class Service(AbstractItem):
- """ A service we sell
- (considered monthly)
- """
- costs = models.ManyToManyField(
- Cost,
- through=CostUse,
- related_name='using_services')
- goods = models.ManyToManyField(
- Good,
- through=GoodUse,
- related_name='using_services')
- # services = models.ManyToMany('Service') #TODO
|