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-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
|
|
|
|
from orchestra.admin.utils import admin_link
|
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-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):
|
|
|
|
num = service.orders.count()
|
|
|
|
url = reverse('admin:orders_order_changelist')
|
|
|
|
url += '?service=%i' % service.pk
|
|
|
|
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)
|
|
|
|
qs = qs.annotate(models.Count('orders'))
|
|
|
|
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 = (
|
|
|
|
'id', 'service', 'account_link', 'content_object_link', 'cancelled_on'
|
|
|
|
)
|
|
|
|
list_filter = (ActiveOrderListFilter, 'service',)
|
|
|
|
default_changelist_filters = (
|
|
|
|
('is_active', 'True'),
|
|
|
|
)
|
|
|
|
|
|
|
|
content_object_link = admin_link('content_object')
|
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)
|