##// END OF EJS Templates
quick-filter: make sure we always apply IN filter query. Otherwise we can...
marcink -
r2167:23aaeb72 default
parent child Browse files
Show More

The requested changes are too big and content was truncated. Show full diff

@@ -1,322 +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 cache=False, name_filter=name_contains)
107 cache=False, name_filter=name_contains) or [-1]
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 133 'obj': {'repo_type': obj.repo_type, 'private': obj.private,
134 134 'repo_id': obj.repo_id},
135 135 'url': h.route_path('repo_summary', repo_name=obj.repo_name)
136 136 }
137 137 for obj in acl_repo_iter]
138 138
139 139 def _get_repo_group_list(self, name_contains=None, limit=20):
140 140 allowed_ids = self._rhodecode_user.repo_group_acl_ids(
141 141 ['group.read', 'group.write', 'group.admin'],
142 cache=False, name_filter=name_contains)
142 cache=False, name_filter=name_contains) or [-1]
143 143
144 144 query = RepoGroup.query()\
145 145 .order_by(func.length(RepoGroup.group_name))\
146 146 .order_by(RepoGroup.group_name) \
147 147 .filter(or_(
148 148 # generate multiple IN to fix limitation problems
149 149 *in_filter_generator(RepoGroup.group_id, allowed_ids)
150 150 ))
151 151
152 152 if name_contains:
153 153 ilike_expression = u'%{}%'.format(safe_unicode(name_contains))
154 154 query = query.filter(
155 155 RepoGroup.group_name.ilike(ilike_expression))
156 156 query = query.limit(limit)
157 157
158 158 acl_repo_iter = query
159 159
160 160 return [
161 161 {
162 162 'id': obj.group_name,
163 163 'text': obj.group_name,
164 164 'type': 'group',
165 165 'obj': {},
166 166 'url': h.route_path(
167 167 'repo_group_home', repo_group_name=obj.group_name)
168 168 }
169 169 for obj in acl_repo_iter]
170 170
171 171 def _get_hash_commit_list(self, auth_user, query=None):
172 172 if not query or len(query) < 3:
173 173 return []
174 174
175 175 commit_hashes = re.compile('(?:commit:)([0-9a-f]{2,40})').findall(query)
176 176
177 177 if len(commit_hashes) != 1:
178 178 return []
179 179
180 180 commit_hash_prefix = commit_hashes[0]
181 181
182 182 searcher = searcher_from_config(self.request.registry.settings)
183 183 result = searcher.search(
184 184 'commit_id:%s*' % commit_hash_prefix, 'commit', auth_user,
185 185 raise_on_exc=False)
186 186
187 187 return [
188 188 {
189 189 'id': entry['commit_id'],
190 190 'text': entry['commit_id'],
191 191 'type': 'commit',
192 192 'obj': {'repo': entry['repository']},
193 193 'url': h.route_path(
194 194 'repo_commit',
195 195 repo_name=entry['repository'], commit_id=entry['commit_id'])
196 196 }
197 197 for entry in result['results']]
198 198
199 199 @LoginRequired()
200 200 @view_config(
201 201 route_name='repo_list_data', request_method='GET',
202 202 renderer='json_ext', xhr=True)
203 203 def repo_list_data(self):
204 204 _ = self.request.translate
205 205
206 206 query = self.request.GET.get('query')
207 207 repo_type = self.request.GET.get('repo_type')
208 208 log.debug('generating repo list, query:%s, repo_type:%s',
209 209 query, repo_type)
210 210
211 211 res = []
212 212 repos = self._get_repo_list(query, repo_type=repo_type)
213 213 if repos:
214 214 res.append({
215 215 'text': _('Repositories'),
216 216 'children': repos
217 217 })
218 218
219 219 data = {
220 220 'more': False,
221 221 'results': res
222 222 }
223 223 return data
224 224
225 225 @LoginRequired()
226 226 @view_config(
227 227 route_name='goto_switcher_data', request_method='GET',
228 228 renderer='json_ext', xhr=True)
229 229 def goto_switcher_data(self):
230 230 c = self.load_default_context()
231 231
232 232 _ = self.request.translate
233 233
234 234 query = self.request.GET.get('query')
235 235 log.debug('generating goto switcher list, query %s', query)
236 236
237 237 res = []
238 238 repo_groups = self._get_repo_group_list(query)
239 239 if repo_groups:
240 240 res.append({
241 241 'text': _('Groups'),
242 242 'children': repo_groups
243 243 })
244 244
245 245 repos = self._get_repo_list(query)
246 246 if repos:
247 247 res.append({
248 248 'text': _('Repositories'),
249 249 'children': repos
250 250 })
251 251
252 252 commits = self._get_hash_commit_list(c.auth_user, query)
253 253 if commits:
254 254 unique_repos = {}
255 255 for commit in commits:
256 256 unique_repos.setdefault(commit['obj']['repo'], []
257 257 ).append(commit)
258 258
259 259 for repo in unique_repos:
260 260 res.append({
261 261 'text': _('Commits in %(repo)s') % {'repo': repo},
262 262 'children': unique_repos[repo]
263 263 })
264 264
265 265 data = {
266 266 'more': False,
267 267 'results': res
268 268 }
269 269 return data
270 270
271 271 def _get_groups_and_repos(self, repo_group_id=None):
272 272 # repo groups groups
273 273 repo_group_list = RepoGroup.get_all_repo_groups(group_id=repo_group_id)
274 274 _perms = ['group.read', 'group.write', 'group.admin']
275 275 repo_group_list_acl = RepoGroupList(repo_group_list, perm_set=_perms)
276 276 repo_group_data = RepoGroupModel().get_repo_groups_as_dict(
277 277 repo_group_list=repo_group_list_acl, admin=False)
278 278
279 279 # repositories
280 280 repo_list = Repository.get_all_repos(group_id=repo_group_id)
281 281 _perms = ['repository.read', 'repository.write', 'repository.admin']
282 282 repo_list_acl = RepoList(repo_list, perm_set=_perms)
283 283 repo_data = RepoModel().get_repos_as_dict(
284 284 repo_list=repo_list_acl, admin=False)
285 285
286 286 return repo_data, repo_group_data
287 287
288 288 @LoginRequired()
289 289 @view_config(
290 290 route_name='home', request_method='GET',
291 291 renderer='rhodecode:templates/index.mako')
292 292 def main_page(self):
293 293 c = self.load_default_context()
294 294 c.repo_group = None
295 295
296 296 repo_data, repo_group_data = self._get_groups_and_repos()
297 297 # json used to render the grids
298 298 c.repos_data = json.dumps(repo_data)
299 299 c.repo_groups_data = json.dumps(repo_group_data)
300 300
301 301 return self._get_template_context(c)
302 302
303 303 @LoginRequired()
304 304 @HasRepoGroupPermissionAnyDecorator(
305 305 'group.read', 'group.write', 'group.admin')
306 306 @view_config(
307 307 route_name='repo_group_home', request_method='GET',
308 308 renderer='rhodecode:templates/index_repo_group.mako')
309 309 @view_config(
310 310 route_name='repo_group_home_slash', request_method='GET',
311 311 renderer='rhodecode:templates/index_repo_group.mako')
312 312 def repo_group_main_page(self):
313 313 c = self.load_default_context()
314 314 c.repo_group = self.request.db_repo_group
315 315 repo_data, repo_group_data = self._get_groups_and_repos(
316 316 c.repo_group.group_id)
317 317
318 318 # json used to render the grids
319 319 c.repos_data = json.dumps(repo_data)
320 320 c.repo_groups_data = json.dumps(repo_group_data)
321 321
322 322 return self._get_template_context(c)
1 NO CONTENT: modified file
The requested commit or file is too big and content was truncated. Show full diff
General Comments 0
You need to be logged in to leave comments. Login now