django-orchestra/orchestra/contrib/plans/admin.py

60 lines
2 KiB
Python
Raw Normal View History

2014-11-18 13:59:21 +00:00
from django.contrib import admin
from django.urls import reverse
2016-02-09 12:17:42 +00:00
from django.db import models
from django.utils.html import format_html
from django.utils.translation import gettext_lazy as _
2014-11-18 13:59:21 +00:00
from orchestra.admin import ExtendedModelAdmin
2016-02-09 12:17:42 +00:00
from orchestra.admin.utils import insertattr, admin_link
from orchestra.contrib.accounts.actions import list_accounts
2015-04-05 10:46:24 +00:00
from orchestra.contrib.accounts.admin import AccountAdminMixin
from orchestra.contrib.services.models import Service
2014-11-18 13:59:21 +00:00
from .models import Plan, ContractedPlan, Rate
class RateInline(admin.TabularInline):
model = Rate
2015-04-21 13:12:48 +00:00
ordering = ('service', 'plan', 'quantity')
2014-11-18 13:59:21 +00:00
class PlanAdmin(ExtendedModelAdmin):
2016-02-09 12:17:42 +00:00
list_display = (
'name', 'is_default', 'is_combinable', 'allow_multiple', 'is_active', 'num_contracts',
)
2015-04-09 14:32:10 +00:00
list_filter = ('is_default', 'is_combinable', 'allow_multiple', 'is_active')
2014-11-18 13:59:21 +00:00
fields = ('verbose_name', 'name', 'is_default', 'is_combinable', 'allow_multiple')
prepopulated_fields = {
'name': ('verbose_name',)
}
change_readonly_fields = ('name',)
inlines = [RateInline]
2016-02-09 12:17:42 +00:00
def num_contracts(self, plan):
num = plan.contracts__count
url = reverse('admin:plans_contractedplan_changelist')
url += '?plan__name={}'.format(plan.name)
return format_html('<a href="{0}">{1}</a>', url, num)
2016-02-09 12:17:42 +00:00
num_contracts.short_description = _("Contracts")
num_contracts.admin_order_field = 'contracts__count'
2016-02-09 12:17:42 +00:00
def get_queryset(self, request):
qs = super(PlanAdmin, self).get_queryset(request)
return qs.annotate(models.Count('contracts', distinct=True))
2014-11-18 13:59:21 +00:00
class ContractedPlanAdmin(AccountAdminMixin, admin.ModelAdmin):
2016-02-09 12:17:42 +00:00
list_display = ('id', 'plan_link', 'account_link')
2014-11-18 13:59:21 +00:00
list_filter = ('plan__name',)
2015-04-16 13:15:21 +00:00
list_select_related = ('plan', 'account')
search_fields = ('account__username', 'plan__name', 'id')
actions = (list_accounts,)
2016-02-09 12:17:42 +00:00
plan_link = admin_link('plan')
2014-11-18 13:59:21 +00:00
2015-05-09 15:37:35 +00:00
2014-11-18 13:59:21 +00:00
admin.site.register(Plan, PlanAdmin)
admin.site.register(ContractedPlan, ContractedPlanAdmin)
insertattr(Service, 'inlines', RateInline)