Commits

Marcin Kuzminski committed a62af7d

fixes #228 - added detection of assigned groups to prevent errors on sqlite

  • Participants
  • Parent commits d21c14e
  • Branches beta

Comments (0)

Files changed (4)

rhodecode/controllers/admin/users_groups.py

 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
         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')

rhodecode/lib/exceptions.py

 
 class UserOwnsReposException(Exception):
     pass
+
+class UsersGroupsAssignedException(Exception):
+    pass

rhodecode/model/db.py

 
     members = relationship('UsersGroupMember', cascade="all, delete, delete-orphan", lazy="joined")
 
+    def __repr__(self):
+        return '<userGroup(%s)>' % (self.users_group_name)
 
     @classmethod
     def get_by_group_name(cls, group_name, cache=False, case_insensitive=False):
     permission = relationship('Permission')
     repository = relationship('Repository')
 
+    def __repr__(self):
+        return '<userGroup:%s => %s >' % (self.users_group, self.repository)
 
 class UsersGroupToPerm(Base, BaseModel):
     __tablename__ = 'users_group_to_perm'

rhodecode/model/users_group.py

 
 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__)
 
 
     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()