expephalon/core/models/services.py
Klaus-Uwe Mitterer 95c9748dc2 Base logging setup
Models for products/services/billables
2020-05-27 12:36:49 +02:00

41 lines
No EOL
1.5 KiB
Python

from core.models.billable import CycleChoices
from core.models.products import ProductGroup
from django.db.models import Model, TextField, CharField, ManyToManyField, ForeignKey, CASCADE, PositiveIntegerField, DecimalField, OneToOneField
from importlib import import_module
from logging import getLogger
logger = getLogger(__name__)
class Service(Model):
name = CharField(max_length=255)
description = TextField(null=True, blank=True)
handler_module = CharField(max_length=255, null=True, blank=True)
product_groups = ManyToManyField(ProductGroup)
@property
def handler(self):
if self.handler_module:
try:
handler_module = import_module(self.handler_module)
return handler_module.ProductRouter(self.id)
except Exception as e:
logger.error(f"Could not load product handler {self.handler_module} for product {self.id}: {e}")
return None
class ServicePlan(Model):
service = OneToOneField(Service, on_delete=CASCADE)
@classmethod
def from_productplan(cls, productplan, service):
plan = cls.objects.create(service=service)
for item in productplan.serviceplanitem_set:
ServicePlanItem.objects.create(plan=plan, cycle=item.cycle, count=item.count, cost=item.cost)
class ServicePlanItem(Model):
plan = ForeignKey(ServicePlan, on_delete=CASCADE)
cycle = PositiveIntegerField(choices=CycleChoices.choices)
count = PositiveIntegerField()
cost = DecimalField(max_digits=30, decimal_places=2)