navigation.py
143 lines
| 4.9 KiB
| text/x-python
|
PythonLexer
r1503 | # -*- coding: utf-8 -*- | ||
r2487 | # Copyright (C) 2016-2018 RhodeCode GmbH | ||
r1503 | # | ||
# This program is free software: you can redistribute it and/or modify | |||
# it under the terms of the GNU Affero General Public License, version 3 | |||
# (only), as published by the Free Software Foundation. | |||
# | |||
# This program is distributed in the hope that it will be useful, | |||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
# GNU General Public License for more details. | |||
# | |||
# You should have received a copy of the GNU Affero General Public License | |||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
# | |||
# This program is dual-licensed. If you wish to learn more about the | |||
# RhodeCode Enterprise Edition, including its added features, Support services, | |||
# and proprietary license terms, please see https://rhodecode.com/licenses/ | |||
import logging | |||
import collections | |||
from zope.interface import implementer | |||
from rhodecode.apps.admin.interfaces import IAdminNavigationRegistry | |||
r2327 | from rhodecode.lib.utils2 import str2bool | ||
r1503 | from rhodecode.translation import _ | ||
log = logging.getLogger(__name__) | |||
r2399 | NavListEntry = collections.namedtuple( | ||
'NavListEntry', ['key', 'name', 'url', 'active_list']) | |||
r1503 | |||
class NavEntry(object): | |||
""" | |||
Represents an entry in the admin navigation. | |||
:param key: Unique identifier used to store reference in an OrderedDict. | |||
:param name: Display name, usually a translation string. | |||
:param view_name: Name of the view, used generate the URL. | |||
r2399 | :param active_list: list of urls that we select active for this element | ||
r1503 | """ | ||
r2399 | def __init__(self, key, name, view_name, active_list=None): | ||
r1503 | self.key = key | ||
self.name = name | |||
self.view_name = view_name | |||
r2399 | self._active_list = active_list or [] | ||
r1503 | |||
def generate_url(self, request): | |||
r2333 | return request.route_path(self.view_name) | ||
r1503 | |||
def get_localized_name(self, request): | |||
r2333 | return request.translate(self.name) | ||
r1503 | |||
r2399 | @property | ||
def active_list(self): | |||
active_list = [self.key] | |||
if self._active_list: | |||
active_list = self._active_list | |||
return active_list | |||
r1503 | |||
@implementer(IAdminNavigationRegistry) | |||
class NavigationRegistry(object): | |||
_base_entries = [ | |||
r2333 | NavEntry('global', _('Global'), | ||
'admin_settings_global'), | |||
NavEntry('vcs', _('VCS'), | |||
'admin_settings_vcs'), | |||
NavEntry('visual', _('Visual'), | |||
'admin_settings_visual'), | |||
NavEntry('mapping', _('Remap and Rescan'), | |||
'admin_settings_mapping'), | |||
r1503 | NavEntry('issuetracker', _('Issue Tracker'), | ||
'admin_settings_issuetracker'), | |||
r2333 | NavEntry('email', _('Email'), | ||
'admin_settings_email'), | |||
NavEntry('hooks', _('Hooks'), | |||
'admin_settings_hooks'), | |||
NavEntry('search', _('Full Text Search'), | |||
'admin_settings_search'), | |||
r1503 | NavEntry('integrations', _('Integrations'), | ||
r2333 | 'global_integrations_home'), | ||
r1503 | NavEntry('system', _('System Info'), | ||
r2333 | 'admin_settings_system'), | ||
r1885 | NavEntry('process_management', _('Processes'), | ||
r2333 | 'admin_settings_process_management'), | ||
r1503 | NavEntry('sessions', _('User Sessions'), | ||
r2333 | 'admin_settings_sessions'), | ||
r1503 | NavEntry('open_source', _('Open Source Licenses'), | ||
r2333 | 'admin_settings_open_source'), | ||
r2637 | NavEntry('automation', _('Automation'), | ||
'admin_settings_automation') | |||
r1503 | ] | ||
r2333 | _labs_entry = NavEntry('labs', _('Labs'), | ||
'admin_settings_labs') | |||
r1503 | |||
def __init__(self, labs_active=False): | |||
r2323 | self._registered_entries = collections.OrderedDict() | ||
for item in self.__class__._base_entries: | |||
self._registered_entries[item.key] = item | |||
r1503 | |||
if labs_active: | |||
self.add_entry(self._labs_entry) | |||
def add_entry(self, entry): | |||
self._registered_entries[entry.key] = entry | |||
def get_navlist(self, request): | |||
navlist = [NavListEntry(i.key, i.get_localized_name(request), | |||
r2399 | i.generate_url(request), i.active_list) | ||
r1503 | for i in self._registered_entries.values()] | ||
return navlist | |||
r2324 | def navigation_registry(request, registry=None): | ||
r1503 | """ | ||
Helper that returns the admin navigation registry. | |||
""" | |||
r2351 | pyramid_registry = registry or request.registry | ||
r1503 | nav_registry = pyramid_registry.queryUtility(IAdminNavigationRegistry) | ||
return nav_registry | |||
def navigation_list(request): | |||
""" | |||
Helper that returns the admin navigation as list of NavListEntry objects. | |||
""" | |||
return navigation_registry(request).get_navlist(request) | |||
r2327 | |||
def includeme(config): | |||
# Create admin navigation registry and add it to the pyramid registry. | |||
settings = config.get_settings() | |||
labs_active = str2bool(settings.get('labs_settings_active', False)) | |||
navigation_registry = NavigationRegistry(labs_active=labs_active) | |||
config.registry.registerUtility(navigation_registry) |