django-orchestra-test/orchestra/apps/orders/models.py

473 lines
18 KiB
Python
Raw Normal View History

2014-09-10 16:53:09 +00:00
import sys
2014-05-27 15:55:09 +00:00
from django.db import models
2014-09-10 16:53:09 +00:00
from django.db.migrations.recorder import MigrationRecorder
from django.db.models import F, Q
2014-07-18 15:32:27 +00:00
from django.db.models.signals import pre_delete, post_delete, post_save
from django.dispatch import receiver
from django.contrib.admin.models import LogEntry
2014-05-08 16:59:35 +00:00
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType
2014-07-21 12:20:04 +00:00
from django.core.validators import ValidationError
2014-07-18 15:32:27 +00:00
from django.utils import timezone
2014-07-21 12:20:04 +00:00
from django.utils.functional import cached_property
2014-05-27 15:55:09 +00:00
from django.utils.translation import ugettext_lazy as _
2014-05-08 16:59:35 +00:00
from orchestra.core import caches, services, accounts
2014-07-25 13:27:31 +00:00
from orchestra.models import queryset
2014-07-21 12:20:04 +00:00
from orchestra.utils.apps import autodiscover
from orchestra.utils.python import import_class
2014-07-21 12:20:04 +00:00
2014-09-15 12:15:32 +00:00
from . import helpers, settings, rating
2014-07-21 12:20:04 +00:00
from .handlers import ServiceHandler
2014-05-08 16:59:35 +00:00
2014-09-08 14:23:06 +00:00
class Plan(models.Model):
2014-09-15 12:15:32 +00:00
name = models.CharField(_("plan"), max_length=128)
is_default = models.BooleanField(_("is default"), default=False)
is_combinable = models.BooleanField(_("is combinable"), default=True)
allow_multiple = models.BooleanField(_("allow multipls"), default=False)
def __unicode__(self):
return self.name
class ContractedPlan(models.Model):
plan = models.ForeignKey(Plan, verbose_name=_("plan"), related_name='contracts')
2014-09-08 14:23:06 +00:00
account = models.ForeignKey('accounts.Account', verbose_name=_("account"),
related_name='plans')
def __unicode__(self):
2014-09-15 12:15:32 +00:00
return str(self.plan)
2014-09-08 14:23:06 +00:00
class RateQuerySet(models.QuerySet):
group_by = queryset.group_by
def by_account(self, account):
# Default allways selected
2014-09-15 15:36:24 +00:00
return self.filter(
Q(plan__is_default=True) |
Q(plan__contracts__account=account)
).order_by('plan', 'quantity').select_related('plan')
2014-09-08 14:23:06 +00:00
class Rate(models.Model):
service = models.ForeignKey('orders.Service', verbose_name=_("service"),
related_name='rates')
2014-09-15 12:15:32 +00:00
plan = models.ForeignKey(Plan, verbose_name=_("plan"), related_name='rates')
2014-09-08 14:23:06 +00:00
quantity = models.PositiveIntegerField(_("quantity"), null=True, blank=True)
2014-09-10 16:53:09 +00:00
price = models.DecimalField(_("price"), max_digits=12, decimal_places=2)
2014-09-08 14:23:06 +00:00
objects = RateQuerySet.as_manager()
class Meta:
unique_together = ('service', 'plan', 'quantity')
def __unicode__(self):
2014-09-10 16:53:09 +00:00
return "{}-{}".format(str(self.price), self.quantity)
2014-09-08 14:23:06 +00:00
2014-07-21 12:20:04 +00:00
autodiscover('handlers')
2014-07-16 15:20:16 +00:00
class Service(models.Model):
NEVER = ''
2014-07-16 15:20:16 +00:00
MONTHLY = 'MONTHLY'
ANUAL = 'ANUAL'
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'
REFOUND = 'REFOUND'
COMPENSATE = 'COMPENSATE'
2014-07-16 15:20:16 +00:00
PREPAY = 'PREPAY'
POSTPAY = 'POSTPAY'
2014-09-15 15:36:24 +00:00
STEP_PRICE = 'STEP_PRICE'
2014-07-16 15:20:16 +00:00
MATCH_PRICE = 'MATCH_PRICE'
2014-09-10 16:53:09 +00:00
RATE_METHODS = {
2014-09-15 15:36:24 +00:00
STEP_PRICE: rating.step_price,
2014-09-15 12:15:32 +00:00
MATCH_PRICE: rating.match_price,
2014-09-08 14:23:06 +00:00
}
2014-07-16 15:20:16 +00:00
description = models.CharField(_("description"), max_length=256, unique=True)
2014-07-21 12:20:04 +00:00
content_type = models.ForeignKey(ContentType, verbose_name=_("content type"))
match = models.CharField(_("match"), max_length=256, blank=True)
2014-07-21 15:43:36 +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."),
2014-07-21 12:20:04 +00:00
choices=ServiceHandler.get_plugin_choices())
2014-07-16 15:20:16 +00:00
is_active = models.BooleanField(_("is active"), default=True)
# Billing
billing_period = models.CharField(_("billing period"), max_length=16,
help_text=_("Renewal period for recurring invoicing"),
choices=(
(NEVER, _("One time service")),
(MONTHLY, _("Monthly billing")),
(ANUAL, _("Anual billing")),
),
default=ANUAL, blank=True)
2014-07-16 15:20:16 +00:00
billing_point = models.CharField(_("billing point"), max_length=16,
help_text=_("Reference point for calculating the renewal date "
"on recurring invoices"),
choices=(
(ON_REGISTER, _("Registration date")),
(FIXED_DATE, _("Fixed billing date")),
),
default=FIXED_DATE)
2014-09-14 22:00:00 +00:00
# delayed_billing = models.CharField(_("delayed billing"), max_length=16,
# help_text=_("Period in which this service will be ignored for billing"),
# choices=(
# (NEVER, _("No delay (inmediate billing)")),
# (TEN_DAYS, _("Ten days")),
# (ONE_MONTH, _("One month")),
# ),
# default=ONE_MONTH, blank=True)
2014-07-16 15:20:16 +00:00
is_fee = models.BooleanField(_("is fee"), default=False,
help_text=_("Designates whether this service should be billed as "
" membership fee or not"))
# Pricing
metric = models.CharField(_("metric"), max_length=256, blank=True,
help_text=_("Metric used to compute the pricing rate. "
"Number of orders is used when left blank."))
2014-09-08 14:23:06 +00:00
nominal_price = models.DecimalField(_("nominal price"), max_digits=12,
decimal_places=2)
2014-09-03 13:56:02 +00:00
tax = models.PositiveIntegerField(_("tax"), choices=settings.ORDERS_SERVICE_TAXES,
2014-07-16 15:20:16 +00:00
default=settings.ORDERS_SERVICE_DEFAUL_TAX)
pricing_period = models.CharField(_("pricing period"), max_length=16,
help_text=_("Period used for calculating the metric used on the "
"pricing rate"),
choices=(
(BILLING_PERIOD, _("Same as billing period")),
(MONTHLY, _("Monthly data")),
(ANUAL, _("Anual data")),
),
default=BILLING_PERIOD)
rate_algorithm = models.CharField(_("rate algorithm"), max_length=16,
help_text=_("Algorithm used to interprete the rating table"),
choices=(
2014-09-15 15:36:24 +00:00
(STEP_PRICE, _("Step price")),
2014-07-16 15:20:16 +00:00
(MATCH_PRICE, _("Match price")),
),
2014-09-15 15:36:24 +00:00
default=STEP_PRICE)
2014-09-14 22:00:00 +00:00
# orders_effect = models.CharField(_("orders effect"), max_length=16,
# help_text=_("Defines the lookup behaviour when using orders for "
# "the pricing rate computation of this service."),
# choices=(
# (REGISTER_OR_RENEW, _("Register or renew events")),
# (CONCURRENT, _("Active at every given time")),
# ),
# default=CONCURRENT)
2014-07-16 15:20:16 +00:00
on_cancel = models.CharField(_("on cancel"), max_length=16,
help_text=_("Defines the cancellation behaviour of this service"),
choices=(
(NOTHING, _("Nothing")),
(DISCOUNT, _("Discount")),
(COMPENSATE, _("Discount and compensate")),
2014-07-16 15:20:16 +00:00
),
default=DISCOUNT)
payment_style = models.CharField(_("payment style"), max_length=16,
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)
2014-09-14 22:00:00 +00:00
# trial_period = models.CharField(_("trial period"), max_length=16, blank=True,
# help_text=_("Period in which no charge will be issued"),
# choices=(
# (NEVER, _("No trial")),
# (TEN_DAYS, _("Ten days")),
# (ONE_MONTH, _("One month")),
# ),
# default=NEVER)
# refound_period = models.CharField(_("refound period"), max_length=16,
# help_text=_("Period in which automatic refound will be performed on "
# "service cancellation"),
# choices=(
# (NEVER, _("Never refound")),
# (TEN_DAYS, _("Ten days")),
# (ONE_MONTH, _("One month")),
# (ALWAYS, _("Always refound")),
# ),
# default=NEVER, blank=True)
2014-07-16 15:20:16 +00:00
def __unicode__(self):
return self.description
2014-07-18 16:02:05 +00:00
2014-07-17 16:09:24 +00:00
@classmethod
2014-07-21 12:20:04 +00:00
def get_services(cls, instance):
cache = caches.get_request_cache()
2014-07-18 16:02:05 +00:00
ct = ContentType.objects.get_for_model(instance)
2014-07-21 12:20:04 +00:00
services = cache.get(ct)
if services is None:
services = cls.objects.filter(content_type=ct, is_active=True)
cache.set(ct, services)
return services
2014-07-17 16:09:24 +00:00
2014-07-21 15:43:36 +00:00
# FIXME some times caching is nasty, do we really have to? make get_plugin more efficient?
# @property
@cached_property
2014-07-21 15:43:36 +00:00
def handler(self):
""" Accessor of this service handler instance """
if self.handler_type:
return ServiceHandler.get_plugin(self.handler_type)(self)
2014-07-21 12:20:04 +00:00
return ServiceHandler(self)
2014-07-18 16:02:05 +00:00
2014-07-21 12:20:04 +00:00
def clean(self):
2014-07-21 15:43:36 +00:00
content_type = self.handler.get_content_type()
2014-07-21 12:20:04 +00:00
if self.content_type != content_type:
msg =_("Content type must be equal to '%s'.") % str(content_type)
2014-07-21 12:20:04 +00:00
raise ValidationError(msg)
if not self.match:
msg =_("Match should be provided")
raise ValidationError(msg)
try:
obj = content_type.model_class().objects.all()[0]
except IndexError:
pass
else:
2014-07-22 21:47:01 +00:00
attr = None
try:
bool(self.handler.matches(obj))
except Exception as exception:
attr = "Matches"
try:
metric = self.handler.get_metric(obj)
if metric is not None:
int(metric)
except Exception as exception:
attr = "Get metric"
if attr is not None:
name = type(exception).__name__
message = exception.message
msg = "{0} {1}: {2}".format(attr, name, message)
raise ValidationError(msg)
2014-09-03 13:56:02 +00:00
def get_pricing_period(self):
if self.pricing_period == self.BILLING_PERIOD:
return self.billing_period
return self.pricing_period
def get_price(self, account, metric, rates=None, position=None):
2014-09-08 14:23:06 +00:00
"""
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-15 15:36:24 +00:00
if not rates:
rates = [{
'quantity': metric,
'price': self.nominal_price,
}]
else:
rates = self.rate_method(rates, metric)
2014-09-08 14:23:06 +00:00
counter = 0
if position is None:
ant_counter = 0
accumulated = 0
2014-09-10 16:53:09 +00:00
for rate in rates:
counter += rate['quantity']
2014-09-08 14:23:06 +00:00
if counter >= metric:
counter = metric
accumulated += (counter - ant_counter) * rate['price']
2014-09-10 16:53:09 +00:00
return float(accumulated)
2014-09-08 14:23:06 +00:00
ant_counter = counter
2014-09-10 16:53:09 +00:00
accumulated += rate['price'] * rate['quantity']
2014-09-08 14:23:06 +00:00
else:
2014-09-10 16:53:09 +00:00
for rate in rates:
counter += rate['quantity']
2014-09-08 14:23:06 +00:00
if counter >= position:
2014-09-10 16:53:09 +00:00
return float(rate['price'])
def get_rates(self, account, cache=True):
2014-09-15 15:36:24 +00:00
# rates are cached per account
if not cache:
return self.rates.by_account(account)
2014-09-10 16:53:09 +00:00
if not hasattr(self, '__cached_rates'):
self.__cached_rates = {}
2014-09-15 15:36:24 +00:00
rates = self.__cached_rates.get(account.id, self.rates.by_account(account))
return rates
2014-09-08 14:23:06 +00:00
@property
2014-09-10 16:53:09 +00:00
def rate_method(self):
2014-09-08 14:23:06 +00:00
return self.RATE_METHODS[self.rate_algorithm]
2014-07-17 16:09:24 +00:00
2014-07-16 15:20:16 +00:00
2014-07-18 15:32:27 +00:00
class OrderQuerySet(models.QuerySet):
2014-07-25 13:27:31 +00:00
group_by = queryset.group_by
def bill(self, **options):
2014-09-03 13:56:02 +00:00
bills = []
bill_backend = Order.get_bill_backend()
qs = self.select_related('account', 'service')
2014-09-10 16:53:09 +00:00
commit = options.get('commit', True)
for account, services in qs.group_by('account', 'service').iteritems():
2014-07-25 13:27:31 +00:00
bill_lines = []
for service, orders in services.iteritems():
2014-09-15 12:15:32 +00:00
lines = service.handler.generate_bill_lines(orders, account, **options)
2014-07-25 13:27:31 +00:00
bill_lines.extend(lines)
2014-09-10 16:53:09 +00:00
if commit:
2014-09-11 14:00:20 +00:00
bills += bill_backend.create_bills(account, bill_lines, **options)
2014-09-10 16:53:09 +00:00
else:
bills += [(account, bill_lines)]
2014-09-03 13:56:02 +00:00
return bills
2014-09-14 19:36:27 +00:00
def filter_givers(self, ini, end):
return self.filter(
2014-09-14 09:52:45 +00:00
cancelled_on__isnull=False, billed_until__isnull=False,
2014-09-14 19:36:27 +00:00
cancelled_on__lte=F('billed_until'), billed_until__gt=ini,
registered_on__lt=end)
2014-07-25 13:27:31 +00:00
2014-09-14 19:36:27 +00:00
def filter_pricing_orders(self, ini, end):
return self.filter(billed_until__isnull=False, billed_until__gt=ini,
registered_on__lt=end)
2014-07-25 13:27:31 +00:00
2014-07-21 15:43:36 +00:00
def by_object(self, obj, **kwargs):
2014-07-18 15:32:27 +00:00
ct = ContentType.objects.get_for_model(obj)
2014-07-21 15:43:36 +00:00
return self.filter(object_id=obj.pk, content_type=ct, **kwargs)
2014-07-18 15:32:27 +00:00
2014-07-21 15:43:36 +00:00
def active(self, **kwargs):
2014-07-18 15:32:27 +00:00
""" return active orders """
return self.filter(
Q(cancelled_on__isnull=True) | Q(cancelled_on__gt=timezone.now())
2014-07-21 15:43:36 +00:00
).filter(**kwargs)
def inactive(self, **kwargs):
""" return inactive orders """
return self.filter(cancelled_on__lt=timezone.now(), **kwargs)
2014-07-18 15:32:27 +00:00
2014-05-08 16:59:35 +00:00
class Order(models.Model):
2014-05-27 15:55:09 +00:00
account = models.ForeignKey('accounts.Account', verbose_name=_("account"),
related_name='orders')
2014-05-08 16:59:35 +00:00
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField(null=True)
2014-07-18 16:02:05 +00:00
service = models.ForeignKey(Service, verbose_name=_("service"),
2014-07-16 15:20:16 +00:00
related_name='orders')
2014-09-14 19:36:27 +00:00
registered_on = models.DateField(_("registered on"), auto_now_add=True) # TODO datetime field?
2014-09-03 13:56:02 +00:00
cancelled_on = models.DateField(_("cancelled on"), null=True, blank=True)
billed_on = models.DateField(_("billed on"), null=True, blank=True)
billed_until = models.DateField(_("billed until"), null=True, blank=True)
2014-05-08 16:59:35 +00:00
ignore = models.BooleanField(_("ignore"), default=False)
2014-05-27 15:55:09 +00:00
description = models.TextField(_("description"), blank=True)
2014-05-08 16:59:35 +00:00
content_object = generic.GenericForeignKey()
2014-07-18 15:32:27 +00:00
objects = OrderQuerySet.as_manager()
2014-07-25 13:27:31 +00:00
2014-05-08 16:59:35 +00:00
def __unicode__(self):
2014-07-17 16:09:24 +00:00
return str(self.service)
def update(self):
instance = self.content_object
2014-07-21 15:43:36 +00:00
handler = self.service.handler
if handler.metric:
metric = handler.get_metric(instance)
if metric is not None:
MetricStorage.store(self, metric)
description = "{}: {}".format(handler.description, str(instance))
2014-07-17 16:09:24 +00:00
if self.description != description:
self.description = description
self.save()
@classmethod
2014-07-18 15:32:27 +00:00
def update_orders(cls, instance):
for service in Service.get_services(instance):
orders = Order.objects.by_object(instance, service=service).active()
2014-07-21 15:43:36 +00:00
if service.handler.matches(instance):
2014-07-18 15:32:27 +00:00
if not orders:
account_id = getattr(instance, 'account_id', instance.pk)
2014-08-29 12:45:27 +00:00
if account_id is None:
# New account workaround -> user.account_id == None
continue
2014-07-18 15:32:27 +00:00
order = cls.objects.create(content_object=instance,
service=service, account_id=account_id)
else:
order = orders.get()
order.update()
elif orders:
orders.get().cancel()
2014-09-03 13:56:02 +00:00
@classmethod
def get_bill_backend(cls):
return import_class(settings.ORDERS_BILLING_BACKEND)()
2014-09-03 13:56:02 +00:00
2014-07-18 15:32:27 +00:00
def cancel(self):
self.cancelled_on = timezone.now()
self.save()
2014-09-03 13:56:02 +00:00
def get_metric(self, ini, end):
2014-09-08 14:23:06 +00:00
return MetricStorage.get(self, ini, end)
2014-05-27 15:55:09 +00:00
2014-05-08 16:59:35 +00:00
2014-07-16 15:20:16 +00:00
class MetricStorage(models.Model):
2014-05-27 15:55:09 +00:00
order = models.ForeignKey(Order, verbose_name=_("order"))
value = models.BigIntegerField(_("value"))
2014-09-03 13:56:02 +00:00
created_on = models.DateField(_("created on"), auto_now_add=True)
updated_on = models.DateField(_("updated on"), auto_now=True)
2014-07-18 16:02:05 +00:00
class Meta:
2014-07-21 12:20:04 +00:00
get_latest_by = 'created_on'
2014-05-27 15:55:09 +00:00
def __unicode__(self):
2014-07-18 16:02:05 +00:00
return unicode(self.order)
@classmethod
def store(cls, order, value):
try:
metric = cls.objects.filter(order=order).latest()
except cls.DoesNotExist:
cls.objects.create(order=order, value=value)
else:
if metric.value != value:
cls.objects.create(order=order, value=value)
2014-07-21 12:20:04 +00:00
else:
metric.save()
@classmethod
def get(cls, order, ini, end):
2014-09-08 14:23:06 +00:00
try:
return cls.objects.filter(order=order, updated_on__lt=end,
updated_on__gte=ini).latest('updated_on').value
except cls.DoesNotExist:
return 0
2014-07-18 15:32:27 +00:00
2014-09-14 09:52:45 +00:00
# TODO If this happens to be very costly then, consider an additional
# implementation when runnning within a request/Response cycle, more efficient :)
2014-07-18 15:32:27 +00:00
@receiver(pre_delete, dispatch_uid="orders.cancel_orders")
def cancel_orders(sender, **kwargs):
2014-07-22 21:47:01 +00:00
if sender in services:
2014-07-21 15:43:36 +00:00
instance = kwargs['instance']
for order in Order.objects.by_object(instance).active():
order.cancel()
2014-07-18 15:32:27 +00:00
@receiver(post_save, dispatch_uid="orders.update_orders")
2014-07-22 21:47:01 +00:00
@receiver(post_delete, dispatch_uid="orders.update_orders_post_delete")
2014-07-18 15:32:27 +00:00
def update_orders(sender, **kwargs):
2014-09-10 16:53:09 +00:00
exclude = (
MetricStorage, LogEntry, Order, Service, ContentType, MigrationRecorder.Migration
)
if sender not in exclude:
2014-07-21 15:43:36 +00:00
instance = kwargs['instance']
if instance.pk:
# post_save
Order.update_orders(instance)
2014-07-25 13:27:31 +00:00
related = helpers.get_related_objects(instance)
2014-07-21 15:43:36 +00:00
if related:
Order.update_orders(related)
accounts.register(Order)
2014-09-16 15:01:02 +00:00
accounts.register(ContractedPlan)
services.register(ContractedPlan, menu=False)