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