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

279 lines
11 KiB
Python
Raw Normal View History

2014-09-25 16:28:47 +00:00
import datetime
2014-09-22 15:59:53 +00:00
import decimal
2014-09-19 14:47:25 +00:00
import logging
2014-09-10 16:53:09 +00:00
2014-05-27 15:55:09 +00:00
from django.db import models
2015-05-26 12:59:16 +00:00
from django.db.models import F, Q, Sum
2015-05-01 17:23:22 +00:00
from django.apps import apps
from django.contrib.contenttypes.fields import GenericForeignKey
2014-05-08 16:59:35 +00:00
from django.contrib.contenttypes.models import ContentType
2014-07-18 15:32:27 +00:00
from django.utils import timezone
2014-05-27 15:55:09 +00:00
from django.utils.translation import ugettext_lazy as _
2014-05-08 16:59:35 +00:00
2014-07-25 13:27:31 +00:00
from orchestra.models import queryset
from orchestra.utils.python import import_class
2014-07-21 12:20:04 +00:00
2015-05-04 14:19:58 +00:00
from . import settings
2014-05-08 16:59:35 +00:00
2014-09-19 14:47:25 +00:00
logger = logging.getLogger(__name__)
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)
2015-04-02 16:14:55 +00:00
for account, services in qs.group_by('account', 'service').items():
2014-07-25 13:27:31 +00:00
bill_lines = []
2015-04-02 16:14:55 +00:00
for service, orders in services.items():
2014-09-26 15:05:20 +00:00
for order in orders:
# Saved for undoing support
order.old_billed_on = order.billed_on
order.old_billed_until = order.billed_until
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)
2015-03-29 16:10:07 +00:00
# TODO make this consistent always returning the same fucking types
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)]
2015-04-20 14:23:10 +00:00
# TODO remove if commit and always return unique elemenets (set()) when the other todo is fixed
if commit:
return list(set(bills))
2014-09-03 13:56:02 +00:00
return bills
2014-09-14 19:36:27 +00:00
2014-09-19 14:47:25 +00:00
def givers(self, ini, end):
return self.cancelled_and_billed().filter(billed_until__gt=ini, registered_on__lt=end)
2014-07-25 13:27:31 +00:00
2014-09-19 14:47:25 +00:00
def cancelled_and_billed(self, exclude=False):
qs = dict(cancelled_on__isnull=False, billed_until__isnull=False,
cancelled_on__lte=F('billed_until'))
if exclude:
return self.exclude(**qs)
return self.filter(**qs)
2014-09-30 14:46:29 +00:00
def get_related(self, **options):
2014-10-20 19:22:18 +00:00
""" returns related orders that could have a pricing effect """
2015-05-01 17:23:22 +00:00
Service = apps.get_model(settings.ORDERS_SERVICE_MODEL)
2014-09-30 14:46:29 +00:00
conflictive = self.filter(service__metric='')
2015-05-26 12:59:16 +00:00
conflictive = conflictive.exclude(service__billing_period=Service.NEVER)
# Exclude rates null or all rates with quantity 0
conflictive = conflictive.annotate(quantity_sum=Sum('service__rates__quantity')).exclude(quantity_sum=0)
2015-05-15 14:19:24 +00:00
conflictive = conflictive.select_related('service').distinct().group_by('account_id', 'service')
2014-09-30 14:46:29 +00:00
qs = Q()
2015-04-02 16:14:55 +00:00
for account_id, services in conflictive.items():
for service, orders in services.items():
2014-10-21 15:29:36 +00:00
ini = datetime.date.max
2014-09-30 14:46:29 +00:00
end = datetime.date.min
bp = None
for order in orders:
bp = service.handler.get_billing_point(order, **options)
end = max(end, bp)
2014-10-21 15:29:36 +00:00
ini = min(ini, order.billed_until or order.registered_on)
qs |= Q(
Q(service=service, account=account_id, registered_on__lt=end) & Q(
Q(billed_until__isnull=True) | Q(billed_until__lt=end)
) & Q(
Q(cancelled_on__isnull=True) | Q(cancelled_on__gt=ini)
)
2014-09-30 14:46:29 +00:00
)
2014-10-21 15:29:36 +00:00
if not qs:
return self.model.objects.none()
2014-09-30 14:46:29 +00:00
ids = self.values_list('id', flat=True)
2014-10-21 15:29:36 +00:00
return self.model.objects.filter(qs).exclude(id__in=ids)
2014-09-30 14:46:29 +00:00
2014-09-19 14:47:25 +00:00
def pricing_orders(self, ini, end):
2014-09-14 19:36:27 +00:00
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 """
2014-09-26 15:05:20 +00:00
return self.filter(cancelled_on__lte=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"),
2015-04-05 10:46:24 +00:00
related_name='orders')
2014-05-08 16:59:35 +00:00
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField(null=True)
2014-09-24 20:09:41 +00:00
service = models.ForeignKey(settings.ORDERS_SERVICE_MODEL, verbose_name=_("service"),
2015-04-05 10:46:24 +00:00
related_name='orders')
2015-04-08 14:41:09 +00:00
registered_on = models.DateField(_("registered"), default=timezone.now)
2014-09-17 10:32:29 +00:00
cancelled_on = models.DateField(_("cancelled"), null=True, blank=True)
2014-10-20 19:22:18 +00:00
billed_on = models.DateField(_("billed"), null=True, blank=True)
2014-09-03 13:56:02 +00:00
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
2015-05-01 17:23:22 +00:00
content_object = GenericForeignKey()
2014-07-18 15:32:27 +00:00
objects = OrderQuerySet.as_manager()
2014-07-25 13:27:31 +00:00
2014-09-24 20:09:41 +00:00
class Meta:
get_latest_by = 'id'
2015-04-02 16:14:55 +00:00
def __str__(self):
return str(self.service)
2014-07-17 16:09:24 +00:00
@classmethod
2014-10-20 19:22:18 +00:00
def update_orders(cls, instance, service=None, commit=True):
updates = []
2014-09-26 10:38:50 +00:00
if service is None:
2015-05-01 17:23:22 +00:00
Service = apps.get_model(settings.ORDERS_SERVICE_MODEL)
2014-09-26 10:38:50 +00:00
services = Service.get_services(instance)
else:
services = [service]
for service in services:
2015-04-08 14:41:09 +00:00
orders = Order.objects.by_object(instance, service=service).select_related('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-10-24 10:16:46 +00:00
ignore = service.handler.get_ignore(instance)
2014-10-20 19:22:18 +00:00
order = cls(content_object=instance, service=service,
2014-10-16 17:14:21 +00:00
account_id=account_id, ignore=ignore)
2014-10-20 19:22:18 +00:00
if commit:
order.save()
updates.append((order, 'created'))
2014-09-19 14:47:25 +00:00
logger.info("CREATED new order id: {id}".format(id=order.id))
2014-07-18 15:32:27 +00:00
else:
2015-04-08 14:41:09 +00:00
if len(orders) > 1:
raise ValueError("A single active order was expected.")
order = orders[0]
2014-10-20 19:22:18 +00:00
updates.append((order, 'updated'))
if commit:
order.update()
2014-07-18 15:32:27 +00:00
elif orders:
2015-04-08 14:41:09 +00:00
if len(orders) > 1:
raise ValueError("A single active order was expected.")
order = orders[0]
2014-10-24 10:16:46 +00:00
order.cancel(commit=commit)
2015-04-04 17:44:07 +00:00
logger.info("CANCELLED order id: {id}".format(id=order.id))
2014-10-20 19:22:18 +00:00
updates.append((order, 'cancelled'))
return updates
2014-07-18 15:32:27 +00:00
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-09-19 14:47:25 +00:00
def update(self):
instance = self.content_object
2015-04-20 14:23:10 +00:00
if instance is None:
return
2014-09-19 14:47:25 +00:00
handler = self.service.handler
2014-09-22 15:59:53 +00:00
metric = ''
2014-09-19 14:47:25 +00:00
if handler.metric:
metric = handler.get_metric(instance)
if metric is not None:
MetricStorage.store(self, metric)
2014-09-22 15:59:53 +00:00
metric = ', metric:{}'.format(metric)
2014-10-23 15:38:46 +00:00
description = handler.get_order_description(instance)
2015-04-02 16:14:55 +00:00
logger.info("UPDATED order id:{id}, description:{description}{metric}".format(
2015-04-08 14:41:09 +00:00
id=self.id, description=description, metric=metric).encode('ascii', 'replace')
)
2014-09-19 14:47:25 +00:00
if self.description != description:
self.description = description
self.save(update_fields=['description'])
2014-09-19 14:47:25 +00:00
2014-10-24 10:16:46 +00:00
def cancel(self, commit=True):
2014-07-18 15:32:27 +00:00
self.cancelled_on = timezone.now()
2014-10-24 10:16:46 +00:00
self.ignore = self.service.handler.get_order_ignore(self)
if commit:
self.save(update_fields=['cancelled_on', 'ignore'])
logger.info("CANCELLED order id: {id}".format(id=self.id))
2014-09-03 13:56:02 +00:00
2014-10-16 17:14:21 +00:00
def mark_as_ignored(self):
self.ignore = True
self.save(update_fields=['ignore'])
def mark_as_not_ignored(self):
self.ignore = False
self.save(update_fields=['ignore'])
2014-09-23 16:23:36 +00:00
def get_metric(self, *args, **kwargs):
if kwargs.pop('changes', False):
ini, end = args
2014-09-23 14:01:58 +00:00
result = []
prev = None
2014-09-23 16:23:36 +00:00
for metric in self.metrics.filter(created_on__lt=end).order_by('id'):
created = metric.created_on
2014-09-23 14:01:58 +00:00
if created > ini:
2014-09-23 16:23:36 +00:00
cini = prev.created_on
2014-09-23 14:01:58 +00:00
if not result:
cini = ini
result.append((cini, created, prev.value))
prev = metric
if created < end:
result.append((created, end, metric.value))
return result
2014-09-23 16:23:36 +00:00
if kwargs:
raise AttributeError
if len(args) == 2:
ini, end = args
2014-09-23 14:01:58 +00:00
metrics = self.metrics.filter(updated_on__lt=end, updated_on__gte=ini)
2014-09-23 16:23:36 +00:00
elif len(args) == 1:
date = args[0]
2014-09-25 16:28:47 +00:00
date = datetime.date(year=date.year, month=date.month, day=date.day)
date += datetime.timedelta(days=1)
metrics = self.metrics.filter(updated_on__lt=date)
2014-09-23 16:23:36 +00:00
elif not args:
return self.metrics.latest('updated_on').value
else:
raise AttributeError
try:
2014-09-23 14:01:58 +00:00
return metrics.latest('updated_on').value
except MetricStorage.DoesNotExist:
return decimal.Decimal(0)
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):
""" Stores metric state for future billing """
2014-09-22 15:59:53 +00:00
order = models.ForeignKey(Order, verbose_name=_("order"), related_name='metrics')
value = models.DecimalField(_("value"), max_digits=16, decimal_places=2)
2014-09-23 16:23:36 +00:00
created_on = models.DateField(_("created"), auto_now_add=True)
2014-09-24 20:09:41 +00:00
# default=lambda: timezone.now())
2015-04-07 15:14:49 +00:00
# TODO time field?
2014-09-22 15:59:53 +00:00
updated_on = models.DateTimeField(_("updated"))
2014-07-18 16:02:05 +00:00
class Meta:
2014-09-23 16:23:36 +00:00
get_latest_by = 'id'
2014-05-27 15:55:09 +00:00
2015-04-02 16:14:55 +00:00
def __str__(self):
return str(self.order)
2014-07-18 16:02:05 +00:00
@classmethod
def store(cls, order, value):
2014-09-22 15:59:53 +00:00
now = timezone.now()
2014-07-18 16:02:05 +00:00
try:
last = cls.objects.filter(order=order).latest()
2014-07-18 16:02:05 +00:00
except cls.DoesNotExist:
2014-09-22 15:59:53 +00:00
cls.objects.create(order=order, value=value, updated_on=now)
2014-07-18 16:02:05 +00:00
else:
2015-05-15 14:19:24 +00:00
# Metric storage has per-day granularity (last value of the day is what counts)
if last.created_on == now.date():
last.value = value
last.updated_on = now
2015-05-15 14:19:24 +00:00
last.save()
else:
error = decimal.Decimal(str(settings.ORDERS_METRIC_ERROR))
if value > last.value+error or value < last.value-error:
cls.objects.create(order=order, value=value, updated_on=now)
else:
last.updated_on = now
last.save(update_fields=['updated_on'])