django-orchestra-test/orchestra/contrib/orders/admin.py

99 lines
3.8 KiB
Python
Raw Normal View History

2014-05-27 15:55:09 +00:00
from django.contrib import admin
2015-03-27 19:50:54 +00:00
from django.core.urlresolvers import reverse
2014-07-22 21:47:01 +00:00
from django.utils import timezone
2014-09-03 14:51:07 +00:00
from django.utils.html import escape
2015-03-27 19:50:54 +00:00
from django.utils.safestring import mark_safe
2014-07-16 15:20:16 +00:00
from django.utils.translation import ugettext_lazy as _
2014-05-27 15:55:09 +00:00
2015-03-27 19:50:54 +00:00
from orchestra.admin import ExtendedModelAdmin
2014-07-22 21:47:01 +00:00
from orchestra.admin.utils import admin_link, admin_date
2015-04-05 10:46:24 +00:00
from orchestra.contrib.accounts.admin import AccountAdminMixin
2014-09-03 14:51:07 +00:00
from orchestra.utils.humanize import naturaldate
2014-07-16 15:20:16 +00:00
2014-10-16 17:14:21 +00:00
from .actions import BillSelectedOrders, mark_as_ignored, mark_as_not_ignored
from .filters import IgnoreOrderListFilter, ActiveOrderListFilter, BilledOrderListFilter
2014-09-17 10:32:29 +00:00
from .models import Order, MetricStorage
2014-05-27 15:55:09 +00:00
2015-03-27 19:50:54 +00:00
class MetricStorageInline(admin.TabularInline):
model = MetricStorage
readonly_fields = ('value', 'updated_on')
extra = 0
def has_add_permission(self, request, obj=None):
return False
def get_fieldsets(self, request, obj=None):
if obj:
url = reverse('admin:orders_metricstorage_changelist')
url += '?order=%i' % obj.pk
title = _('Metric storage, last 10 entries, <a href="%s">(See all)</a>')
self.verbose_name_plural = mark_safe(title % url)
return super(MetricStorageInline, self).get_fieldsets(request, obj)
def get_queryset(self, request):
qs = super(MetricStorageInline, self).get_queryset(request)
if self.parent_object and self.parent_object.pk:
qs = qs.filter(order=self.parent_object.pk).order_by('-id')
try:
tenth_id = qs.values_list('id', flat=True)[10]
except IndexError:
pass
else:
return qs.filter(pk__lte=tenth_id)
return qs
class OrderAdmin(AccountAdminMixin, ExtendedModelAdmin):
2014-07-21 15:43:36 +00:00
list_display = (
2014-10-21 15:29:36 +00:00
'id', 'service_link', 'account_link', 'content_object_link',
2015-03-27 19:50:54 +00:00
'display_registered_on', 'display_billed_until', 'display_cancelled_on', 'display_metric'
2014-07-21 15:43:36 +00:00
)
2014-10-16 17:14:21 +00:00
list_filter = (ActiveOrderListFilter, BilledOrderListFilter, IgnoreOrderListFilter, 'service',)
default_changelist_filters = (
('ignore', '0'),
)
actions = (BillSelectedOrders(), mark_as_ignored, mark_as_not_ignored)
2015-03-27 19:50:54 +00:00
change_view_actions = (BillSelectedOrders(), mark_as_ignored, mark_as_not_ignored)
2014-07-22 21:47:01 +00:00
date_hierarchy = 'registered_on'
2015-03-27 19:50:54 +00:00
inlines = (MetricStorageInline,)
add_inlines = ()
search_fields = ('account__username', 'description')
2015-04-01 15:49:21 +00:00
list_prefetch_related = ('metrics', 'content_object')
list_select_related = ('account', 'service')
2014-07-21 15:43:36 +00:00
2014-10-21 15:29:36 +00:00
service_link = admin_link('service')
content_object_link = admin_link('content_object', order=False)
2014-07-22 21:47:01 +00:00
display_registered_on = admin_date('registered_on')
display_cancelled_on = admin_date('cancelled_on')
2014-08-22 15:31:44 +00:00
2014-09-03 14:51:07 +00:00
def display_billed_until(self, order):
value = order.billed_until
color = ''
2014-09-30 14:46:29 +00:00
if value and value < timezone.now().date():
2014-09-03 14:51:07 +00:00
color = 'style="color:red;"'
return '<span title="{raw}" {color}>{human}</span>'.format(
raw=escape(str(value)), color=color, human=escape(naturaldate(value)),
)
display_billed_until.short_description = _("billed until")
display_billed_until.allow_tags = True
display_billed_until.admin_order_field = 'billed_until'
2015-03-27 19:50:54 +00:00
def display_metric(self, order):
2015-04-01 15:49:21 +00:00
""" dispalys latest metric value, don't uses latest() because not loosing prefetch_related """
try:
metric = order.metrics.all()[0]
except IndexError:
return ''
return metric.value
2015-03-27 19:50:54 +00:00
display_metric.short_description = _("Metric")
2014-08-22 15:31:44 +00:00
2014-07-22 21:47:01 +00:00
2014-07-16 15:20:16 +00:00
class MetricStorageAdmin(admin.ModelAdmin):
2014-07-21 12:20:04 +00:00
list_display = ('order', 'value', 'created_on', 'updated_on')
2014-07-18 16:02:05 +00:00
list_filter = ('order__service',)
2014-05-27 15:55:09 +00:00
admin.site.register(Order, OrderAdmin)
2014-07-16 15:20:16 +00:00
admin.site.register(MetricStorage, MetricStorageAdmin)