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
|
|
|
|
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 _
|
|
|
|
|
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')
|
|
|
|
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')
|
|
|
|
|
|
|
|
|
2015-05-07 14:09:37 +00:00
|
|
|
def process_registry(register):
|
2015-07-21 12:23:40 +00:00
|
|
|
def get_item(model, options, parent=False):
|
|
|
|
name = 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-05-07 14:09:37 +00:00
|
|
|
opts.app_label, opts.model_name))
|
2015-07-21 12:23:40 +00:00
|
|
|
if parent:
|
|
|
|
name = opts.app_label
|
|
|
|
name = capfirst(name)
|
2015-05-07 14:09:37 +00:00
|
|
|
return items.MenuItem(name, url)
|
|
|
|
|
|
|
|
childrens = {}
|
|
|
|
for model, options in register.get().items():
|
|
|
|
if options.get('menu', True):
|
|
|
|
parent = options.get('parent')
|
|
|
|
if parent:
|
|
|
|
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-05-07 14:09:37 +00:00
|
|
|
else:
|
2015-07-21 12:23:40 +00:00
|
|
|
parent_item = get_item(parent, register[parent], parent=True)
|
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'
|
|
|
|
|
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
|
|
|
]
|