django-orchestra/orchestra/admin/menu.py

139 lines
5.5 KiB
Python
Raw Normal View History

2014-05-08 16:59:35 +00:00
from admin_tools.menu import items, Menu
from django.core.urlresolvers import reverse
2014-07-08 15:19:15 +00:00
from django.utils.text import capfirst
2014-05-08 16:59:35 +00:00
from django.utils.translation import ugettext_lazy as _
2014-09-08 15:10:16 +00:00
from orchestra.core import services, accounts
2014-05-08 16:59:35 +00:00
from orchestra.utils.apps import isinstalled
def api_link(context):
""" Dynamically generates API related URL """
if 'opts' in context:
opts = context['opts']
elif 'cl' in context:
opts = context['cl'].opts
else:
return reverse('api-root')
if 'object_id' in context:
object_id = context['object_id']
try:
2014-07-08 15:19:15 +00:00
return reverse('%s-detail' % opts.model_name, args=[object_id])
2014-05-08 16:59:35 +00:00
except:
return reverse('api-root')
try:
2014-07-08 15:19:15 +00:00
return reverse('%s-list' % opts.model_name)
2014-05-08 16:59:35 +00:00
except:
return reverse('api-root')
def get_services():
2014-09-08 15:10:16 +00:00
childrens = []
2015-04-02 16:14:55 +00:00
for model, options in services.get().items():
2014-05-08 16:59:35 +00:00
if options.get('menu', True):
opts = model._meta
2014-07-24 09:53:34 +00:00
url = reverse('admin:{}_{}_changelist'.format(
opts.app_label, opts.model_name))
2014-07-08 15:19:15 +00:00
name = capfirst(options.get('verbose_name_plural'))
2014-09-08 15:10:16 +00:00
childrens.append(items.MenuItem(name, url))
return sorted(childrens, key=lambda i: i.title)
2014-05-08 16:59:35 +00:00
2014-09-08 15:10:16 +00:00
def get_accounts():
2014-11-18 13:59:21 +00:00
childrens=[]
2015-04-05 10:46:24 +00:00
if isinstalled('orchestra.contrib.payments'):
url = reverse('admin:payments_transactionprocess_changelist')
childrens.append(items.MenuItem(_("Transaction processes"), url))
2015-04-05 10:46:24 +00:00
if isinstalled('orchestra.contrib.issues'):
2014-07-23 18:28:40 +00:00
url = reverse('admin:issues_ticket_changelist')
childrens.append(items.MenuItem(_("Tickets"), url))
2015-04-02 16:14:55 +00:00
for model, options in accounts.get().items():
2014-09-08 15:10:16 +00:00
if options.get('menu', True):
opts = model._meta
url = reverse('admin:{}_{}_changelist'.format(
opts.app_label, opts.model_name))
name = capfirst(options.get('verbose_name_plural'))
childrens.append(items.MenuItem(name, url))
return sorted(childrens, key=lambda i: i.title)
2014-05-08 16:59:35 +00:00
2014-07-23 18:28:40 +00:00
def get_administration_items():
childrens = []
2015-04-26 13:53:00 +00:00
if isinstalled('orchestra.contrib.settings'):
2015-04-26 15:52:40 +00:00
url = reverse('admin:settings_setting_change')
2015-04-26 13:53:00 +00:00
childrens.append(items.MenuItem(_("Settings"), url))
2015-04-05 10:46:24 +00:00
if isinstalled('orchestra.contrib.services'):
2014-09-17 10:32:29 +00:00
url = reverse('admin:services_service_changelist')
2014-07-23 18:28:40 +00:00
childrens.append(items.MenuItem(_("Services"), url))
2014-11-18 13:59:21 +00:00
url = reverse('admin:plans_plan_changelist')
2014-09-16 15:01:02 +00:00
childrens.append(items.MenuItem(_("Plans"), url))
2015-04-05 10:46:24 +00:00
if isinstalled('orchestra.contrib.orchestration'):
2014-07-23 18:28:40 +00:00
route = reverse('admin:orchestration_route_changelist')
backendlog = reverse('admin:orchestration_backendlog_changelist')
server = reverse('admin:orchestration_server_changelist')
childrens.append(items.MenuItem(_("Orchestration"), route, children=[
items.MenuItem(_("Routes"), route),
items.MenuItem(_("Backend logs"), backendlog),
items.MenuItem(_("Servers"), server),
]))
2015-04-05 10:46:24 +00:00
if isinstalled('orchestra.contrib.resources'):
2014-07-23 18:28:40 +00:00
resource = reverse('admin:resources_resource_changelist')
data = reverse('admin:resources_resourcedata_changelist')
monitor = reverse('admin:resources_monitordata_changelist')
childrens.append(items.MenuItem(_("Resources"), resource, children=[
items.MenuItem(_("Resources"), resource),
items.MenuItem(_("Data"), data),
items.MenuItem(_("Monitoring"), monitor),
]))
2015-04-05 10:46:24 +00:00
if isinstalled('orchestra.contrib.miscellaneous'):
2014-07-23 18:28:40 +00:00
url = reverse('admin:miscellaneous_miscservice_changelist')
childrens.append(items.MenuItem(_("Miscellaneous"), url))
2015-04-05 10:46:24 +00:00
if isinstalled('orchestra.contrib.issues'):
2014-07-23 18:28:40 +00:00
url = reverse('admin:issues_queue_changelist')
2014-07-24 09:53:34 +00:00
childrens.append(items.MenuItem(_("Ticket queues"), url))
2014-07-23 18:28:40 +00:00
if isinstalled('djcelery'):
task = reverse('admin:djcelery_taskstate_changelist')
periodic = reverse('admin:djcelery_periodictask_changelist')
worker = reverse('admin:djcelery_workerstate_changelist')
childrens.append(items.MenuItem(_("Celery"), task, children=[
items.MenuItem(_("Tasks"), task),
items.MenuItem(_("Periodic tasks"), periodic),
items.MenuItem(_("Workers"), worker),
]))
return childrens
2014-05-08 16:59:35 +00:00
2014-10-18 12:26:59 +00:00
2014-05-08 16:59:35 +00:00
class OrchestraMenu(Menu):
2014-10-18 12:26:59 +00:00
template = 'admin/orchestra/menu.html'
2014-05-08 16:59:35 +00:00
def init_with_context(self, context):
2014-10-18 12:26:59 +00:00
self.children = [
# items.MenuItem(
# mark_safe('{site_name} <span style="{version_style}">v{version}</span>'.format(
# site_name=force_text(settings.SITE_VERBOSE_NAME),
# version_style="text-transform:none; float:none; font-size:smaller; background:none;",
# version=get_version())),
# reverse('admin:index')
# ),
# items.MenuItem(
# _('Dashboard'),
# reverse('admin:index')
# ),
# items.Bookmarks(),
2014-05-08 16:59:35 +00:00
items.MenuItem(
_("Services"),
children=get_services()
),
items.MenuItem(
_("Accounts"),
reverse('admin:accounts_account_changelist'),
2014-09-08 15:10:16 +00:00
children=get_accounts()
2014-05-08 16:59:35 +00:00
),
2014-07-23 18:28:40 +00:00
items.MenuItem(
2014-05-08 16:59:35 +00:00
_("Administration"),
2014-07-23 18:28:40 +00:00
children=get_administration_items()
2014-05-08 16:59:35 +00:00
),
2014-10-18 12:26:59 +00:00
items.MenuItem("API", api_link(context)),
2014-05-08 16:59:35 +00:00
]