django-orchestra/orchestra/apps/orders/admin.py

53 lines
2 KiB
Python
Raw Normal View History

2014-05-27 15:55:09 +00:00
from django.contrib import admin
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
2014-07-16 15:20:16 +00:00
from django.utils.translation import ugettext_lazy as _
2014-05-27 15:55:09 +00:00
2014-07-22 21:47:01 +00:00
from orchestra.admin.utils import admin_link, admin_date
2014-07-18 15:32:27 +00:00
from orchestra.apps.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
from .actions import BillSelectedOrders
2014-09-03 14:51:07 +00:00
from .filters import ActiveOrderListFilter, BilledOrderListFilter
2014-09-17 10:32:29 +00:00
from .models import Order, MetricStorage
2014-05-27 15:55:09 +00:00
2014-09-26 15:05:20 +00:00
class OrderAdmin(AccountAdminMixin, admin.ModelAdmin):
2014-07-21 15:43:36 +00:00
list_display = (
2014-07-22 21:47:01 +00:00
'id', 'service', 'account_link', 'content_object_link',
2014-09-03 13:56:02 +00:00
'display_registered_on', 'display_billed_until', 'display_cancelled_on'
2014-07-21 15:43:36 +00:00
)
2014-09-03 13:56:02 +00:00
list_display_links = ('id', 'service')
2014-09-03 14:51:07 +00:00
list_filter = (ActiveOrderListFilter, BilledOrderListFilter, 'service',)
actions = (BillSelectedOrders(),)
2014-07-22 21:47:01 +00:00
date_hierarchy = 'registered_on'
2014-07-21 15:43:36 +00:00
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'
2014-08-22 15:31:44 +00:00
def get_queryset(self, request):
qs = super(OrderAdmin, self).get_queryset(request)
return qs.select_related('service').prefetch_related('content_object')
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)