Removed tmp file

This commit is contained in:
Marc 2014-07-18 16:02:05 +00:00
parent 8320a119f4
commit f4732c9f8e
3 changed files with 30 additions and 28 deletions

View file

@ -38,11 +38,13 @@ class ServiceAdmin(admin.ModelAdmin):
class OrderAdmin(AccountAdminMixin, admin.ModelAdmin): class OrderAdmin(AccountAdminMixin, admin.ModelAdmin):
pass list_display = ('id', 'service', 'account_link', 'cancelled_on')
list_filter = ('service',)
class MetricStorageAdmin(admin.ModelAdmin): class MetricStorageAdmin(admin.ModelAdmin):
pass list_display = ('order', 'value', 'date')
list_filter = ('order__service',)
admin.site.register(Service, ServiceAdmin) admin.site.register(Service, ServiceAdmin)

View file

@ -149,12 +149,12 @@ class Service(models.Model):
def __unicode__(self): def __unicode__(self):
return self.description return self.description
@classmethod @classmethod
def get_services(cls, instance, **kwargs): def get_services(cls, instance, **kwargs):
# TODO get per-request cache from thread local # TODO get per-request cache from thread local
cache = kwargs.get('cache', {}) cache = kwargs.get('cache', {})
ct = ContentType.objects.get_for_model(type(instance)) ct = ContentType.objects.get_for_model(instance)
try: try:
return cache[ct] return cache[ct]
except KeyError: except KeyError:
@ -166,6 +166,12 @@ class Service(models.Model):
instance._meta.model_name: instance instance._meta.model_name: instance
} }
return eval(self.match, safe_locals) 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): class OrderQuerySet(models.QuerySet):
@ -188,7 +194,7 @@ class Order(models.Model):
related_name='orders') related_name='orders')
content_type = models.ForeignKey(ContentType) content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField(null=True) object_id = models.PositiveIntegerField(null=True)
service = models.ForeignKey(Service, verbose_name=_("price"), service = models.ForeignKey(Service, verbose_name=_("service"),
related_name='orders') related_name='orders')
registered_on = models.DateTimeField(_("registered on"), auto_now_add=True) registered_on = models.DateTimeField(_("registered on"), auto_now_add=True)
cancelled_on = models.DateTimeField(_("cancelled on"), null=True, blank=True) cancelled_on = models.DateTimeField(_("cancelled on"), null=True, blank=True)
@ -207,7 +213,7 @@ class Order(models.Model):
instance = self.content_object instance = self.content_object
if self.service.metric: if self.service.metric:
metric = self.service.get_metric(instance) metric = self.service.get_metric(instance)
self.store_metric(instance, metric) MetricStorage.store(self, metric)
description = "{}: {}".format(self.service.description, str(instance)) description = "{}: {}".format(self.service.description, str(instance))
if self.description != description: if self.description != description:
self.description = description self.description = description
@ -236,10 +242,23 @@ class Order(models.Model):
class MetricStorage(models.Model): class MetricStorage(models.Model):
order = models.ForeignKey(Order, verbose_name=_("order")) order = models.ForeignKey(Order, verbose_name=_("order"))
value = models.BigIntegerField(_("value")) 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): 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") @receiver(pre_delete, dispatch_uid="orders.cancel_orders")

View file

@ -1,5 +1,5 @@
from django.conf import settings from django.conf import settings
from django.db.models import loading, Manager from django.db.models import loading
from django.utils import importlib from django.utils import importlib
@ -16,25 +16,6 @@ def get_model(label, import_module=True):
return model 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): def get_field_value(obj, field_name):
names = field_name.split('__') names = field_name.split('__')
rel = getattr(obj, names.pop(0)) rel = getattr(obj, names.pop(0))