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):
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)

View File

@ -154,7 +154,7 @@ class Service(models.Model):
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:
@ -167,6 +167,12 @@ class Service(models.Model):
}
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):
def by_object(self, obj, *args, **kwargs):
@ -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")

View File

@ -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))