Show More
@@ -31,6 +31,7 b' from rhodecode.lib.utils2 import safe_un' | |||||
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_group import UserGroupModel |
|
35 | from rhodecode.model.user_group import UserGroupModel | |
35 |
|
36 | |||
36 | log = logging.getLogger(__name__) |
|
37 | log = logging.getLogger(__name__) | |
@@ -56,8 +57,7 b' class HomeView(BaseAppView):' | |||||
56 | log.debug('generating user list, query:%s, active:%s, with_groups:%s', |
|
57 | log.debug('generating user list, query:%s, active:%s, with_groups:%s', | |
57 | query, active, include_groups) |
|
58 | query, active, include_groups) | |
58 |
|
59 | |||
59 | repo_model = RepoModel() |
|
60 | _users = UserModel().get_users( | |
60 | _users = repo_model.get_users( |
|
|||
61 | name_contains=query, only_active=active) |
|
61 | name_contains=query, only_active=active) | |
62 |
|
62 | |||
63 | if include_groups: |
|
63 | if include_groups: |
@@ -30,7 +30,6 b' import time' | |||||
30 | import traceback |
|
30 | import traceback | |
31 | from datetime import datetime, timedelta |
|
31 | from datetime import datetime, timedelta | |
32 |
|
32 | |||
33 | from sqlalchemy.sql.expression import true, or_ |
|
|||
34 | from zope.cachedescriptors.property import Lazy as LazyProperty |
|
33 | from zope.cachedescriptors.property import Lazy as LazyProperty | |
35 |
|
34 | |||
36 | from rhodecode import events |
|
35 | from rhodecode import events | |
@@ -157,42 +156,6 b' class RepoModel(BaseModel):' | |||||
157 | return h.url('summary_home', repo_name=safe_str(repo.repo_name), |
|
156 | return h.url('summary_home', repo_name=safe_str(repo.repo_name), | |
158 | qualified=True) |
|
157 | qualified=True) | |
159 |
|
158 | |||
160 | def get_users(self, name_contains=None, limit=20, only_active=True): |
|
|||
161 |
|
||||
162 | # TODO: mikhail: move this method to the UserModel. |
|
|||
163 | query = self.sa.query(User) |
|
|||
164 | if only_active: |
|
|||
165 | query = query.filter(User.active == true()) |
|
|||
166 |
|
||||
167 | if name_contains: |
|
|||
168 | ilike_expression = u'%{}%'.format(safe_unicode(name_contains)) |
|
|||
169 | query = query.filter( |
|
|||
170 | or_( |
|
|||
171 | User.name.ilike(ilike_expression), |
|
|||
172 | User.lastname.ilike(ilike_expression), |
|
|||
173 | User.username.ilike(ilike_expression) |
|
|||
174 | ) |
|
|||
175 | ) |
|
|||
176 | query = query.limit(limit) |
|
|||
177 | users = query.all() |
|
|||
178 |
|
||||
179 | _users = [ |
|
|||
180 | { |
|
|||
181 | 'id': user.user_id, |
|
|||
182 | 'first_name': user.name, |
|
|||
183 | 'last_name': user.lastname, |
|
|||
184 | 'username': user.username, |
|
|||
185 | 'email': user.email, |
|
|||
186 | 'icon_link': h.gravatar_url(user.email, 30), |
|
|||
187 | 'value_display': h.person(user), |
|
|||
188 | 'value': user.username, |
|
|||
189 | 'value_type': 'user', |
|
|||
190 | 'active': user.active, |
|
|||
191 | } |
|
|||
192 | for user in users |
|
|||
193 | ] |
|
|||
194 | return _users |
|
|||
195 |
|
||||
196 | @classmethod |
|
159 | @classmethod | |
197 | def update_repoinfo(cls, repositories=None): |
|
160 | def update_repoinfo(cls, repositories=None): | |
198 | if not repositories: |
|
161 | if not repositories: |
@@ -65,6 +65,42 b' class UserModel(BaseModel):' | |||||
65 | def get_user(self, user): |
|
65 | def get_user(self, user): | |
66 | return self._get_user(user) |
|
66 | return self._get_user(user) | |
67 |
|
67 | |||
|
68 | def get_users(self, name_contains=None, limit=20, only_active=True): | |||
|
69 | import rhodecode.lib.helpers as h | |||
|
70 | ||||
|
71 | query = self.sa.query(User) | |||
|
72 | if only_active: | |||
|
73 | query = query.filter(User.active == true()) | |||
|
74 | ||||
|
75 | if name_contains: | |||
|
76 | ilike_expression = u'%{}%'.format(safe_unicode(name_contains)) | |||
|
77 | query = query.filter( | |||
|
78 | or_( | |||
|
79 | User.name.ilike(ilike_expression), | |||
|
80 | User.lastname.ilike(ilike_expression), | |||
|
81 | User.username.ilike(ilike_expression) | |||
|
82 | ) | |||
|
83 | ) | |||
|
84 | query = query.limit(limit) | |||
|
85 | users = query.all() | |||
|
86 | ||||
|
87 | _users = [ | |||
|
88 | { | |||
|
89 | 'id': user.user_id, | |||
|
90 | 'first_name': user.name, | |||
|
91 | 'last_name': user.lastname, | |||
|
92 | 'username': user.username, | |||
|
93 | 'email': user.email, | |||
|
94 | 'icon_link': h.gravatar_url(user.email, 30), | |||
|
95 | 'value_display': h.person(user), | |||
|
96 | 'value': user.username, | |||
|
97 | 'value_type': 'user', | |||
|
98 | 'active': user.active, | |||
|
99 | } | |||
|
100 | for user in users | |||
|
101 | ] | |||
|
102 | return _users | |||
|
103 | ||||
68 | def get_by_username(self, username, cache=False, case_insensitive=False): |
|
104 | def get_by_username(self, username, cache=False, case_insensitive=False): | |
69 |
|
105 | |||
70 | if case_insensitive: |
|
106 | if case_insensitive: |
@@ -169,84 +169,3 b' class TestRepoModel(object):' | |||||
169 | repo.update_commit_cache(config=config) |
|
169 | repo.update_commit_cache(config=config) | |
170 | scm.assert_called_with( |
|
170 | scm.assert_called_with( | |
171 | cache=False, config=config) |
|
171 | cache=False, config=config) | |
172 |
|
||||
173 |
|
||||
174 | class TestGetUsers(object): |
|
|||
175 | def test_returns_active_users(self, backend, user_util): |
|
|||
176 | for i in range(4): |
|
|||
177 | is_active = i % 2 == 0 |
|
|||
178 | user_util.create_user(active=is_active, lastname='Fake user') |
|
|||
179 |
|
||||
180 | with mock.patch('rhodecode.lib.helpers.gravatar_url'): |
|
|||
181 | users = RepoModel().get_users() |
|
|||
182 | fake_users = [u for u in users if u['last_name'] == 'Fake user'] |
|
|||
183 | assert len(fake_users) == 2 |
|
|||
184 |
|
||||
185 | expected_keys = ( |
|
|||
186 | 'id', 'first_name', 'last_name', 'username', 'icon_link', |
|
|||
187 | 'value_display', 'value', 'value_type') |
|
|||
188 | for user in users: |
|
|||
189 | assert user['value_type'] is 'user' |
|
|||
190 | for key in expected_keys: |
|
|||
191 | assert key in user |
|
|||
192 |
|
||||
193 | def test_returns_user_filtered_by_last_name(self, backend, user_util): |
|
|||
194 | keywords = ('aBc', u'ΓΌnicode') |
|
|||
195 | for keyword in keywords: |
|
|||
196 | for i in range(2): |
|
|||
197 | user_util.create_user( |
|
|||
198 | active=True, lastname=u'Fake {} user'.format(keyword)) |
|
|||
199 |
|
||||
200 | with mock.patch('rhodecode.lib.helpers.gravatar_url'): |
|
|||
201 | keyword = keywords[1].lower() |
|
|||
202 | users = RepoModel().get_users(name_contains=keyword) |
|
|||
203 |
|
||||
204 | fake_users = [u for u in users if u['last_name'].startswith('Fake')] |
|
|||
205 | assert len(fake_users) == 2 |
|
|||
206 | for user in fake_users: |
|
|||
207 | assert user['last_name'] == safe_unicode('Fake ΓΌnicode user') |
|
|||
208 |
|
||||
209 | def test_returns_user_filtered_by_first_name(self, backend, user_util): |
|
|||
210 | created_users = [] |
|
|||
211 | keywords = ('aBc', u'ΓΌnicode') |
|
|||
212 | for keyword in keywords: |
|
|||
213 | for i in range(2): |
|
|||
214 | created_users.append(user_util.create_user( |
|
|||
215 | active=True, lastname='Fake user', |
|
|||
216 | firstname=u'Fake {} user'.format(keyword))) |
|
|||
217 |
|
||||
218 | keyword = keywords[1].lower() |
|
|||
219 | with mock.patch('rhodecode.lib.helpers.gravatar_url'): |
|
|||
220 | users = RepoModel().get_users(name_contains=keyword) |
|
|||
221 |
|
||||
222 | fake_users = [u for u in users if u['last_name'].startswith('Fake')] |
|
|||
223 | assert len(fake_users) == 2 |
|
|||
224 | for user in fake_users: |
|
|||
225 | assert user['first_name'] == safe_unicode('Fake ΓΌnicode user') |
|
|||
226 |
|
||||
227 | def test_returns_user_filtered_by_username(self, backend, user_util): |
|
|||
228 | created_users = [] |
|
|||
229 | for i in range(5): |
|
|||
230 | created_users.append(user_util.create_user( |
|
|||
231 | active=True, lastname='Fake user')) |
|
|||
232 |
|
||||
233 | user_filter = created_users[-1].username[-2:] |
|
|||
234 | with mock.patch('rhodecode.lib.helpers.gravatar_url'): |
|
|||
235 | users = RepoModel().get_users(name_contains=user_filter) |
|
|||
236 |
|
||||
237 | fake_users = [u for u in users if u['last_name'].startswith('Fake')] |
|
|||
238 | assert len(fake_users) == 1 |
|
|||
239 | assert fake_users[0]['username'] == created_users[-1].username |
|
|||
240 |
|
||||
241 | def test_returns_limited_user_list(self, backend, user_util): |
|
|||
242 | created_users = [] |
|
|||
243 | for i in range(5): |
|
|||
244 | created_users.append(user_util.create_user( |
|
|||
245 | active=True, lastname='Fake user')) |
|
|||
246 |
|
||||
247 | with mock.patch('rhodecode.lib.helpers.gravatar_url'): |
|
|||
248 | users = RepoModel().get_users(name_contains='Fake', limit=3) |
|
|||
249 |
|
||||
250 | fake_users = [u for u in users if u['last_name'].startswith('Fake')] |
|
|||
251 | assert len(fake_users) == 3 |
|
|||
252 |
|
@@ -19,10 +19,11 b'' | |||||
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 pytest |
|
21 | import pytest | |
22 | from sqlalchemy.sql.expression import true |
|
22 | import mock | |
23 |
|
23 | |||
24 | from rhodecode.model.db import User, UserGroup, UserGroupMember, UserEmailMap,\ |
|
24 | from rhodecode.lib.utils2 import safe_unicode | |
25 | Permission, UserIpMap |
|
25 | from rhodecode.model.db import ( | |
|
26 | true, User, UserGroup, UserGroupMember, UserEmailMap, Permission, UserIpMap) | |||
26 | from rhodecode.model.meta import Session |
|
27 | from rhodecode.model.meta import Session | |
27 | from rhodecode.model.user import UserModel |
|
28 | from rhodecode.model.user import UserModel | |
28 | from rhodecode.model.user_group import UserGroupModel |
|
29 | from rhodecode.model.user_group import UserGroupModel | |
@@ -33,6 +34,86 b' from rhodecode.tests.fixture import Fixt' | |||||
33 | fixture = Fixture() |
|
34 | fixture = Fixture() | |
34 |
|
35 | |||
35 |
|
36 | |||
|
37 | class TestGetUsers(object): | |||
|
38 | def test_returns_active_users(self, backend, user_util): | |||
|
39 | for i in range(4): | |||
|
40 | is_active = i % 2 == 0 | |||
|
41 | user_util.create_user(active=is_active, lastname='Fake user') | |||
|
42 | ||||
|
43 | with mock.patch('rhodecode.lib.helpers.gravatar_url'): | |||
|
44 | users = UserModel().get_users() | |||
|
45 | fake_users = [u for u in users if u['last_name'] == 'Fake user'] | |||
|
46 | assert len(fake_users) == 2 | |||
|
47 | ||||
|
48 | expected_keys = ( | |||
|
49 | 'id', 'first_name', 'last_name', 'username', 'icon_link', | |||
|
50 | 'value_display', 'value', 'value_type') | |||
|
51 | for user in users: | |||
|
52 | assert user['value_type'] is 'user' | |||
|
53 | for key in expected_keys: | |||
|
54 | assert key in user | |||
|
55 | ||||
|
56 | def test_returns_user_filtered_by_last_name(self, backend, user_util): | |||
|
57 | keywords = ('aBc', u'ΓΌnicode') | |||
|
58 | for keyword in keywords: | |||
|
59 | for i in range(2): | |||
|
60 | user_util.create_user( | |||
|
61 | active=True, lastname=u'Fake {} user'.format(keyword)) | |||
|
62 | ||||
|
63 | with mock.patch('rhodecode.lib.helpers.gravatar_url'): | |||
|
64 | keyword = keywords[1].lower() | |||
|
65 | users = UserModel().get_users(name_contains=keyword) | |||
|
66 | ||||
|
67 | fake_users = [u for u in users if u['last_name'].startswith('Fake')] | |||
|
68 | assert len(fake_users) == 2 | |||
|
69 | for user in fake_users: | |||
|
70 | assert user['last_name'] == safe_unicode('Fake ΓΌnicode user') | |||
|
71 | ||||
|
72 | def test_returns_user_filtered_by_first_name(self, backend, user_util): | |||
|
73 | created_users = [] | |||
|
74 | keywords = ('aBc', u'ΓΌnicode') | |||
|
75 | for keyword in keywords: | |||
|
76 | for i in range(2): | |||
|
77 | created_users.append(user_util.create_user( | |||
|
78 | active=True, lastname='Fake user', | |||
|
79 | firstname=u'Fake {} user'.format(keyword))) | |||
|
80 | ||||
|
81 | keyword = keywords[1].lower() | |||
|
82 | with mock.patch('rhodecode.lib.helpers.gravatar_url'): | |||
|
83 | users = UserModel().get_users(name_contains=keyword) | |||
|
84 | ||||
|
85 | fake_users = [u for u in users if u['last_name'].startswith('Fake')] | |||
|
86 | assert len(fake_users) == 2 | |||
|
87 | for user in fake_users: | |||
|
88 | assert user['first_name'] == safe_unicode('Fake ΓΌnicode user') | |||
|
89 | ||||
|
90 | def test_returns_user_filtered_by_username(self, backend, user_util): | |||
|
91 | created_users = [] | |||
|
92 | for i in range(5): | |||
|
93 | created_users.append(user_util.create_user( | |||
|
94 | active=True, lastname='Fake user')) | |||
|
95 | ||||
|
96 | user_filter = created_users[-1].username[-2:] | |||
|
97 | with mock.patch('rhodecode.lib.helpers.gravatar_url'): | |||
|
98 | users = UserModel().get_users(name_contains=user_filter) | |||
|
99 | ||||
|
100 | fake_users = [u for u in users if u['last_name'].startswith('Fake')] | |||
|
101 | assert len(fake_users) == 1 | |||
|
102 | assert fake_users[0]['username'] == created_users[-1].username | |||
|
103 | ||||
|
104 | def test_returns_limited_user_list(self, backend, user_util): | |||
|
105 | created_users = [] | |||
|
106 | for i in range(5): | |||
|
107 | created_users.append(user_util.create_user( | |||
|
108 | active=True, lastname='Fake user')) | |||
|
109 | ||||
|
110 | with mock.patch('rhodecode.lib.helpers.gravatar_url'): | |||
|
111 | users = UserModel().get_users(name_contains='Fake', limit=3) | |||
|
112 | ||||
|
113 | fake_users = [u for u in users if u['last_name'].startswith('Fake')] | |||
|
114 | assert len(fake_users) == 3 | |||
|
115 | ||||
|
116 | ||||
36 | @pytest.fixture |
|
117 | @pytest.fixture | |
37 | def test_user(request, pylonsapp): |
|
118 | def test_user(request, pylonsapp): | |
38 | usr = UserModel().create_or_update( |
|
119 | usr = UserModel().create_or_update( |
General Comments 0
You need to be logged in to leave comments.
Login now