diff --git a/rhodecode/controllers/admin/users_groups.py b/rhodecode/controllers/admin/users_groups.py --- a/rhodecode/controllers/admin/users_groups.py +++ b/rhodecode/controllers/admin/users_groups.py @@ -39,7 +39,6 @@ from rhodecode.lib.base import BaseContr from rhodecode.model.db import User, UsersGroup, Permission, UsersGroupToPerm from rhodecode.model.forms import UserForm, UsersGroupForm -from rhodecode.model.users_group import UsersGroupModel log = logging.getLogger(__name__) @@ -67,11 +66,11 @@ class UsersGroupsController(BaseControll def create(self): """POST /users_groups: Create a new item""" # url('users_groups') - users_group_model = UsersGroupModel() + users_group_form = UsersGroupForm()() try: form_result = users_group_form.to_python(dict(request.POST)) - users_group_model.create(form_result) + UsersGroup.create(form_result) h.flash(_('created users group %s') \ % form_result['users_group_name'], category='success') #action_logger(self.rhodecode_user, 'new_user', '', '', self.sa) @@ -103,8 +102,7 @@ class UsersGroupsController(BaseControll # method='put') # url('users_group', id=ID) - users_group_model = UsersGroupModel() - c.users_group = users_group_model.get(id) + c.users_group = UsersGroup.get(id) c.group_members = [(x.user_id, x.user.username) for x in c.users_group.members] @@ -117,7 +115,7 @@ class UsersGroupsController(BaseControll try: form_result = users_group_form.to_python(request.POST) - users_group_model.update(id, form_result) + UsersGroup.update(id, form_result) h.flash(_('updated users group %s') \ % form_result['users_group_name'], category='success') @@ -150,9 +148,9 @@ class UsersGroupsController(BaseControll # h.form(url('users_group', id=ID), # method='delete') # url('users_group', id=ID) - users_group_model = UsersGroupModel() + try: - users_group_model.delete(id) + UsersGroup.delete(id) h.flash(_('successfully deleted users group'), category='success') except UsersGroupsAssignedException, e: h.flash(e, category='error') diff --git a/rhodecode/model/db.py b/rhodecode/model/db.py --- a/rhodecode/model/db.py +++ b/rhodecode/model/db.py @@ -42,6 +42,7 @@ from vcs.exceptions import RepositoryErr from vcs.utils.lazy import LazyProperty from vcs.nodes import FileNode +from rhodecode.lib.exceptions import UsersGroupsAssignedException from rhodecode.lib import str2bool, json, safe_str from rhodecode.model.meta import Base, Session from rhodecode.model.caching_query import FromCache @@ -302,6 +303,77 @@ class UsersGroup(Base, BaseModel): "get_user_%s" % group_name)) return gr.scalar() + + @classmethod + def get(cls, users_group_id, cache=False): + users_group = Session.query(cls) + if cache: + users_group = users_group.options(FromCache("sql_cache_short", + "get_users_group_%s" % users_group_id)) + return users_group.get(users_group_id) + + @classmethod + def create(cls, form_data): + try: + new_users_group = cls() + for k, v in form_data.items(): + setattr(new_users_group, k, v) + + Session.add(new_users_group) + Session.commit() + except: + log.error(traceback.format_exc()) + Session.rollback() + raise + + @classmethod + def update(cls, users_group_id, form_data): + + try: + users_group = cls.get(users_group_id, cache=False) + + for k, v in form_data.items(): + if k == 'users_group_members': + users_group.members = [] + Session.flush() + members_list = [] + if v: + for u_id in set(v): + members_list.append(UsersGroupMember( + users_group_id, + u_id)) + setattr(users_group, 'members', members_list) + setattr(users_group, k, v) + + Session.add(users_group) + Session.commit() + except: + log.error(traceback.format_exc()) + Session.rollback() + raise + + @classmethod + def delete(cls, users_group_id): + try: + + # check if this group is not assigned to repo + assigned_groups = UsersGroupRepoToPerm.query()\ + .filter(UsersGroupRepoToPerm.users_group_id == + users_group_id).all() + + if assigned_groups: + raise UsersGroupsAssignedException('Group assigned to %s' % + assigned_groups) + + users_group = cls.get(users_group_id, cache=False) + Session.delete(users_group) + Session.commit() + except: + log.error(traceback.format_exc()) + Session.rollback() + raise + + class UsersGroupMember(Base, BaseModel): __tablename__ = 'users_groups_members' __table_args__ = {'extend_existing':True} diff --git a/rhodecode/model/users_group.py b/rhodecode/model/users_group.py deleted file mode 100644 --- a/rhodecode/model/users_group.py +++ /dev/null @@ -1,105 +0,0 @@ -# -*- coding: utf-8 -*- -""" - rhodecode.model.user_group - ~~~~~~~~~~~~~~~~~~~~~~~~~~ - - users groups model for RhodeCode - - :created_on: Jan 25, 2011 - :author: marcink - :copyright: (C) 2009-2011 Marcin Kuzminski - :license: GPLv3, see COPYING for more details. -""" -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -import logging -import traceback - -from pylons.i18n.translation import _ - -from rhodecode.lib.exceptions import UsersGroupsAssignedException -from rhodecode.model import BaseModel -from rhodecode.model.caching_query import FromCache -from rhodecode.model.db import UsersGroup, UsersGroupMember, \ - UsersGroupRepoToPerm - -log = logging.getLogger(__name__) - - -class UsersGroupModel(BaseModel): - - def get(self, users_group_id, cache=False): - users_group = self.sa.query(UsersGroup) - if cache: - users_group = users_group.options(FromCache("sql_cache_short", - "get_users_group_%s" % users_group_id)) - return users_group.get(users_group_id) - - def create(self, form_data): - try: - new_users_group = UsersGroup() - for k, v in form_data.items(): - setattr(new_users_group, k, v) - - self.sa.add(new_users_group) - self.sa.commit() - except: - log.error(traceback.format_exc()) - self.sa.rollback() - raise - - def update(self, users_group_id, form_data): - - try: - users_group = self.get(users_group_id, cache=False) - - for k, v in form_data.items(): - if k == 'users_group_members': - users_group.members = [] - self.sa.flush() - members_list = [] - if v: - for u_id in set(v): - members_list.append(UsersGroupMember( - users_group_id, - u_id)) - setattr(users_group, 'members', members_list) - setattr(users_group, k, v) - - self.sa.add(users_group) - self.sa.commit() - except: - log.error(traceback.format_exc()) - self.sa.rollback() - raise - - def delete(self, users_group_id): - try: - - # check if this group is not assigned to repo - assigned_groups = UsersGroupRepoToPerm.query()\ - .filter(UsersGroupRepoToPerm.users_group_id == - users_group_id).all() - - if assigned_groups: - raise UsersGroupsAssignedException('Group assigned to %s' % - assigned_groups) - - users_group = self.get(users_group_id, cache=False) - self.sa.delete(users_group) - self.sa.commit() - except: - log.error(traceback.format_exc()) - self.sa.rollback() - raise diff --git a/rhodecode/templates/changelog/changelog.html b/rhodecode/templates/changelog/changelog.html --- a/rhodecode/templates/changelog/changelog.html +++ b/rhodecode/templates/changelog/changelog.html @@ -60,10 +60,7 @@
- - ${len(cs.affected_files)} - + ${len(cs.affected_files)}
%if len(cs.parents)>1:
diff --git a/rhodecode/tests/functional/test_admin_users_groups.py b/rhodecode/tests/functional/test_admin_users_groups.py --- a/rhodecode/tests/functional/test_admin_users_groups.py +++ b/rhodecode/tests/functional/test_admin_users_groups.py @@ -1,4 +1,5 @@ from rhodecode.tests import * +from rhodecode.model.db import UsersGroup TEST_USERS_GROUP = 'admins_test' @@ -36,13 +37,37 @@ class TestAdminUsersGroupsController(Tes response = self.app.put(url('users_group', id=1)) def test_update_browser_fakeout(self): - response = self.app.post(url('users_group', id=1), params=dict(_method='put')) + response = self.app.post(url('users_group', id=1), + params=dict(_method='put')) def test_delete(self): - response = self.app.delete(url('users_group', id=1)) + self.log_user() + users_group_name = TEST_USERS_GROUP + 'another' + response = self.app.post(url('users_groups'), + {'users_group_name':users_group_name, + 'active':True}) + response.follow() + + self.checkSessionFlash(response, + 'created users group %s' % users_group_name) + + + gr = self.sa.query(UsersGroup)\ + .filter(UsersGroup.users_group_name == + users_group_name).one() + + response = self.app.delete(url('users_group', id=gr.users_group_id)) + + gr = self.sa.query(UsersGroup)\ + .filter(UsersGroup.users_group_name == + users_group_name).scalar() + + self.assertEqual(gr, None) + def test_delete_browser_fakeout(self): - response = self.app.post(url('users_group', id=1), params=dict(_method='delete')) + response = self.app.post(url('users_group', id=1), + params=dict(_method='delete')) def test_show(self): response = self.app.get(url('users_group', id=1)) diff --git a/rhodecode/tests/functional/test_changelog.py b/rhodecode/tests/functional/test_changelog.py --- a/rhodecode/tests/functional/test_changelog.py +++ b/rhodecode/tests/functional/test_changelog.py @@ -4,35 +4,55 @@ class TestChangelogController(TestContro def test_index_hg(self): self.log_user() - response = self.app.get(url(controller='changelog', action='index', repo_name=HG_REPO)) + response = self.app.get(url(controller='changelog', action='index', + repo_name=HG_REPO)) - assert """
""" in response.body, 'wrong info about number of changes' - assert """""" in response.body, 'no checkbox for this commit' - assert """commit 154: 5e204e7583b9@2010-08-10 01:18:46""" in response.body , 'no info on this commit' - assert """Small update at simplevcs app""" in response.body, 'missing info about commit message' + self.assertTrue("""
""" + in response.body) + self.assertTrue("""""" + in response.body) + self.assertTrue("""commit 154: 5e204e7583b9@2010-08-10 """ + """01:18:46""" in response.body) + self.assertTrue("""Small update at simplevcs app""" in response.body) - assert """0""" in response.body, 'wrong info about removed nodes' - assert """2""" in response.body, 'wrong info about changed nodes' - assert """1""" in response.body, 'wrong info about added nodes' + + self.assertTrue("""3""" in response.body) #pagination - response = self.app.get(url(controller='changelog', action='index', repo_name=HG_REPO), {'page':1}) - response = self.app.get(url(controller='changelog', action='index', repo_name=HG_REPO), {'page':2}) - response = self.app.get(url(controller='changelog', action='index', repo_name=HG_REPO), {'page':3}) - response = self.app.get(url(controller='changelog', action='index', repo_name=HG_REPO), {'page':4}) - response = self.app.get(url(controller='changelog', action='index', repo_name=HG_REPO), {'page':5}) - response = self.app.get(url(controller='changelog', action='index', repo_name=HG_REPO), {'page':6}) + response = self.app.get(url(controller='changelog', action='index', + repo_name=HG_REPO), {'page':1}) + response = self.app.get(url(controller='changelog', action='index', + repo_name=HG_REPO), {'page':2}) + response = self.app.get(url(controller='changelog', action='index', + repo_name=HG_REPO), {'page':3}) + response = self.app.get(url(controller='changelog', action='index', + repo_name=HG_REPO), {'page':4}) + response = self.app.get(url(controller='changelog', action='index', + repo_name=HG_REPO), {'page':5}) + response = self.app.get(url(controller='changelog', action='index', + repo_name=HG_REPO), {'page':6}) # Test response after pagination... - print response.body - assert """""" in response.body, 'no checkbox for this commit' - assert """commit 64: 46ad32a4f974@2010-04-20 00:33:21"""in response.body, 'wrong info about commit 64' - assert """1"""in response.body, 'wrong info about number of removed' - assert """13"""in response.body, 'wrong info about number of changes' - assert """20"""in response.body, 'wrong info about number of added' - assert """""" % HG_REPO in response.body, 'wrong info about commit 64 is a merge' + self.assertTrue("""""" + in response.body) + self.assertTrue("""commit 64: 46ad32a4f974@2010-04-20""" + """ 00:33:21"""in response.body) + + self.assertTrue("""21"""in response.body) + self.assertTrue("""""" % HG_REPO in response.body)