##// END OF EJS Templates
admin: Use pyramid translation instead of pylons.
Martin Bornhold -
r299:5db189c6 default
parent child Browse files
Show More
@@ -1,128 +1,124 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2016-2016 RhodeCode GmbH
3 # Copyright (C) 2016-2016 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21
21
22 import logging
22 import logging
23 import collections
23 import collections
24
24
25 from pylons import url
25 from pylons import url
26 from pylons.i18n.translation import lazy_ugettext
27 from zope.interface import implementer
26 from zope.interface import implementer
28
27
29 from rhodecode.admin.interfaces import IAdminNavigationRegistry
28 from rhodecode.admin.interfaces import IAdminNavigationRegistry
30 from rhodecode.lib.utils import get_registry
29 from rhodecode.lib.utils import get_registry
30 from rhodecode.translation import _
31
31
32
32
33 log = logging.getLogger(__name__)
33 log = logging.getLogger(__name__)
34
34
35 NavListEntry = collections.namedtuple('NavListEntry', ['key', 'name', 'url'])
35 NavListEntry = collections.namedtuple('NavListEntry', ['key', 'name', 'url'])
36
36
37
37
38 class NavEntry(object):
38 class NavEntry(object):
39 """
39 """
40 Represents an entry in the admin navigation.
40 Represents an entry in the admin navigation.
41
41
42 :param key: Unique identifier used to store reference in an OrderedDict.
42 :param key: Unique identifier used to store reference in an OrderedDict.
43 :param name: Display name, usually a translation string.
43 :param name: Display name, usually a translation string.
44 :param view_name: Name of the view, used generate the URL.
44 :param view_name: Name of the view, used generate the URL.
45 :param pyramid: Indicator to use pyramid for URL generation. This should
45 :param pyramid: Indicator to use pyramid for URL generation. This should
46 be removed as soon as we are fully migrated to pyramid.
46 be removed as soon as we are fully migrated to pyramid.
47 """
47 """
48
48
49 def __init__(self, key, name, view_name, pyramid=False):
49 def __init__(self, key, name, view_name, pyramid=False):
50 self.key = key
50 self.key = key
51 self.name = name
51 self.name = name
52 self.view_name = view_name
52 self.view_name = view_name
53 self.pyramid = pyramid
53 self.pyramid = pyramid
54
54
55 def generate_url(self, request):
55 def generate_url(self, request):
56 if self.pyramid:
56 if self.pyramid:
57 if hasattr(request, 'route_path'):
57 if hasattr(request, 'route_path'):
58 return request.route_path(self.view_name)
58 return request.route_path(self.view_name)
59 else:
59 else:
60 # TODO: johbo: Remove this after migrating to pyramid.
60 # TODO: johbo: Remove this after migrating to pyramid.
61 # We need the pyramid request here to generate URLs to pyramid
61 # We need the pyramid request here to generate URLs to pyramid
62 # views from within pylons views.
62 # views from within pylons views.
63 from pyramid.threadlocal import get_current_request
63 from pyramid.threadlocal import get_current_request
64 pyramid_request = get_current_request()
64 pyramid_request = get_current_request()
65 return pyramid_request.route_path(self.view_name)
65 return pyramid_request.route_path(self.view_name)
66 else:
66 else:
67 return url(self.view_name)
67 return url(self.view_name)
68
68
69
69
70 @implementer(IAdminNavigationRegistry)
70 @implementer(IAdminNavigationRegistry)
71 class NavigationRegistry(object):
71 class NavigationRegistry(object):
72
72
73 _base_entries = [
73 _base_entries = [
74 NavEntry('global', lazy_ugettext('Global'), 'admin_settings_global'),
74 NavEntry('global', _('Global'), 'admin_settings_global'),
75 NavEntry('vcs', lazy_ugettext('VCS'), 'admin_settings_vcs'),
75 NavEntry('vcs', _('VCS'), 'admin_settings_vcs'),
76 NavEntry('visual', lazy_ugettext('Visual'), 'admin_settings_visual'),
76 NavEntry('visual', _('Visual'), 'admin_settings_visual'),
77 NavEntry('mapping', lazy_ugettext('Remap and Rescan'),
77 NavEntry('mapping', _('Remap and Rescan'), 'admin_settings_mapping'),
78 'admin_settings_mapping'),
78 NavEntry('issuetracker', _('Issue Tracker'),
79 NavEntry('issuetracker', lazy_ugettext('Issue Tracker'),
80 'admin_settings_issuetracker'),
79 'admin_settings_issuetracker'),
81 NavEntry('email', lazy_ugettext('Email'), 'admin_settings_email'),
80 NavEntry('email', _('Email'), 'admin_settings_email'),
82 NavEntry('hooks', lazy_ugettext('Hooks'), 'admin_settings_hooks'),
81 NavEntry('hooks', _('Hooks'), 'admin_settings_hooks'),
83 NavEntry('search', lazy_ugettext('Full Text Search'),
82 NavEntry('search', _('Full Text Search'), 'admin_settings_search'),
84 'admin_settings_search'),
83 NavEntry('system', _('System Info'), 'admin_settings_system'),
85 NavEntry('system', lazy_ugettext('System Info'),
84 NavEntry('open_source', _('Open Source Licenses'),
86 'admin_settings_system'),
87 NavEntry('open_source', lazy_ugettext('Open Source Licenses'),
88 'admin_settings_open_source', pyramid=True),
85 'admin_settings_open_source', pyramid=True),
89 # TODO: marcink: we disable supervisor now until the supervisor stats
86 # TODO: marcink: we disable supervisor now until the supervisor stats
90 # page is fixed in the nix configuration
87 # page is fixed in the nix configuration
91 # NavEntry('supervisor', lazy_ugettext('Supervisor'),
88 # NavEntry('supervisor', _('Supervisor'), 'admin_settings_supervisor'),
92 # 'admin_settings_supervisor'),
93 ]
89 ]
94
90
95 _labs_entry = NavEntry('labs', lazy_ugettext('Labs'),
91 _labs_entry = NavEntry('labs', _('Labs'),
96 'admin_settings_labs')
92 'admin_settings_labs')
97
93
98 def __init__(self, labs_active=False):
94 def __init__(self, labs_active=False):
99 self._registered_entries = collections.OrderedDict([
95 self._registered_entries = collections.OrderedDict([
100 (item.key, item) for item in self.__class__._base_entries
96 (item.key, item) for item in self.__class__._base_entries
101 ])
97 ])
102
98
103 if labs_active:
99 if labs_active:
104 self.add_entry(self._labs_entry)
100 self.add_entry(self._labs_entry)
105
101
106 def add_entry(self, entry):
102 def add_entry(self, entry):
107 self._registered_entries[entry.key] = entry
103 self._registered_entries[entry.key] = entry
108
104
109 def get_navlist(self, request):
105 def get_navlist(self, request):
110 navlist = [NavListEntry(i.key, i.name, i.generate_url(request))
106 navlist = [NavListEntry(i.key, i.name, i.generate_url(request))
111 for i in self._registered_entries.values()]
107 for i in self._registered_entries.values()]
112 return navlist
108 return navlist
113
109
114
110
115 def navigation_registry(request):
111 def navigation_registry(request):
116 """
112 """
117 Helper that returns the admin navigation registry.
113 Helper that returns the admin navigation registry.
118 """
114 """
119 pyramid_registry = get_registry(request)
115 pyramid_registry = get_registry(request)
120 nav_registry = pyramid_registry.queryUtility(IAdminNavigationRegistry)
116 nav_registry = pyramid_registry.queryUtility(IAdminNavigationRegistry)
121 return nav_registry
117 return nav_registry
122
118
123
119
124 def navigation_list(request):
120 def navigation_list(request):
125 """
121 """
126 Helper that returns the admin navigation as list of NavListEntry objects.
122 Helper that returns the admin navigation as list of NavListEntry objects.
127 """
123 """
128 return navigation_registry(request).get_navlist(request)
124 return navigation_registry(request).get_navlist(request)
General Comments 0
You need to be logged in to leave comments. Login now