expephalon/core/models/services.py

43 lines
1.6 KiB
Python

from core.fields.numbers import CostField
from core.models.local import Currency
from core.models.profiles import ClientProfile
from core.models.billable import BaseBillable
from core.mixins.billable import RecurMixin
from django.db.models import Model, TextField, CharField, ManyToManyField, ForeignKey, CASCADE, PositiveIntegerField, OneToOneField, BooleanField, SET_NULL
from importlib import import_module
from logging import getLogger
logger = getLogger(__name__)
class Service(Model):
from core.models.products import ProductGroup
name = CharField(max_length=255)
description = TextField(null=True, blank=True)
service_type = CharField(max_length=255, null=True, blank=True)
product_groups = ManyToManyField(ProductGroup)
@property
def handler(self):
if self.service_type:
try:
service_type = import_module(self.service_type)
return service_type.ServiceRouter(self)
except Exception as e:
logger.error(f"Could not load product handler {self.service_type} 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(RecurMixin, BaseBillable):
plan = ForeignKey(ServicePlan, on_delete=CASCADE)