# HG changeset patch # User Marcin Kuzminski # Date 2011-07-19 15:10:34 # Node ID a62af7d509eeb85d2613c2ef1db2c9a7bb90e1a7 # Parent d21c14e36fa81e2bb86531cfc83636ea12238857 fixes #228 - added detection of assigned groups to prevent errors on sqlite 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 @@ -32,6 +32,7 @@ from pylons import request, session, tmp from pylons.controllers.util import abort, redirect from pylons.i18n.translation import _ +from rhodecode.lib.exceptions import UsersGroupsAssignedException from rhodecode.lib import helpers as h from rhodecode.lib.auth import LoginRequired, HasPermissionAllDecorator from rhodecode.lib.base import BaseController, render @@ -153,6 +154,8 @@ class UsersGroupsController(BaseControll try: users_group_model.delete(id) h.flash(_('successfully deleted users group'), category='success') + except UsersGroupsAssignedException, e: + h.flash(e, category='error') except Exception: h.flash(_('An error occurred during deletion of users group'), category='error') diff --git a/rhodecode/lib/exceptions.py b/rhodecode/lib/exceptions.py --- a/rhodecode/lib/exceptions.py +++ b/rhodecode/lib/exceptions.py @@ -45,3 +45,6 @@ class DefaultUserException(Exception): class UserOwnsReposException(Exception): pass + +class UsersGroupsAssignedException(Exception): + pass diff --git a/rhodecode/model/db.py b/rhodecode/model/db.py --- a/rhodecode/model/db.py +++ b/rhodecode/model/db.py @@ -286,6 +286,8 @@ class UsersGroup(Base, BaseModel): members = relationship('UsersGroupMember', cascade="all, delete, delete-orphan", lazy="joined") + def __repr__(self): + return '' % (self.users_group_name) @classmethod def get_by_group_name(cls, group_name, cache=False, case_insensitive=False): @@ -677,6 +679,8 @@ class UsersGroupRepoToPerm(Base, BaseMod permission = relationship('Permission') repository = relationship('Repository') + def __repr__(self): + return ' %s >' % (self.users_group, self.repository) class UsersGroupToPerm(Base, BaseModel): __tablename__ = 'users_group_to_perm' diff --git a/rhodecode/model/users_group.py b/rhodecode/model/users_group.py --- a/rhodecode/model/users_group.py +++ b/rhodecode/model/users_group.py @@ -28,9 +28,11 @@ 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 +from rhodecode.model.db import UsersGroup, UsersGroupMember, \ + UsersGroupRepoToPerm log = logging.getLogger(__name__) @@ -84,6 +86,16 @@ class UsersGroupModel(BaseModel): 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()