manager: dynamically produce homepage menu (fixes #24229)
New entries can be added by applications using the a2_hook_manager_homepage_entries(view) hook. Each entry is a dict containing at least a string label and a string href. A CSS "class", a numeric "order" and a string "permission" can also be provisionning but are not mandatory.
This commit is contained in:
parent
eeae1f3f8f
commit
c0f3b54ebf
|
@ -17,34 +17,11 @@
|
|||
|
||||
{% block content %}
|
||||
<ul class="apps">
|
||||
{% if perms.any.a2_rbac.search_organizationalunit %}
|
||||
<li class="icon-organizational-units">
|
||||
<a href="{% url "a2-manager-ous" %}">
|
||||
{% trans "Organizational units" %}
|
||||
</a>
|
||||
{% for entry in entries %}
|
||||
<li {% if entry.class %}class="{{ entry.class }}"{% endif %}>
|
||||
<a href="{{ entry.href }}">{{ entry.label }}</a>
|
||||
</li>
|
||||
{% endif %}
|
||||
{% if perms.any.custom_user.search_user %}
|
||||
<li class="icon-users">
|
||||
<a href="{% url "a2-manager-users" %}">
|
||||
{% trans "Users" %}
|
||||
</a>
|
||||
</li>
|
||||
{% endif %}
|
||||
{% if perms.any.a2_rbac.search_role %}
|
||||
<li class="icon-roles">
|
||||
<a href="{% url "a2-manager-roles" %}">
|
||||
{% trans "Roles" %}
|
||||
</a>
|
||||
</li>
|
||||
{% endif %}
|
||||
{% if perms.any.authentic2.search_service %}
|
||||
<li class="icon-services">
|
||||
<a href="{% url "a2-manager-services" %}">
|
||||
{% trans "Services" %}
|
||||
</a>
|
||||
</li>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</ul>
|
||||
<br style="clear: both;"/>
|
||||
{% endblock %}
|
||||
|
|
|
@ -541,12 +541,65 @@ class HomepageView(TitleMixin, PermissionMixin, MediaMixin, TemplateView):
|
|||
template_name = 'authentic2/manager/homepage.html'
|
||||
permissions = ['a2_rbac.search_role', 'a2_rbac.search_organizationalunit',
|
||||
'auth.search_group', 'custom_user.search_user']
|
||||
default_entries = [
|
||||
{
|
||||
'class': 'icon-organizational-units',
|
||||
'href': reverse_lazy('a2-manager-ous'),
|
||||
'label': _('Organizational units'),
|
||||
'order': -1,
|
||||
'permission': 'a2_rbac.search_organizationalunit',
|
||||
},
|
||||
{
|
||||
'class': 'icon-users',
|
||||
'href': reverse_lazy('a2-manager-users'),
|
||||
'label': _('Users'),
|
||||
'order': -1,
|
||||
'permission': 'a2_rbac.search_user',
|
||||
},
|
||||
{
|
||||
'class': 'icon-roles',
|
||||
'href': reverse_lazy('a2-manager-roles'),
|
||||
'label': _('Roles'),
|
||||
'order': -1,
|
||||
'permission': 'a2_rbac.search_role',
|
||||
},
|
||||
{
|
||||
'class': 'icon-services',
|
||||
'href': reverse_lazy('a2-manager-services'),
|
||||
'label': _('Services'),
|
||||
'order': -1,
|
||||
'permission': 'a2_rbac.search_service',
|
||||
},
|
||||
]
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
if app_settings.HOMEPAGE_URL:
|
||||
return redirect(request, app_settings.HOMEPAGE_URL)
|
||||
return super(HomepageView, self).dispatch(request, *args, **kwargs)
|
||||
|
||||
def get_homepage_entries(self):
|
||||
entries = []
|
||||
for entry in self.default_entries:
|
||||
if 'permission' in entry and not self.request.user.has_perm(entry['permission']):
|
||||
continue
|
||||
entries.append(entry)
|
||||
for hook_entries in hooks.call_hooks('manager_homepage_entries', self):
|
||||
if not hasattr(hook_entries, 'append'):
|
||||
hook_entries = [hook_entries]
|
||||
for entry in hook_entries:
|
||||
if 'permission' in entry and not self.request.user.has_perm(entry['permission']):
|
||||
continue
|
||||
entries.append(entry)
|
||||
# use possible key order to sort
|
||||
# list.sort() is supposed to be a stable sort (already sorted entries
|
||||
# are kept in the same order)
|
||||
entries.sort(key=lambda d: d.get('order', 0))
|
||||
return entries
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
kwargs['entries'] = self.get_homepage_entries()
|
||||
return super(HomepageView, self).get_context_data(**kwargs)
|
||||
|
||||
|
||||
homepage = HomepageView.as_view()
|
||||
|
||||
|
|
Loading…
Reference in New Issue