django-orchestra/orchestra/contrib/services/models.py

267 lines
11 KiB
Python
Raw Permalink Normal View History

import calendar
2014-09-22 15:59:53 +00:00
import decimal
from orchestra.contrib.services import helpers
2014-09-17 10:32:29 +00:00
2014-10-17 10:04:47 +00:00
from django.contrib.contenttypes.models import ContentType
2014-09-17 10:32:29 +00:00
from django.db import models
2015-05-01 17:23:22 +00:00
from django.apps import apps
2014-09-17 10:32:29 +00:00
from django.utils.functional import cached_property
2014-10-07 13:08:59 +00:00
from django.utils.module_loading import autodiscover_modules
from django.utils.translation import gettext_lazy as _
2014-09-17 10:32:29 +00:00
2014-11-18 13:59:21 +00:00
from orchestra.core import caches, validators
from orchestra.utils.python import import_class
2014-09-17 10:32:29 +00:00
2014-11-18 13:59:21 +00:00
from . import settings
2014-09-17 10:32:29 +00:00
from .handlers import ServiceHandler
from .helpers import get_rate_methods_help_text
2014-09-17 10:32:29 +00:00
2014-10-07 13:08:59 +00:00
autodiscover_modules('handlers')
2014-09-17 10:32:29 +00:00
rate_class = import_class(settings.SERVICES_RATE_CLASS)
2014-09-17 10:32:29 +00:00
class ServiceQuerySet(models.QuerySet):
def filter_by_instance(self, instance):
cache = caches.get_request_cache()
ct = ContentType.objects.get_for_model(instance)
key = 'services.Service-%i' % ct.pk
services = cache.get(key)
if services is None:
services = self.filter(content_type=ct, is_active=True)
cache.set(key, services)
return services
2014-09-17 10:32:29 +00:00
class Service(models.Model):
NEVER = ''
2014-09-25 16:28:47 +00:00
# DAILY = 'DAILY'
2014-09-17 10:32:29 +00:00
MONTHLY = 'MONTHLY'
ANUAL = 'ANUAL'
2014-10-24 10:16:46 +00:00
ONE_DAY = 'ONE_DAY'
TWO_DAYS = 'TWO_DAYS'
2014-09-17 10:32:29 +00:00
TEN_DAYS = 'TEN_DAYS'
ONE_MONTH = 'ONE_MONTH'
ALWAYS = 'ALWAYS'
ON_REGISTER = 'ON_REGISTER'
FIXED_DATE = 'ON_FIXED_DATE'
BILLING_PERIOD = 'BILLING_PERIOD'
REGISTER_OR_RENEW = 'REGISTER_OR_RENEW'
CONCURRENT = 'CONCURRENT'
NOTHING = 'NOTHING'
DISCOUNT = 'DISCOUNT'
2014-09-23 16:23:36 +00:00
COMPENSATE = 'COMPENSATE'
2014-09-25 16:28:47 +00:00
REFUND = 'REFUND'
2014-09-17 10:32:29 +00:00
PREPAY = 'PREPAY'
POSTPAY = 'POSTPAY'
2016-04-06 19:00:16 +00:00
_ignore_types = ' and '.join(
', '.join(settings.SERVICES_IGNORE_ACCOUNT_TYPE).rsplit(', ', 1)).lower()
2014-09-17 10:32:29 +00:00
description = models.CharField(_("description"), max_length=256, unique=True)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE,
verbose_name=_("content type"),
2015-04-05 10:46:24 +00:00
help_text=_("Content type of the related service objects."))
match = models.CharField(_("match"), max_length=256, blank=True,
2015-04-05 10:46:24 +00:00
help_text=_(
"Python <a href='https://docs.python.org/2/library/functions.html#eval'>expression</a> "
"that designates wheter a <tt>content_type</tt> object is related to this service "
"or not, always evaluates <tt>True</tt> when left blank. "
"Related instance can be instantiated with <tt>instance</tt> keyword or "
"<tt>content_type.model_name</tt>.</br>"
"<tt>&nbsp;databaseuser.type == 'MYSQL'</tt><br>"
"<tt>&nbsp;miscellaneous.active and str(miscellaneous.identifier).endswith(('.org', '.net', '.com'))</tt><br>"
"<tt>&nbsp;contractedplan.plan.name == 'association_fee''</tt><br>"
"<tt>&nbsp;instance.active</tt>"))
2016-04-06 19:00:16 +00:00
periodic_update = models.BooleanField(_("periodic update"), default=False,
help_text=_("Whether a periodic update of this service orders should be performed or not. "
"Needed for <tt>match</tt> definitions that depend on complex model interactions, "
"where <tt>content type</tt> model save and delete operations are not enought."))
2014-09-17 10:32:29 +00:00
handler_type = models.CharField(_("handler"), max_length=256, blank=True,
help_text=_("Handler used for processing this Service. A handler enables customized "
"behaviour far beyond what options here allow to."),
2015-04-05 10:46:24 +00:00
choices=ServiceHandler.get_choices())
2014-09-30 10:20:11 +00:00
is_active = models.BooleanField(_("active"), default=True)
2015-04-02 16:14:55 +00:00
ignore_superusers = models.BooleanField(_("ignore %s") % _ignore_types, default=True,
2015-04-05 10:46:24 +00:00
help_text=_("Designates whether %s orders are marked as ignored by default or not.") % _ignore_types)
2014-09-17 10:32:29 +00:00
# Billing
billing_period = models.CharField(_("billing period"), max_length=16,
2015-04-05 10:46:24 +00:00
help_text=_("Renewal period for recurring invoicing."),
choices=(
(NEVER, _("One time service")),
(MONTHLY, _("Monthly billing")),
(ANUAL, _("Anual billing")),
),
default=ANUAL, blank=True)
2014-09-17 10:32:29 +00:00
billing_point = models.CharField(_("billing point"), max_length=16,
help_text=_("Reference point for calculating the renewal date on recurring invoices"),
2015-04-05 10:46:24 +00:00
choices=(
(ON_REGISTER, _("Registration date")),
(FIXED_DATE, _("Every %(month)s") % {
'month': calendar.month_name[settings.SERVICES_SERVICE_ANUAL_BILLING_MONTH]
}),
2015-04-05 10:46:24 +00:00
),
default=FIXED_DATE)
2014-09-30 10:20:11 +00:00
is_fee = models.BooleanField(_("fee"), default=False,
help_text=_("Designates whether this service should be billed as membership fee or not"))
order_description = models.CharField(_("Order description"), max_length=256, blank=True,
2015-04-05 10:46:24 +00:00
help_text=_(
"Python <a href='https://docs.python.org/2/library/functions.html#eval'>expression</a> "
"used for generating the description for the bill lines of this services.<br>"
"Defaults to <tt>'%s: %s' % (gettext(handler.description), instance)</tt>"
2015-04-05 10:46:24 +00:00
))
2014-10-24 10:16:46 +00:00
ignore_period = models.CharField(_("ignore period"), max_length=16, blank=True,
2015-04-05 10:46:24 +00:00
help_text=_("Period in which orders will be ignored if cancelled. "
"Useful for designating <i>trial periods</i>"),
choices=(
(NEVER, _("Never")),
(ONE_DAY, _("One day")),
(TWO_DAYS, _("Two days")),
(TEN_DAYS, _("Ten days")),
(ONE_MONTH, _("One month")),
),
default=settings.SERVICES_DEFAULT_IGNORE_PERIOD)
2014-09-17 10:32:29 +00:00
# Pricing
metric = models.CharField(_("metric"), max_length=256, blank=True,
2015-04-05 10:46:24 +00:00
help_text=_(
"Python <a href='https://docs.python.org/2/library/functions.html#eval'>expression</a> "
"used for obtinging the <i>metric value</i> for the pricing rate computation. "
"Number of orders is used when left blank. Related instance can be instantiated "
"with <tt>instance</tt> keyword or <tt>content_type.model_name</tt>.<br>"
"<tt>&nbsp;max((mailbox.resources.disk.allocated or 0) -1, 0)</tt><br>"
"<tt>&nbsp;miscellaneous.amount</tt><br>"
"<tt>&nbsp;max((account.resources.traffic.used or 0) -"
" getattr(account.miscellaneous.filter(is_active=True,"
" service__name='traffic-prepay').last(), 'amount', 0), 0)</tt>"))
2014-09-17 10:32:29 +00:00
nominal_price = models.DecimalField(_("nominal price"), max_digits=12,
2015-04-05 10:46:24 +00:00
decimal_places=2)
2014-09-17 10:32:29 +00:00
tax = models.PositiveIntegerField(_("tax"), choices=settings.SERVICES_SERVICE_TAXES,
2015-04-05 10:46:24 +00:00
default=settings.SERVICES_SERVICE_DEFAULT_TAX)
2015-03-27 19:50:54 +00:00
pricing_period = models.CharField(_("pricing period"), max_length=16, blank=True,
2015-04-05 10:46:24 +00:00
help_text=_("Time period that is used for computing the rate metric."),
choices=(
(NEVER, _("Current value")),
(BILLING_PERIOD, _("Same as billing period")),
(MONTHLY, _("Monthly data")),
(ANUAL, _("Anual data")),
),
default=BILLING_PERIOD)
rate_algorithm = models.CharField(
_("rate algorithm"), max_length=64,
2015-05-09 15:37:35 +00:00
choices=rate_class.get_choices(),
default=rate_class.get_default(),
help_text=get_rate_methods_help_text(rate_class),
)
2014-09-17 10:32:29 +00:00
on_cancel = models.CharField(_("on cancel"), max_length=16,
2015-04-05 10:46:24 +00:00
help_text=_("Defines the cancellation behaviour of this service."),
choices=(
(NOTHING, _("Nothing")),
(DISCOUNT, _("Discount")),
(COMPENSATE, _("Compensat")),
(REFUND, _("Refund")),
),
default=DISCOUNT)
2014-09-17 10:32:29 +00:00
payment_style = models.CharField(_("payment style"), max_length=16,
2015-04-05 10:46:24 +00:00
help_text=_("Designates whether this service should be paid after "
"consumtion (postpay/on demand) or prepaid."),
choices=(
(PREPAY, _("Prepay")),
(POSTPAY, _("Postpay (on demand)")),
),
default=PREPAY)
objects = ServiceQuerySet.as_manager()
2015-04-02 16:14:55 +00:00
def __str__(self):
2014-09-17 10:32:29 +00:00
return self.description
2014-09-17 10:32:29 +00:00
@cached_property
def handler(self):
""" Accessor of this service handler instance """
if self.handler_type:
return ServiceHandler.get(self.handler_type)(self)
2014-09-17 10:32:29 +00:00
return ServiceHandler(self)
2014-09-17 10:32:29 +00:00
def clean(self):
self.description = self.description.strip()
if hasattr(self, 'content_type'):
validators.all_valid({
'content_type': (self.handler.validate_content_type, self),
'match': (self.handler.validate_match, self),
'metric': (self.handler.validate_metric, self),
2015-04-20 14:23:10 +00:00
'order_description': (self.handler.validate_order_description, self),
})
2014-09-17 10:32:29 +00:00
def get_pricing_period(self):
if self.pricing_period == self.BILLING_PERIOD:
return self.billing_period
return self.pricing_period
2014-09-17 10:32:29 +00:00
def get_price(self, account, metric, rates=None, position=None):
"""
if position is provided an specific price for that position is returned,
accumulated price is returned otherwise
"""
if rates is None:
rates = self.get_rates(account)
2014-09-23 16:23:36 +00:00
if rates:
rates = self.rate_method(rates, metric)
2014-09-17 10:32:29 +00:00
if not rates:
rates = [{
'quantity': metric,
'price': self.nominal_price,
}]
counter = 0
if position is None:
ant_counter = 0
accumulated = 0
for rate in rates:
counter += rate['quantity']
if counter >= metric:
counter = metric
accumulated += (counter - ant_counter) * rate['price']
accumulated = round(accumulated, 2)
2015-04-01 15:49:21 +00:00
return decimal.Decimal(str(accumulated))
2014-09-17 10:32:29 +00:00
ant_counter = counter
accumulated += rate['price'] * rate['quantity']
raise RuntimeError("Rating algorithm bad result")
2014-09-17 10:32:29 +00:00
else:
2015-04-14 14:29:22 +00:00
if metric < position:
raise ValueError("Metric can not be less than the position.")
2014-09-17 10:32:29 +00:00
for rate in rates:
counter += rate['quantity']
if counter >= position:
price = round(rate['price'], 2)
2015-04-01 15:49:21 +00:00
return decimal.Decimal(str(rate['price']))
raise RuntimeError("Rating algorithm bad result")
2014-09-17 10:32:29 +00:00
def get_rates(self, account, cache=True):
# rates are cached per account
if not cache:
return self.rates.by_account(account)
if not hasattr(self, '__cached_rates'):
self.__cached_rates = {}
2015-05-12 14:04:20 +00:00
try:
return self.__cached_rates[account.id]
except KeyError:
rates = self.rates.by_account(account)
self.__cached_rates[account.id] = rates
return rates
2014-09-17 10:32:29 +00:00
@property
def rate_method(self):
return rate_class.get_methods()[self.rate_algorithm]
2014-10-20 19:22:18 +00:00
def update_orders(self, commit=True):
2015-05-01 17:23:22 +00:00
order_model = apps.get_model(settings.SERVICES_ORDER_MODEL)
2016-04-06 19:00:16 +00:00
manager = order_model.objects
2014-09-26 10:38:50 +00:00
related_model = self.content_type.model_class()
2014-10-20 19:22:18 +00:00
updates = []
2015-04-20 14:23:10 +00:00
queryset = related_model.objects.all()
if related_model._meta.model_name != 'account':
queryset = queryset.select_related('account').all()
for instance in queryset:
2016-04-06 19:00:16 +00:00
updates += manager.update_by_instance(instance, service=self, commit=commit)
2014-10-20 19:22:18 +00:00
return updates