expephalon/core/models/services.py

42 lines
1.5 KiB
Python
Raw Normal View History

from core.models.billable import CycleChoices
from core.models.products import ProductGroup
from core.fields.numbers import CostField
from django.db.models import Model, TextField, CharField, ManyToManyField, ForeignKey, CASCADE, PositiveIntegerField, 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 = CostField()