2015-05-07 14:18:09 +00:00
|
|
|
from copy import deepcopy
|
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
from admin_tools.menu import items, Menu
|
2021-04-21 12:27:18 +00:00
|
|
|
from django.urls 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 _
|
|
|
|
|
2015-05-05 19:42:55 +00:00
|
|
|
from orchestra.core import services, accounts, administration
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
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')
|
2021-04-21 12:27:18 +00:00
|
|
|
if 'object_id' in context:
|
2014-05-08 16:59:35 +00:00
|
|
|
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')
|
|
|
|
|
|
|
|
|
2015-05-07 14:09:37 +00:00
|
|
|
def process_registry(register):
|
2015-09-17 11:21:35 +00:00
|
|
|
def get_item(model, options, name=None):
|
|
|
|
if name is None:
|
|
|
|
name = capfirst(options.get('verbose_name_plural'))
|
2015-05-07 14:09:37 +00:00
|
|
|
if isinstance(model, str):
|
|
|
|
url = reverse('admin:'+model)
|
|
|
|
else:
|
2014-05-08 16:59:35 +00:00
|
|
|
opts = model._meta
|
2014-07-24 09:53:34 +00:00
|
|
|
url = reverse('admin:{}_{}_changelist'.format(
|
2015-09-17 11:21:35 +00:00
|
|
|
opts.app_label, opts.model_name)
|
|
|
|
)
|
|
|
|
item = items.MenuItem(name, url)
|
|
|
|
item.options = options
|
|
|
|
return item
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-05-07 14:09:37 +00:00
|
|
|
childrens = {}
|
|
|
|
for model, options in register.get().items():
|
|
|
|
if options.get('menu', True):
|
|
|
|
parent = options.get('parent')
|
|
|
|
if parent:
|
2015-09-17 11:21:35 +00:00
|
|
|
name = capfirst(model._meta.app_label)
|
2015-05-07 14:09:37 +00:00
|
|
|
parent_item = childrens.get(parent)
|
|
|
|
if parent_item:
|
|
|
|
if not parent_item.children:
|
2015-05-07 14:18:09 +00:00
|
|
|
parent_item.children.append(deepcopy(parent_item))
|
2015-09-17 11:21:35 +00:00
|
|
|
parent_item.title = name
|
2015-05-07 14:09:37 +00:00
|
|
|
else:
|
2015-09-17 11:21:35 +00:00
|
|
|
parent_item = get_item(parent, register[parent], name=name)
|
2015-05-07 14:09:37 +00:00
|
|
|
parent_item.children = []
|
|
|
|
parent_item.children.append(get_item(model, options))
|
|
|
|
childrens[parent] = parent_item
|
|
|
|
elif model not in childrens:
|
|
|
|
childrens[model] = get_item(model, options)
|
|
|
|
else:
|
|
|
|
childrens[model].children.insert(0, get_item(model, options))
|
|
|
|
return sorted(childrens.values(), key=lambda i: i.title)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OrchestraMenu(Menu):
|
2014-10-18 12:26:59 +00:00
|
|
|
template = 'admin/orchestra/menu.html'
|
2021-04-21 12:27:18 +00:00
|
|
|
|
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"),
|
2015-05-07 14:09:37 +00:00
|
|
|
children=process_registry(services)
|
2014-05-08 16:59:35 +00:00
|
|
|
),
|
|
|
|
items.MenuItem(
|
|
|
|
_("Accounts"),
|
|
|
|
reverse('admin:accounts_account_changelist'),
|
2015-05-07 14:09:37 +00:00
|
|
|
children=process_registry(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"),
|
2015-05-07 14:09:37 +00:00
|
|
|
children=process_registry(administration)
|
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
|
|
|
]
|