2014-11-18 13:59:21 +00:00
|
|
|
from django.contrib import admin
|
2021-04-21 12:27:18 +00:00
|
|
|
from django.urls import reverse
|
2016-02-09 12:17:42 +00:00
|
|
|
from django.db import models
|
2021-05-21 08:07:59 +00:00
|
|
|
from django.utils.html import format_html
|
2023-11-17 12:25:13 +00:00
|
|
|
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
|
2015-07-21 10:44:32 +00:00
|
|
|
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]
|
2021-04-21 12:27:18 +00:00
|
|
|
|
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)
|
2021-05-21 08:07:59 +00:00
|
|
|
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'
|
2021-04-21 12:27:18 +00:00
|
|
|
|
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')
|
2015-05-08 14:05:57 +00:00
|
|
|
search_fields = ('account__username', 'plan__name', 'id')
|
2015-07-21 10:44:32 +00:00
|
|
|
actions = (list_accounts,)
|
2021-04-21 12:27:18 +00:00
|
|
|
|
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)
|