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