Show More
@@ -1,321 +1,322 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 | import re |
|
22 | 22 | import logging |
|
23 | 23 | |
|
24 | 24 | from pyramid.view import view_config |
|
25 | 25 | |
|
26 | 26 | from rhodecode.apps._base import BaseAppView |
|
27 | 27 | from rhodecode.lib import helpers as h |
|
28 | 28 | from rhodecode.lib.auth import ( |
|
29 | 29 | LoginRequired, NotAnonymous, HasRepoGroupPermissionAnyDecorator) |
|
30 | 30 | from rhodecode.lib.index import searcher_from_config |
|
31 | 31 | from rhodecode.lib.utils2 import safe_unicode, str2bool |
|
32 | 32 | from rhodecode.lib.ext_json import json |
|
33 | 33 | from rhodecode.model.db import ( |
|
34 | 34 | func, or_, in_filter_generator, Repository, RepoGroup) |
|
35 | 35 | from rhodecode.model.repo import RepoModel |
|
36 | 36 | from rhodecode.model.repo_group import RepoGroupModel |
|
37 | 37 | from rhodecode.model.scm import RepoGroupList, RepoList |
|
38 | 38 | from rhodecode.model.user import UserModel |
|
39 | 39 | from rhodecode.model.user_group import UserGroupModel |
|
40 | 40 | |
|
41 | 41 | log = logging.getLogger(__name__) |
|
42 | 42 | |
|
43 | 43 | |
|
44 | 44 | class HomeView(BaseAppView): |
|
45 | 45 | |
|
46 | 46 | def load_default_context(self): |
|
47 | 47 | c = self._get_local_tmpl_context() |
|
48 | 48 | c.user = c.auth_user.get_instance() |
|
49 | 49 | self._register_global_c(c) |
|
50 | 50 | return c |
|
51 | 51 | |
|
52 | 52 | @LoginRequired() |
|
53 | 53 | @view_config( |
|
54 | 54 | route_name='user_autocomplete_data', request_method='GET', |
|
55 | 55 | renderer='json_ext', xhr=True) |
|
56 | 56 | def user_autocomplete_data(self): |
|
57 | 57 | query = self.request.GET.get('query') |
|
58 | 58 | active = str2bool(self.request.GET.get('active') or True) |
|
59 | 59 | include_groups = str2bool(self.request.GET.get('user_groups')) |
|
60 | 60 | expand_groups = str2bool(self.request.GET.get('user_groups_expand')) |
|
61 | 61 | skip_default_user = str2bool(self.request.GET.get('skip_default_user')) |
|
62 | 62 | |
|
63 | 63 | log.debug('generating user list, query:%s, active:%s, with_groups:%s', |
|
64 | 64 | query, active, include_groups) |
|
65 | 65 | |
|
66 | 66 | _users = UserModel().get_users( |
|
67 | 67 | name_contains=query, only_active=active) |
|
68 | 68 | |
|
69 | 69 | def maybe_skip_default_user(usr): |
|
70 | 70 | if skip_default_user and usr['username'] == UserModel.cls.DEFAULT_USER: |
|
71 | 71 | return False |
|
72 | 72 | return True |
|
73 | 73 | _users = filter(maybe_skip_default_user, _users) |
|
74 | 74 | |
|
75 | 75 | if include_groups: |
|
76 | 76 | # extend with user groups |
|
77 | 77 | _user_groups = UserGroupModel().get_user_groups( |
|
78 | 78 | name_contains=query, only_active=active, |
|
79 | 79 | expand_groups=expand_groups) |
|
80 | 80 | _users = _users + _user_groups |
|
81 | 81 | |
|
82 | 82 | return {'suggestions': _users} |
|
83 | 83 | |
|
84 | 84 | @LoginRequired() |
|
85 | 85 | @NotAnonymous() |
|
86 | 86 | @view_config( |
|
87 | 87 | route_name='user_group_autocomplete_data', request_method='GET', |
|
88 | 88 | renderer='json_ext', xhr=True) |
|
89 | 89 | def user_group_autocomplete_data(self): |
|
90 | 90 | query = self.request.GET.get('query') |
|
91 | 91 | active = str2bool(self.request.GET.get('active') or True) |
|
92 | 92 | expand_groups = str2bool(self.request.GET.get('user_groups_expand')) |
|
93 | 93 | |
|
94 | 94 | log.debug('generating user group list, query:%s, active:%s', |
|
95 | 95 | query, active) |
|
96 | 96 | |
|
97 | 97 | _user_groups = UserGroupModel().get_user_groups( |
|
98 | 98 | name_contains=query, only_active=active, |
|
99 | 99 | expand_groups=expand_groups) |
|
100 | 100 | _user_groups = _user_groups |
|
101 | 101 | |
|
102 | 102 | return {'suggestions': _user_groups} |
|
103 | 103 | |
|
104 | 104 | def _get_repo_list(self, name_contains=None, repo_type=None, limit=20): |
|
105 | 105 | allowed_ids = self._rhodecode_user.repo_acl_ids( |
|
106 | 106 | ['repository.read', 'repository.write', 'repository.admin'], |
|
107 | 107 | cache=False, name_filter=name_contains) |
|
108 | 108 | |
|
109 | 109 | query = Repository.query()\ |
|
110 | 110 | .order_by(func.length(Repository.repo_name))\ |
|
111 | 111 | .order_by(Repository.repo_name)\ |
|
112 | 112 | .filter(or_( |
|
113 | 113 | # generate multiple IN to fix limitation problems |
|
114 | 114 | *in_filter_generator(Repository.repo_id, allowed_ids) |
|
115 | 115 | )) |
|
116 | 116 | |
|
117 | 117 | if repo_type: |
|
118 | 118 | query = query.filter(Repository.repo_type == repo_type) |
|
119 | 119 | |
|
120 | 120 | if name_contains: |
|
121 | 121 | ilike_expression = u'%{}%'.format(safe_unicode(name_contains)) |
|
122 | 122 | query = query.filter( |
|
123 | 123 | Repository.repo_name.ilike(ilike_expression)) |
|
124 | 124 | query = query.limit(limit) |
|
125 | 125 | |
|
126 | 126 | acl_repo_iter = query |
|
127 | 127 | |
|
128 | 128 | return [ |
|
129 | 129 | { |
|
130 | 130 | 'id': obj.repo_name, |
|
131 | 131 | 'text': obj.repo_name, |
|
132 | 132 | 'type': 'repo', |
|
133 |
'obj': {'repo_type': obj.repo_type, 'private': obj.private |
|
|
133 | 'obj': {'repo_type': obj.repo_type, 'private': obj.private, | |
|
134 | 'repo_id': obj.repo_id}, | |
|
134 | 135 | 'url': h.route_path('repo_summary', repo_name=obj.repo_name) |
|
135 | 136 | } |
|
136 | 137 | for obj in acl_repo_iter] |
|
137 | 138 | |
|
138 | 139 | def _get_repo_group_list(self, name_contains=None, limit=20): |
|
139 | 140 | allowed_ids = self._rhodecode_user.repo_group_acl_ids( |
|
140 | 141 | ['group.read', 'group.write', 'group.admin'], |
|
141 | 142 | cache=False, name_filter=name_contains) |
|
142 | 143 | |
|
143 | 144 | query = RepoGroup.query()\ |
|
144 | 145 | .order_by(func.length(RepoGroup.group_name))\ |
|
145 | 146 | .order_by(RepoGroup.group_name) \ |
|
146 | 147 | .filter(or_( |
|
147 | 148 | # generate multiple IN to fix limitation problems |
|
148 | 149 | *in_filter_generator(RepoGroup.group_id, allowed_ids) |
|
149 | 150 | )) |
|
150 | 151 | |
|
151 | 152 | if name_contains: |
|
152 | 153 | ilike_expression = u'%{}%'.format(safe_unicode(name_contains)) |
|
153 | 154 | query = query.filter( |
|
154 | 155 | RepoGroup.group_name.ilike(ilike_expression)) |
|
155 | 156 | query = query.limit(limit) |
|
156 | 157 | |
|
157 | 158 | acl_repo_iter = query |
|
158 | 159 | |
|
159 | 160 | return [ |
|
160 | 161 | { |
|
161 | 162 | 'id': obj.group_name, |
|
162 | 163 | 'text': obj.group_name, |
|
163 | 164 | 'type': 'group', |
|
164 | 165 | 'obj': {}, |
|
165 | 166 | 'url': h.route_path( |
|
166 | 167 | 'repo_group_home', repo_group_name=obj.group_name) |
|
167 | 168 | } |
|
168 | 169 | for obj in acl_repo_iter] |
|
169 | 170 | |
|
170 | 171 | def _get_hash_commit_list(self, auth_user, query=None): |
|
171 | 172 | if not query or len(query) < 3: |
|
172 | 173 | return [] |
|
173 | 174 | |
|
174 | 175 | commit_hashes = re.compile('(?:commit:)([0-9a-f]{2,40})').findall(query) |
|
175 | 176 | |
|
176 | 177 | if len(commit_hashes) != 1: |
|
177 | 178 | return [] |
|
178 | 179 | |
|
179 | 180 | commit_hash_prefix = commit_hashes[0] |
|
180 | 181 | |
|
181 | 182 | searcher = searcher_from_config(self.request.registry.settings) |
|
182 | 183 | result = searcher.search( |
|
183 | 184 | 'commit_id:%s*' % commit_hash_prefix, 'commit', auth_user, |
|
184 | 185 | raise_on_exc=False) |
|
185 | 186 | |
|
186 | 187 | return [ |
|
187 | 188 | { |
|
188 | 189 | 'id': entry['commit_id'], |
|
189 | 190 | 'text': entry['commit_id'], |
|
190 | 191 | 'type': 'commit', |
|
191 | 192 | 'obj': {'repo': entry['repository']}, |
|
192 | 193 | 'url': h.route_path( |
|
193 | 194 | 'repo_commit', |
|
194 | 195 | repo_name=entry['repository'], commit_id=entry['commit_id']) |
|
195 | 196 | } |
|
196 | 197 | for entry in result['results']] |
|
197 | 198 | |
|
198 | 199 | @LoginRequired() |
|
199 | 200 | @view_config( |
|
200 | 201 | route_name='repo_list_data', request_method='GET', |
|
201 | 202 | renderer='json_ext', xhr=True) |
|
202 | 203 | def repo_list_data(self): |
|
203 | 204 | _ = self.request.translate |
|
204 | 205 | |
|
205 | 206 | query = self.request.GET.get('query') |
|
206 | 207 | repo_type = self.request.GET.get('repo_type') |
|
207 | 208 | log.debug('generating repo list, query:%s, repo_type:%s', |
|
208 | 209 | query, repo_type) |
|
209 | 210 | |
|
210 | 211 | res = [] |
|
211 | 212 | repos = self._get_repo_list(query, repo_type=repo_type) |
|
212 | 213 | if repos: |
|
213 | 214 | res.append({ |
|
214 | 215 | 'text': _('Repositories'), |
|
215 | 216 | 'children': repos |
|
216 | 217 | }) |
|
217 | 218 | |
|
218 | 219 | data = { |
|
219 | 220 | 'more': False, |
|
220 | 221 | 'results': res |
|
221 | 222 | } |
|
222 | 223 | return data |
|
223 | 224 | |
|
224 | 225 | @LoginRequired() |
|
225 | 226 | @view_config( |
|
226 | 227 | route_name='goto_switcher_data', request_method='GET', |
|
227 | 228 | renderer='json_ext', xhr=True) |
|
228 | 229 | def goto_switcher_data(self): |
|
229 | 230 | c = self.load_default_context() |
|
230 | 231 | |
|
231 | 232 | _ = self.request.translate |
|
232 | 233 | |
|
233 | 234 | query = self.request.GET.get('query') |
|
234 | 235 | log.debug('generating goto switcher list, query %s', query) |
|
235 | 236 | |
|
236 | 237 | res = [] |
|
237 | 238 | repo_groups = self._get_repo_group_list(query) |
|
238 | 239 | if repo_groups: |
|
239 | 240 | res.append({ |
|
240 | 241 | 'text': _('Groups'), |
|
241 | 242 | 'children': repo_groups |
|
242 | 243 | }) |
|
243 | 244 | |
|
244 | 245 | repos = self._get_repo_list(query) |
|
245 | 246 | if repos: |
|
246 | 247 | res.append({ |
|
247 | 248 | 'text': _('Repositories'), |
|
248 | 249 | 'children': repos |
|
249 | 250 | }) |
|
250 | 251 | |
|
251 | 252 | commits = self._get_hash_commit_list(c.auth_user, query) |
|
252 | 253 | if commits: |
|
253 | 254 | unique_repos = {} |
|
254 | 255 | for commit in commits: |
|
255 | 256 | unique_repos.setdefault(commit['obj']['repo'], [] |
|
256 | 257 | ).append(commit) |
|
257 | 258 | |
|
258 | 259 | for repo in unique_repos: |
|
259 | 260 | res.append({ |
|
260 | 261 | 'text': _('Commits in %(repo)s') % {'repo': repo}, |
|
261 | 262 | 'children': unique_repos[repo] |
|
262 | 263 | }) |
|
263 | 264 | |
|
264 | 265 | data = { |
|
265 | 266 | 'more': False, |
|
266 | 267 | 'results': res |
|
267 | 268 | } |
|
268 | 269 | return data |
|
269 | 270 | |
|
270 | 271 | def _get_groups_and_repos(self, repo_group_id=None): |
|
271 | 272 | # repo groups groups |
|
272 | 273 | repo_group_list = RepoGroup.get_all_repo_groups(group_id=repo_group_id) |
|
273 | 274 | _perms = ['group.read', 'group.write', 'group.admin'] |
|
274 | 275 | repo_group_list_acl = RepoGroupList(repo_group_list, perm_set=_perms) |
|
275 | 276 | repo_group_data = RepoGroupModel().get_repo_groups_as_dict( |
|
276 | 277 | repo_group_list=repo_group_list_acl, admin=False) |
|
277 | 278 | |
|
278 | 279 | # repositories |
|
279 | 280 | repo_list = Repository.get_all_repos(group_id=repo_group_id) |
|
280 | 281 | _perms = ['repository.read', 'repository.write', 'repository.admin'] |
|
281 | 282 | repo_list_acl = RepoList(repo_list, perm_set=_perms) |
|
282 | 283 | repo_data = RepoModel().get_repos_as_dict( |
|
283 | 284 | repo_list=repo_list_acl, admin=False) |
|
284 | 285 | |
|
285 | 286 | return repo_data, repo_group_data |
|
286 | 287 | |
|
287 | 288 | @LoginRequired() |
|
288 | 289 | @view_config( |
|
289 | 290 | route_name='home', request_method='GET', |
|
290 | 291 | renderer='rhodecode:templates/index.mako') |
|
291 | 292 | def main_page(self): |
|
292 | 293 | c = self.load_default_context() |
|
293 | 294 | c.repo_group = None |
|
294 | 295 | |
|
295 | 296 | repo_data, repo_group_data = self._get_groups_and_repos() |
|
296 | 297 | # json used to render the grids |
|
297 | 298 | c.repos_data = json.dumps(repo_data) |
|
298 | 299 | c.repo_groups_data = json.dumps(repo_group_data) |
|
299 | 300 | |
|
300 | 301 | return self._get_template_context(c) |
|
301 | 302 | |
|
302 | 303 | @LoginRequired() |
|
303 | 304 | @HasRepoGroupPermissionAnyDecorator( |
|
304 | 305 | 'group.read', 'group.write', 'group.admin') |
|
305 | 306 | @view_config( |
|
306 | 307 | route_name='repo_group_home', request_method='GET', |
|
307 | 308 | renderer='rhodecode:templates/index_repo_group.mako') |
|
308 | 309 | @view_config( |
|
309 | 310 | route_name='repo_group_home_slash', request_method='GET', |
|
310 | 311 | renderer='rhodecode:templates/index_repo_group.mako') |
|
311 | 312 | def repo_group_main_page(self): |
|
312 | 313 | c = self.load_default_context() |
|
313 | 314 | c.repo_group = self.request.db_repo_group |
|
314 | 315 | repo_data, repo_group_data = self._get_groups_and_repos( |
|
315 | 316 | c.repo_group.group_id) |
|
316 | 317 | |
|
317 | 318 | # json used to render the grids |
|
318 | 319 | c.repos_data = json.dumps(repo_data) |
|
319 | 320 | c.repo_groups_data = json.dumps(repo_group_data) |
|
320 | 321 | |
|
321 | 322 | return self._get_template_context(c) |
General Comments 0
You need to be logged in to leave comments.
Login now