##// END OF EJS Templates
user-autocomplete: allow skipping default user.
marcink -
r1768:ea751575 default
parent child Browse files
Show More
@@ -1,241 +1,248 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 LoginRequired, NotAnonymous
29 29 from rhodecode.lib.index import searcher_from_config
30 30 from rhodecode.lib.utils2 import safe_unicode, str2bool
31 31 from rhodecode.model.db import func, Repository, RepoGroup
32 32 from rhodecode.model.repo import RepoModel
33 33 from rhodecode.model.scm import ScmModel
34 34 from rhodecode.model.user import UserModel
35 35 from rhodecode.model.user_group import UserGroupModel
36 36
37 37 log = logging.getLogger(__name__)
38 38
39 39
40 40 class HomeView(BaseAppView):
41 41
42 42 def load_default_context(self):
43 43 c = self._get_local_tmpl_context()
44 44 c.user = c.auth_user.get_instance()
45 45 self._register_global_c(c)
46 46 return c
47 47
48 48 @LoginRequired()
49 49 @view_config(
50 50 route_name='user_autocomplete_data', request_method='GET',
51 51 renderer='json_ext', xhr=True)
52 52 def user_autocomplete_data(self):
53 53 query = self.request.GET.get('query')
54 54 active = str2bool(self.request.GET.get('active') or True)
55 55 include_groups = str2bool(self.request.GET.get('user_groups'))
56 56 expand_groups = str2bool(self.request.GET.get('user_groups_expand'))
57 skip_default_user = str2bool(self.request.GET.get('skip_default_user'))
57 58
58 59 log.debug('generating user list, query:%s, active:%s, with_groups:%s',
59 60 query, active, include_groups)
60 61
61 62 _users = UserModel().get_users(
62 63 name_contains=query, only_active=active)
63 64
65 def maybe_skip_default_user(usr):
66 if skip_default_user and usr['username'] == UserModel.cls.DEFAULT_USER:
67 return False
68 return True
69 _users = filter(maybe_skip_default_user, _users)
70
64 71 if include_groups:
65 72 # extend with user groups
66 73 _user_groups = UserGroupModel().get_user_groups(
67 74 name_contains=query, only_active=active,
68 75 expand_groups=expand_groups)
69 76 _users = _users + _user_groups
70 77
71 78 return {'suggestions': _users}
72 79
73 80 @LoginRequired()
74 81 @NotAnonymous()
75 82 @view_config(
76 83 route_name='user_group_autocomplete_data', request_method='GET',
77 84 renderer='json_ext', xhr=True)
78 85 def user_group_autocomplete_data(self):
79 86 query = self.request.GET.get('query')
80 87 active = str2bool(self.request.GET.get('active') or True)
81 88 expand_groups = str2bool(self.request.GET.get('user_groups_expand'))
82 89
83 90 log.debug('generating user group list, query:%s, active:%s',
84 91 query, active)
85 92
86 93 _user_groups = UserGroupModel().get_user_groups(
87 94 name_contains=query, only_active=active,
88 95 expand_groups=expand_groups)
89 96 _user_groups = _user_groups
90 97
91 98 return {'suggestions': _user_groups}
92 99
93 100 def _get_repo_list(self, name_contains=None, repo_type=None, limit=20):
94 101 query = Repository.query()\
95 102 .order_by(func.length(Repository.repo_name))\
96 103 .order_by(Repository.repo_name)
97 104
98 105 if repo_type:
99 106 query = query.filter(Repository.repo_type == repo_type)
100 107
101 108 if name_contains:
102 109 ilike_expression = u'%{}%'.format(safe_unicode(name_contains))
103 110 query = query.filter(
104 111 Repository.repo_name.ilike(ilike_expression))
105 112 query = query.limit(limit)
106 113
107 114 all_repos = query.all()
108 115 # permission checks are inside this function
109 116 repo_iter = ScmModel().get_repos(all_repos)
110 117 return [
111 118 {
112 119 'id': obj['name'],
113 120 'text': obj['name'],
114 121 'type': 'repo',
115 122 'obj': obj['dbrepo'],
116 123 'url': h.url('summary_home', repo_name=obj['name'])
117 124 }
118 125 for obj in repo_iter]
119 126
120 127 def _get_repo_group_list(self, name_contains=None, limit=20):
121 128 query = RepoGroup.query()\
122 129 .order_by(func.length(RepoGroup.group_name))\
123 130 .order_by(RepoGroup.group_name)
124 131
125 132 if name_contains:
126 133 ilike_expression = u'%{}%'.format(safe_unicode(name_contains))
127 134 query = query.filter(
128 135 RepoGroup.group_name.ilike(ilike_expression))
129 136 query = query.limit(limit)
130 137
131 138 all_groups = query.all()
132 139 repo_groups_iter = ScmModel().get_repo_groups(all_groups)
133 140 return [
134 141 {
135 142 'id': obj.group_name,
136 143 'text': obj.group_name,
137 144 'type': 'group',
138 145 'obj': {},
139 146 'url': h.url('repo_group_home', group_name=obj.group_name)
140 147 }
141 148 for obj in repo_groups_iter]
142 149
143 150 def _get_hash_commit_list(self, auth_user, hash_starts_with=None):
144 151 if not hash_starts_with or len(hash_starts_with) < 3:
145 152 return []
146 153
147 154 commit_hashes = re.compile('([0-9a-f]{2,40})').findall(hash_starts_with)
148 155
149 156 if len(commit_hashes) != 1:
150 157 return []
151 158
152 159 commit_hash_prefix = commit_hashes[0]
153 160
154 161 searcher = searcher_from_config(self.request.registry.settings)
155 162 result = searcher.search(
156 163 'commit_id:%s*' % commit_hash_prefix, 'commit', auth_user,
157 164 raise_on_exc=False)
158 165
159 166 return [
160 167 {
161 168 'id': entry['commit_id'],
162 169 'text': entry['commit_id'],
163 170 'type': 'commit',
164 171 'obj': {'repo': entry['repository']},
165 172 'url': h.url('changeset_home',
166 173 repo_name=entry['repository'],
167 174 revision=entry['commit_id'])
168 175 }
169 176 for entry in result['results']]
170 177
171 178 @LoginRequired()
172 179 @view_config(
173 180 route_name='repo_list_data', request_method='GET',
174 181 renderer='json_ext', xhr=True)
175 182 def repo_list_data(self):
176 183 _ = self.request.translate
177 184
178 185 query = self.request.GET.get('query')
179 186 repo_type = self.request.GET.get('repo_type')
180 187 log.debug('generating repo list, query:%s, repo_type:%s',
181 188 query, repo_type)
182 189
183 190 res = []
184 191 repos = self._get_repo_list(query, repo_type=repo_type)
185 192 if repos:
186 193 res.append({
187 194 'text': _('Repositories'),
188 195 'children': repos
189 196 })
190 197
191 198 data = {
192 199 'more': False,
193 200 'results': res
194 201 }
195 202 return data
196 203
197 204 @LoginRequired()
198 205 @view_config(
199 206 route_name='goto_switcher_data', request_method='GET',
200 207 renderer='json_ext', xhr=True)
201 208 def goto_switcher_data(self):
202 209 c = self.load_default_context()
203 210
204 211 _ = self.request.translate
205 212
206 213 query = self.request.GET.get('query')
207 214 log.debug('generating goto switcher list, query %s', query)
208 215
209 216 res = []
210 217 repo_groups = self._get_repo_group_list(query)
211 218 if repo_groups:
212 219 res.append({
213 220 'text': _('Groups'),
214 221 'children': repo_groups
215 222 })
216 223
217 224 repos = self._get_repo_list(query)
218 225 if repos:
219 226 res.append({
220 227 'text': _('Repositories'),
221 228 'children': repos
222 229 })
223 230
224 231 commits = self._get_hash_commit_list(c.auth_user, query)
225 232 if commits:
226 233 unique_repos = {}
227 234 for commit in commits:
228 235 unique_repos.setdefault(commit['obj']['repo'], []
229 236 ).append(commit)
230 237
231 238 for repo in unique_repos:
232 239 res.append({
233 240 'text': _('Commits in %(repo)s') % {'repo': repo},
234 241 'children': unique_repos[repo]
235 242 })
236 243
237 244 data = {
238 245 'more': False,
239 246 'results': res
240 247 }
241 248 return data
General Comments 0
You need to be logged in to leave comments. Login now