# HG changeset patch # User Marcin Kuzminski # Date 2017-04-19 14:52:24 # Node ID 40da1231f508a82f4494ca3908fe86788da44dd5 # Parent 17141cebf8741d9c298d16474463537d785b7b3d user-groups: moved get_user_groups from RepoModel to UserGroupModel. diff --git a/rhodecode/apps/admin/views/users.py b/rhodecode/apps/admin/views/users.py --- a/rhodecode/apps/admin/views/users.py +++ b/rhodecode/apps/admin/views/users.py @@ -242,7 +242,8 @@ class AdminUsersView(BaseAppView, DataGr c.user = User.get_or_404(user_id, pyramid_exc=True) c.data = c.user.group_member self._redirect_for_default_user(c.user.username) - groups = [UserGroupModel.get_user_groups_as_dict(group.users_group) for group in c.user.group_member] + groups = [UserGroupModel.get_user_groups_as_dict(group.users_group) + for group in c.user.group_member] c.groups = json.dumps(groups) c.active = 'groups' diff --git a/rhodecode/apps/home/views.py b/rhodecode/apps/home/views.py --- a/rhodecode/apps/home/views.py +++ b/rhodecode/apps/home/views.py @@ -31,7 +31,7 @@ from rhodecode.lib.utils2 import safe_un from rhodecode.model.db import func, Repository, RepoGroup from rhodecode.model.repo import RepoModel from rhodecode.model.scm import ScmModel - +from rhodecode.model.user_group import UserGroupModel log = logging.getLogger(__name__) @@ -62,7 +62,7 @@ class HomeView(BaseAppView): if include_groups: # extend with user groups - _user_groups = repo_model.get_user_groups( + _user_groups = UserGroupModel().get_user_groups( name_contains=query, only_active=active) _users = _users + _user_groups @@ -79,8 +79,7 @@ class HomeView(BaseAppView): log.debug('generating user group list, query:%s, active:%s', query, active) - repo_model = RepoModel() - _user_groups = repo_model.get_user_groups( + _user_groups = UserGroupModel().get_user_groups( name_contains=query, only_active=active) _user_groups = _user_groups diff --git a/rhodecode/model/repo.py b/rhodecode/model/repo.py --- a/rhodecode/model/repo.py +++ b/rhodecode/model/repo.py @@ -30,7 +30,6 @@ import time import traceback from datetime import datetime, timedelta -from sqlalchemy.sql import func from sqlalchemy.sql.expression import true, or_ from zope.cachedescriptors.property import Lazy as LazyProperty @@ -40,19 +39,17 @@ from rhodecode.lib.auth import HasUserGr from rhodecode.lib.caching_query import FromCache from rhodecode.lib.exceptions import AttachedForksError from rhodecode.lib.hooks_base import log_delete_repository -from rhodecode.lib.markup_renderer import MarkupRenderer from rhodecode.lib.utils import make_db_config from rhodecode.lib.utils2 import ( safe_str, safe_unicode, remove_prefix, obfuscate_url_pw, get_current_rhodecode_user, safe_int, datetime_to_time, action_logger_generic) from rhodecode.lib.vcs.backends import get_backend -from rhodecode.lib.vcs.exceptions import NodeDoesNotExistError from rhodecode.model import BaseModel from rhodecode.model.db import ( Repository, UserRepoToPerm, UserGroupRepoToPerm, UserRepoGroupToPerm, UserGroupRepoGroupToPerm, User, Permission, Statistics, UserGroup, RepoGroup, RepositoryField) -from rhodecode.model.scm import UserGroupList + from rhodecode.model.settings import VcsSettingsModel @@ -196,47 +193,6 @@ class RepoModel(BaseModel): ] return _users - def get_user_groups(self, name_contains=None, limit=20, only_active=True): - - # TODO: mikhail: move this method to the UserGroupModel. - query = self.sa.query(UserGroup) - if only_active: - query = query.filter(UserGroup.users_group_active == true()) - - if name_contains: - ilike_expression = u'%{}%'.format(safe_unicode(name_contains)) - query = query.filter( - UserGroup.users_group_name.ilike(ilike_expression))\ - .order_by(func.length(UserGroup.users_group_name))\ - .order_by(UserGroup.users_group_name) - - query = query.limit(limit) - user_groups = query.all() - perm_set = ['usergroup.read', 'usergroup.write', 'usergroup.admin'] - user_groups = UserGroupList(user_groups, perm_set=perm_set) - - _groups = [ - { - 'id': group.users_group_id, - # TODO: marcink figure out a way to generate the url for the - # icon - 'icon_link': '', - 'value_display': 'Group: %s (%d members)' % ( - group.users_group_name, len(group.members),), - 'value': group.users_group_name, - 'description': group.user_group_description, - 'owner': group.user.username, - - 'owner_icon': h.gravatar_url(group.user.email, 30), - 'value_display_owner': h.person(group.user.email), - - 'value_type': 'user_group', - 'active': group.users_group_active, - } - for group in user_groups - ] - return _groups - @classmethod def update_repoinfo(cls, repositories=None): if not repositories: diff --git a/rhodecode/model/user_group.py b/rhodecode/model/user_group.py --- a/rhodecode/model/user_group.py +++ b/rhodecode/model/user_group.py @@ -27,9 +27,10 @@ user group model for RhodeCode import logging import traceback -from rhodecode.lib.utils2 import safe_str +from rhodecode.lib.utils2 import safe_str, safe_unicode from rhodecode.model import BaseModel -from rhodecode.model.db import UserGroupMember, UserGroup,\ +from rhodecode.model.scm import UserGroupList +from rhodecode.model.db import true, func, UserGroupMember, UserGroup,\ UserGroupRepoToPerm, Permission, UserGroupToPerm, User, UserUserGroupToPerm,\ UserGroupUserGroupToPerm, UserGroupRepoGroupToPerm from rhodecode.lib.exceptions import UserGroupAssignedException,\ @@ -539,6 +540,48 @@ class UserGroupModel(BaseModel): log.debug('Adding user %s to user group %s', user.username, gr.users_group_name) UserGroupModel().add_user_to_group(gr.users_group_name, user.username) + def get_user_groups(self, name_contains=None, limit=20, only_active=True, + expand_groups=False): + import rhodecode.lib.helpers as h + + query = self.sa.query(UserGroup) + if only_active: + query = query.filter(UserGroup.users_group_active == true()) + + if name_contains: + ilike_expression = u'%{}%'.format(safe_unicode(name_contains)) + query = query.filter( + UserGroup.users_group_name.ilike(ilike_expression))\ + .order_by(func.length(UserGroup.users_group_name))\ + .order_by(UserGroup.users_group_name) + + query = query.limit(limit) + user_groups = query.all() + perm_set = ['usergroup.read', 'usergroup.write', 'usergroup.admin'] + user_groups = UserGroupList(user_groups, perm_set=perm_set) + + _groups = [ + { + 'id': group.users_group_id, + # TODO: marcink figure out a way to generate the url for the + # icon + 'icon_link': '', + 'value_display': 'Group: %s (%d members)' % ( + group.users_group_name, len(group.members),), + 'value': group.users_group_name, + 'description': group.user_group_description, + 'owner': group.user.username, + + 'owner_icon': h.gravatar_url(group.user.email, 30), + 'value_display_owner': h.person(group.user.email), + + 'value_type': 'user_group', + 'active': group.users_group_active, + } + for group in user_groups + ] + return _groups + @staticmethod def get_user_groups_as_dict(user_group): import rhodecode.lib.helpers as h @@ -550,7 +593,9 @@ class UserGroupModel(BaseModel): 'active': user_group.users_group_active, "owner": user_group.user.username, 'owner_icon': h.gravatar_url(user_group.user.email, 30), - "owner_data": {'owner': user_group.user.username, 'owner_icon': h.gravatar_url(user_group.user.email, 30)} + "owner_data": { + 'owner': user_group.user.username, + 'owner_icon': h.gravatar_url(user_group.user.email, 30)} } return data diff --git a/rhodecode/tests/models/test_repos.py b/rhodecode/tests/models/test_repos.py --- a/rhodecode/tests/models/test_repos.py +++ b/rhodecode/tests/models/test_repos.py @@ -29,10 +29,9 @@ from rhodecode.model.db import Repositor from rhodecode.model.meta import Session from rhodecode.model.repo import RepoModel from rhodecode.model.scm import ScmModel -from rhodecode.lib.utils2 import safe_unicode -class TestRepoModel: +class TestRepoModel(object): def test_remove_repo(self, backend): repo = backend.create_repo() @@ -251,60 +250,3 @@ class TestGetUsers(object): fake_users = [u for u in users if u['last_name'].startswith('Fake')] assert len(fake_users) == 3 - -class TestGetUserGroups(object): - def test_returns_filtered_list(self, backend, user_util): - created_groups = [] - for i in range(4): - created_groups.append( - user_util.create_user_group(users_group_active=True)) - - group_filter = created_groups[-1].users_group_name[-2:] - with mock.patch('rhodecode.lib.helpers.gravatar_url'): - with self._patch_user_group_list(): - groups = RepoModel().get_user_groups(group_filter) - - fake_groups = [ - u for u in groups if u['value'].startswith('test_returns')] - assert len(fake_groups) == 1 - assert fake_groups[0]['value'] == created_groups[-1].users_group_name - assert fake_groups[0]['value_display'].startswith( - 'Group: test_returns') - - def test_returns_limited_list(self, backend, user_util): - created_groups = [] - for i in range(3): - created_groups.append( - user_util.create_user_group(users_group_active=True)) - with mock.patch('rhodecode.lib.helpers.gravatar_url'): - with self._patch_user_group_list(): - groups = RepoModel().get_user_groups('test_returns') - - fake_groups = [ - u for u in groups if u['value'].startswith('test_returns')] - assert len(fake_groups) == 3 - - def test_returns_active_user_groups(self, backend, user_util): - for i in range(4): - is_active = i % 2 == 0 - user_util.create_user_group(users_group_active=is_active) - with mock.patch('rhodecode.lib.helpers.gravatar_url'): - with self._patch_user_group_list(): - groups = RepoModel().get_user_groups() - expected = ('id', 'icon_link', 'value_display', 'value', 'value_type') - for group in groups: - assert group['value_type'] is 'user_group' - for key in expected: - assert key in group - - fake_groups = [ - u for u in groups if u['value'].startswith('test_returns')] - assert len(fake_groups) == 2 - for user in fake_groups: - assert user['value_display'].startswith('Group: test_returns') - - def _patch_user_group_list(self): - def side_effect(group_list, perm_set): - return group_list - return mock.patch( - 'rhodecode.model.repo.UserGroupList', side_effect=side_effect) diff --git a/rhodecode/tests/models/test_user_groups.py b/rhodecode/tests/models/test_user_groups.py --- a/rhodecode/tests/models/test_user_groups.py +++ b/rhodecode/tests/models/test_user_groups.py @@ -35,6 +35,64 @@ def teardown_module(self): _delete_all_user_groups() +class TestGetUserGroups(object): + def test_returns_filtered_list(self, backend, user_util): + created_groups = [] + for i in range(4): + created_groups.append( + user_util.create_user_group(users_group_active=True)) + + group_filter = created_groups[-1].users_group_name[-2:] + with mock.patch('rhodecode.lib.helpers.gravatar_url'): + with self._patch_user_group_list(): + groups = UserGroupModel().get_user_groups(group_filter) + + fake_groups = [ + u for u in groups if u['value'].startswith('test_returns')] + assert len(fake_groups) == 1 + assert fake_groups[0]['value'] == created_groups[-1].users_group_name + assert fake_groups[0]['value_display'].startswith( + 'Group: test_returns') + + def test_returns_limited_list(self, backend, user_util): + created_groups = [] + for i in range(3): + created_groups.append( + user_util.create_user_group(users_group_active=True)) + with mock.patch('rhodecode.lib.helpers.gravatar_url'): + with self._patch_user_group_list(): + groups = UserGroupModel().get_user_groups('test_returns') + + fake_groups = [ + u for u in groups if u['value'].startswith('test_returns')] + assert len(fake_groups) == 3 + + def test_returns_active_user_groups(self, backend, user_util): + for i in range(4): + is_active = i % 2 == 0 + user_util.create_user_group(users_group_active=is_active) + with mock.patch('rhodecode.lib.helpers.gravatar_url'): + with self._patch_user_group_list(): + groups = UserGroupModel().get_user_groups() + expected = ('id', 'icon_link', 'value_display', 'value', 'value_type') + for group in groups: + assert group['value_type'] is 'user_group' + for key in expected: + assert key in group + + fake_groups = [ + u for u in groups if u['value'].startswith('test_returns')] + assert len(fake_groups) == 2 + for user in fake_groups: + assert user['value_display'].startswith('Group: test_returns') + + def _patch_user_group_list(self): + def side_effect(group_list, perm_set): + return group_list + return mock.patch( + 'rhodecode.model.user_group.UserGroupList', side_effect=side_effect) + + @pytest.mark.parametrize( "pre_existing, regular_should_be, external_should_be, groups, " "expected", [