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

104 lines
3.8 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-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
from .actions import BillSelectedOrders
2014-07-21 15:43:36 +00:00
from .filters import ActiveOrderListFilter
2014-07-16 15:20:16 +00:00
from .models import Service, Order, MetricStorage
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',
'trial_period', 'refound_period', 'tax')
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',
'display_registered_on', 'display_cancelled_on'
2014-07-21 15:43:36 +00:00
)
2014-07-22 21:47:01 +00:00
list_display_link = ('id', 'service')
2014-07-21 15:43:36 +00:00
list_filter = (ActiveOrderListFilter, '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')
2014-07-22 21:47:01 +00:00
display_registered_on = admin_date('registered_on')
display_cancelled_on = admin_date('cancelled_on')
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-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)