2014-09-19 14:47:25 +00:00
|
|
|
import logging
|
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-09-17 10:32:29 +00:00
|
|
|
from django.db.models.loading import get_model
|
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-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
|
|
|
|
2014-07-28 17:28:00 +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
|
2014-09-08 10:03:42 +00:00
|
|
|
from orchestra.utils.python import import_class
|
2014-07-21 12:20:04 +00:00
|
|
|
|
2014-09-17 10:32:29 +00:00
|
|
|
from . import helpers, settings
|
2014-07-21 12:20:04 +00:00
|
|
|
from .handlers import ServiceHandler
|
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-19 14:47:25 +00:00
|
|
|
# TODO classmethod?
|
2014-09-03 13:56:02 +00:00
|
|
|
bills = []
|
|
|
|
bill_backend = Order.get_bill_backend()
|
2014-09-03 22:01:44 +00:00
|
|
|
qs = self.select_related('account', 'service')
|
2014-09-10 16:53:09 +00:00
|
|
|
commit = options.get('commit', True)
|
2014-09-16 14:35:00 +00:00
|
|
|
for account, services in qs.group_by('account', 'service').iteritems():
|
2014-07-25 13:27:31 +00:00
|
|
|
bill_lines = []
|
2014-09-16 14:42:32 +00:00
|
|
|
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
|
|
|
|
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)
|
|
|
|
|
|
|
|
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 """
|
|
|
|
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-09-17 10:32:29 +00:00
|
|
|
service = models.ForeignKey(settings.ORDERS_SERVICE_MODEL,
|
|
|
|
verbose_name=_("service"), related_name='orders')
|
|
|
|
registered_on = models.DateField(_("registered"), auto_now_add=True) # TODO datetime field?
|
|
|
|
cancelled_on = models.DateField(_("cancelled"), null=True, blank=True)
|
2014-09-03 13:56:02 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
@classmethod
|
2014-07-18 15:32:27 +00:00
|
|
|
def update_orders(cls, instance):
|
2014-09-17 10:32:29 +00:00
|
|
|
Service = get_model(*settings.ORDERS_SERVICE_MODEL.split('.'))
|
2014-07-18 15:32:27 +00:00
|
|
|
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)
|
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:
|
|
|
|
order = orders.get()
|
|
|
|
order.update()
|
|
|
|
elif orders:
|
|
|
|
orders.get().cancel()
|
|
|
|
|
2014-09-03 13:56:02 +00:00
|
|
|
@classmethod
|
|
|
|
def get_bill_backend(cls):
|
2014-09-08 10:03:42 +00:00
|
|
|
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
|
|
|
|
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))
|
|
|
|
logger.info("UPDATED order id: {id} description:{description}".format(
|
|
|
|
id=self.id, description=description))
|
|
|
|
if self.description != description:
|
|
|
|
self.description = description
|
|
|
|
self.save()
|
|
|
|
|
2014-07-18 15:32:27 +00:00
|
|
|
def cancel(self):
|
|
|
|
self.cancelled_on = timezone.now()
|
|
|
|
self.save()
|
2014-09-19 14:47:25 +00:00
|
|
|
logger.info("CANCELLED order id: {id}".format(id=self.id))
|
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()
|
2014-09-03 22:01:44 +00:00
|
|
|
|
|
|
|
@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-17 10:32:29 +00:00
|
|
|
|
2014-09-19 14:47:25 +00:00
|
|
|
_excluded_models = (MetricStorage, LogEntry, Order, ContentType, MigrationRecorder.Migration)
|
|
|
|
|
|
|
|
@receiver(post_delete, dispatch_uid="orders.cancel_orders")
|
2014-07-18 15:32:27 +00:00
|
|
|
def cancel_orders(sender, **kwargs):
|
2014-09-19 14:47:25 +00:00
|
|
|
if sender not in _excluded_models:
|
2014-07-21 15:43:36 +00:00
|
|
|
instance = kwargs['instance']
|
2014-09-19 14:47:25 +00:00
|
|
|
if hasattr(instance, 'account'):
|
|
|
|
for order in Order.objects.by_object(instance).active():
|
|
|
|
order.cancel()
|
|
|
|
else:
|
|
|
|
related = helpers.get_related_objects(instance)
|
|
|
|
if related and related != instance:
|
|
|
|
Order.update_orders(related)
|
2014-07-18 15:32:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, dispatch_uid="orders.update_orders")
|
|
|
|
def update_orders(sender, **kwargs):
|
2014-09-19 14:47:25 +00:00
|
|
|
if sender not in _excluded_models:
|
2014-07-21 15:43:36 +00:00
|
|
|
instance = kwargs['instance']
|
2014-09-19 14:47:25 +00:00
|
|
|
if hasattr(instance, 'account'):
|
2014-07-21 15:43:36 +00:00
|
|
|
Order.update_orders(instance)
|
2014-09-19 14:47:25 +00:00
|
|
|
else:
|
|
|
|
related = helpers.get_related_objects(instance)
|
|
|
|
if related and related != instance:
|
|
|
|
Order.update_orders(related)
|
2014-07-28 17:28:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
accounts.register(Order)
|