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

135 lines
5 KiB
Python
Raw Normal View History

2014-07-16 15:20:16 +00:00
from django import forms
2014-07-21 15:43:36 +00:00
from django.db import models
2014-05-27 15:55:09 +00:00
from django.contrib import admin
2014-07-21 15:43:36 +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
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-21 15:43:36 +00:00
from orchestra.admin import ChangeListDefaultFilter
from orchestra.admin.filters import UsedContentTypeFilter
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-07-16 15:20:16 +00:00
from orchestra.core import services
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-08 14:23:06 +00:00
from .models import Plan, Rate, Service, Order, MetricStorage
class PlanAdmin(AccountAdminMixin, admin.ModelAdmin):
list_display = ('name', 'account_link')
list_filter = ('name',)
class RateInline(admin.TabularInline):
model = Rate
ordering = ('plan', 'quantity')
2014-07-16 15:20:16 +00:00
class ServiceAdmin(admin.ModelAdmin):
2014-07-21 15:43:36 +00:00
list_display = (
'description', 'content_type', 'handler_type', 'num_orders', 'is_active'
)
list_filter = ('is_active', 'handler_type', UsedContentTypeFilter)
2014-07-16 15:20:16 +00:00
fieldsets = (
(None, {
'classes': ('wide',),
2014-07-21 15:43:36 +00:00
'fields': ('description', 'content_type', 'match', 'handler_type',
'is_active')
2014-07-16 15:20:16 +00:00
}),
(_("Billing options"), {
'classes': ('wide',),
'fields': ('billing_period', 'billing_point', 'delayed_billing',
'is_fee')
}),
(_("Pricing options"), {
'classes': ('wide',),
'fields': ('metric', 'pricing_period', 'rate_algorithm',
2014-07-21 12:20:04 +00:00
'orders_effect', 'on_cancel', 'payment_style',
2014-09-08 14:23:06 +00:00
'trial_period', 'refound_period', 'tax', 'nominal_price')
2014-07-16 15:20:16 +00:00
}),
)
2014-09-08 14:23:06 +00:00
inlines = [RateInline]
2014-07-16 15:20:16 +00:00
def formfield_for_dbfield(self, db_field, **kwargs):
""" Improve performance of account field and filter by account """
2014-07-21 12:20:04 +00:00
if db_field.name == 'content_type':
models = [model._meta.model_name for model in services.get()]
queryset = db_field.rel.to.objects
kwargs['queryset'] = queryset.filter(model__in=models)
2014-07-16 15:20:16 +00:00
if db_field.name in ['match', 'metric']:
kwargs['widget'] = forms.TextInput(attrs={'size':'160'})
return super(ServiceAdmin, self).formfield_for_dbfield(db_field, **kwargs)
2014-07-21 15:43:36 +00:00
def num_orders(self, service):
2014-07-22 21:47:01 +00:00
num = service.orders__count
2014-07-21 15:43:36 +00:00
url = reverse('admin:orders_order_changelist')
2014-07-22 21:47:01 +00:00
url += '?service=%i&is_active=True' % service.pk
2014-07-21 15:43:36 +00:00
return '<a href="%s">%d</a>' % (url, num)
num_orders.short_description = _("Orders")
num_orders.admin_order_field = 'orders__count'
num_orders.allow_tags = True
def get_queryset(self, request):
qs = super(ServiceAdmin, self).get_queryset(request)
2014-07-22 21:47:01 +00:00
# Count active orders
qs = qs.extra(select={
'orders__count': (
"SELECT COUNT(*) "
"FROM orders_order "
"WHERE orders_order.service_id = orders_service.id AND ("
" orders_order.cancelled_on IS NULL OR"
" orders_order.cancelled_on > '%s' "
")" % timezone.now()
)
})
2014-07-21 15:43:36 +00:00
return qs
2014-05-27 15:55:09 +00:00
2014-07-21 15:43:36 +00:00
class OrderAdmin(AccountAdminMixin, ChangeListDefaultFilter, admin.ModelAdmin):
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
default_changelist_filters = (
('is_active', 'True'),
)
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 = ''
if value and value < timezone.now():
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-05-27 15:55:09 +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
2014-09-08 14:23:06 +00:00
admin.site.register(Plan, PlanAdmin)
2014-07-16 15:20:16 +00:00
admin.site.register(Service, ServiceAdmin)
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)