django-orchestra/orchestra/admin/menu.py

95 lines
3.3 KiB
Python
Raw Normal View History

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')
def process_registry(register):
def get_item(model, options):
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(
opts.app_label, opts.model_name))
name = capfirst(options.get('verbose_name_plural'))
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))
else:
parent_item = get_item(parent, register[parent])
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"),
children=process_registry(services)
2014-05-08 16:59:35 +00:00
),
items.MenuItem(
_("Accounts"),
reverse('admin:accounts_account_changelist'),
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"),
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
]