From f4732c9f8eb69c0fc1862247c1989a60b3bfdae5 Mon Sep 17 00:00:00 2001 From: Marc Date: Fri, 18 Jul 2014 16:02:05 +0000 Subject: [PATCH] Removed tmp file --- orchestra/apps/orders/admin.py | 6 ++++-- orchestra/apps/orders/models.py | 31 +++++++++++++++++++++++++------ orchestra/models/utils.py | 21 +-------------------- 3 files changed, 30 insertions(+), 28 deletions(-) diff --git a/orchestra/apps/orders/admin.py b/orchestra/apps/orders/admin.py index c69bf38e..5f2249ad 100644 --- a/orchestra/apps/orders/admin.py +++ b/orchestra/apps/orders/admin.py @@ -38,11 +38,13 @@ class ServiceAdmin(admin.ModelAdmin): class OrderAdmin(AccountAdminMixin, admin.ModelAdmin): - pass + list_display = ('id', 'service', 'account_link', 'cancelled_on') + list_filter = ('service',) class MetricStorageAdmin(admin.ModelAdmin): - pass + list_display = ('order', 'value', 'date') + list_filter = ('order__service',) admin.site.register(Service, ServiceAdmin) diff --git a/orchestra/apps/orders/models.py b/orchestra/apps/orders/models.py index b52ef564..421d2c35 100644 --- a/orchestra/apps/orders/models.py +++ b/orchestra/apps/orders/models.py @@ -149,12 +149,12 @@ class Service(models.Model): def __unicode__(self): return self.description - + @classmethod def get_services(cls, instance, **kwargs): # TODO get per-request cache from thread local cache = kwargs.get('cache', {}) - ct = ContentType.objects.get_for_model(type(instance)) + ct = ContentType.objects.get_for_model(instance) try: return cache[ct] except KeyError: @@ -166,6 +166,12 @@ class Service(models.Model): instance._meta.model_name: instance } return eval(self.match, safe_locals) + + def get_metric(self, instance): + safe_locals = { + instance._meta.model_name: instance + } + return eval(self.metric, safe_locals) class OrderQuerySet(models.QuerySet): @@ -188,7 +194,7 @@ class Order(models.Model): related_name='orders') content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField(null=True) - service = models.ForeignKey(Service, verbose_name=_("price"), + service = models.ForeignKey(Service, verbose_name=_("service"), related_name='orders') registered_on = models.DateTimeField(_("registered on"), auto_now_add=True) cancelled_on = models.DateTimeField(_("cancelled on"), null=True, blank=True) @@ -207,7 +213,7 @@ class Order(models.Model): instance = self.content_object if self.service.metric: metric = self.service.get_metric(instance) - self.store_metric(instance, metric) + MetricStorage.store(self, metric) description = "{}: {}".format(self.service.description, str(instance)) if self.description != description: self.description = description @@ -236,10 +242,23 @@ class Order(models.Model): class MetricStorage(models.Model): order = models.ForeignKey(Order, verbose_name=_("order")) value = models.BigIntegerField(_("value")) - date = models.DateTimeField(_("date")) + date = models.DateTimeField(_("date"), auto_now_add=True) + + class Meta: + get_latest_by = 'date' def __unicode__(self): - return self.order + 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) @receiver(pre_delete, dispatch_uid="orders.cancel_orders") diff --git a/orchestra/models/utils.py b/orchestra/models/utils.py index ff4d9874..10c182f5 100644 --- a/orchestra/models/utils.py +++ b/orchestra/models/utils.py @@ -1,5 +1,5 @@ from django.conf import settings -from django.db.models import loading, Manager +from django.db.models import loading from django.utils import importlib @@ -16,25 +16,6 @@ def get_model(label, import_module=True): return model -def queryset_as_manager(qs_class): - # Allow chained managers - # Based on http://djangosnippets.org/snippets/562/#c2486 - class ChainerManager(Manager): - def __init__(self): - super(ChainerManager,self).__init__() - self.queryset_class = qs_class - - def get_query_set(self): - return self.queryset_class(self.model) - - def __getattr__(self, attr, *args): - try: - return getattr(type(self), attr, *args) - except AttributeError: - return getattr(self.get_query_set(), attr, *args) - return ChainerManager() - - def get_field_value(obj, field_name): names = field_name.split('__') rel = getattr(obj, names.pop(0))