2014-09-17 10:32:29 +00:00
|
|
|
from django import forms
|
2015-05-19 13:27:04 +00:00
|
|
|
from django.conf.urls import url
|
2014-09-17 10:32:29 +00:00
|
|
|
from django.contrib import admin
|
|
|
|
from django.core.urlresolvers import reverse
|
2015-04-01 15:49:21 +00:00
|
|
|
from django.template.response import TemplateResponse
|
2014-09-17 10:32:29 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2014-11-18 13:59:21 +00:00
|
|
|
from orchestra.admin import ChangeViewActionsMixin
|
2016-03-31 16:18:38 +00:00
|
|
|
from orchestra.admin.actions import disable, enable
|
2014-09-17 10:32:29 +00:00
|
|
|
from orchestra.core import services
|
|
|
|
|
2014-10-23 15:38:46 +00:00
|
|
|
from .actions import update_orders, view_help, clone
|
2014-11-18 13:59:21 +00:00
|
|
|
from .models import Service
|
2014-09-17 10:32:29 +00:00
|
|
|
|
|
|
|
|
2014-09-26 10:38:50 +00:00
|
|
|
class ServiceAdmin(ChangeViewActionsMixin, admin.ModelAdmin):
|
2014-09-17 10:32:29 +00:00
|
|
|
list_display = (
|
|
|
|
'description', 'content_type', 'handler_type', 'num_orders', 'is_active'
|
|
|
|
)
|
2015-04-21 14:14:07 +00:00
|
|
|
list_filter = (
|
|
|
|
'is_active', 'handler_type', ('content_type', admin.RelatedOnlyFieldListFilter),
|
|
|
|
)
|
2014-09-17 10:32:29 +00:00
|
|
|
fieldsets = (
|
|
|
|
(None, {
|
|
|
|
'classes': ('wide',),
|
|
|
|
'fields': ('description', 'content_type', 'match', 'handler_type',
|
2014-10-16 17:14:21 +00:00
|
|
|
'ignore_superusers', 'is_active')
|
2014-09-17 10:32:29 +00:00
|
|
|
}),
|
|
|
|
(_("Billing options"), {
|
|
|
|
'classes': ('wide',),
|
2014-10-24 10:16:46 +00:00
|
|
|
'fields': ('billing_period', 'billing_point', 'is_fee', 'order_description',
|
|
|
|
'ignore_period')
|
2014-09-17 10:32:29 +00:00
|
|
|
}),
|
|
|
|
(_("Pricing options"), {
|
|
|
|
'classes': ('wide',),
|
|
|
|
'fields': ('metric', 'pricing_period', 'rate_algorithm',
|
|
|
|
'on_cancel', 'payment_style', 'tax', 'nominal_price')
|
|
|
|
}),
|
|
|
|
)
|
2016-03-31 16:18:38 +00:00
|
|
|
actions = (update_orders, clone, disable, enable)
|
2015-04-01 15:49:21 +00:00
|
|
|
change_view_actions = actions + (view_help,)
|
2015-03-31 12:39:08 +00:00
|
|
|
change_form_template = 'admin/services/service/change_form.html'
|
2014-09-17 10:32:29 +00:00
|
|
|
|
2015-04-01 15:49:21 +00:00
|
|
|
def get_urls(self):
|
|
|
|
"""Returns the additional urls for the change view links"""
|
|
|
|
urls = super(ServiceAdmin, self).get_urls()
|
|
|
|
admin_site = self.admin_site
|
|
|
|
opts = self.model._meta
|
2015-05-19 13:27:04 +00:00
|
|
|
return [
|
2015-04-01 15:49:21 +00:00
|
|
|
url('^add/help/$',
|
|
|
|
admin_site.admin_view(self.help_view),
|
|
|
|
name='%s_%s_help' % (opts.app_label, opts.model_name)
|
|
|
|
)
|
2015-05-19 13:27:04 +00:00
|
|
|
] + urls
|
2015-04-01 15:49:21 +00:00
|
|
|
|
2014-09-17 10:32:29 +00:00
|
|
|
def formfield_for_dbfield(self, db_field, **kwargs):
|
|
|
|
""" Improve performance of account field and filter by account """
|
|
|
|
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-10-23 15:38:46 +00:00
|
|
|
if db_field.name in ['match', 'metric', 'order_description']:
|
2014-09-17 10:32:29 +00:00
|
|
|
kwargs['widget'] = forms.TextInput(attrs={'size':'160'})
|
|
|
|
return super(ServiceAdmin, self).formfield_for_dbfield(db_field, **kwargs)
|
|
|
|
|
|
|
|
def num_orders(self, service):
|
|
|
|
num = service.orders__count
|
|
|
|
url = reverse('admin:orders_order_changelist')
|
2016-02-09 12:17:42 +00:00
|
|
|
url += '?service__id__exact=%i&is_active=True' % service.pk
|
2014-09-17 10:32:29 +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)
|
|
|
|
# Count active orders
|
|
|
|
qs = qs.extra(select={
|
|
|
|
'orders__count': (
|
|
|
|
"SELECT COUNT(*) "
|
|
|
|
"FROM orders_order "
|
|
|
|
"WHERE orders_order.service_id = services_service.id AND ("
|
|
|
|
" orders_order.cancelled_on IS NULL OR"
|
|
|
|
" orders_order.cancelled_on > '%s' "
|
|
|
|
")" % timezone.now()
|
|
|
|
)
|
|
|
|
})
|
|
|
|
return qs
|
2015-04-01 15:49:21 +00:00
|
|
|
|
|
|
|
def help_view(self, request, *args):
|
|
|
|
opts = self.model._meta
|
|
|
|
context = {
|
|
|
|
'add': True,
|
|
|
|
'title': _("Need some help?"),
|
|
|
|
'opts': opts,
|
|
|
|
'obj': args[0].get() if args else None,
|
|
|
|
'action_name': _("help"),
|
|
|
|
'app_label': opts.app_label,
|
|
|
|
}
|
|
|
|
return TemplateResponse(request, 'admin/services/service/help.html', context)
|
|
|
|
help_view.url_name = 'help'
|
|
|
|
help_view.verbose_name = _("Help")
|
2014-09-17 10:32:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
admin.site.register(Service, ServiceAdmin)
|