# HG changeset patch # User Marcin Kuzminski # Date 2011-05-26 01:28:06 # Node ID 526120c77a38c76dc3fa88e58d636b1d4d3fe252 # Parent dccba44ee176e56cf1e9fc26a251ce442f86927f #47 added editing of groups, and moving them between. Added check constraint for groups to prevent of loops diff --git a/rhodecode/controllers/admin/repos_groups.py b/rhodecode/controllers/admin/repos_groups.py --- a/rhodecode/controllers/admin/repos_groups.py +++ b/rhodecode/controllers/admin/repos_groups.py @@ -56,9 +56,9 @@ class ReposGroupsController(BaseControll repo_group = Group.get(group_id) - defaults = repo_group.get_dict() + data = repo_group.get_dict() - return defaults + return data @HasPermissionAnyDecorator('hg.admin') def index(self, format='html'): @@ -140,7 +140,7 @@ class ReposGroupsController(BaseControll encoding="UTF-8") except Exception: log.error(traceback.format_exc()) - h.flash(_('error occurred during creation of repos group %s') \ + h.flash(_('error occurred during update of repos group %s') \ % request.POST.get('group_name'), category='error') return redirect(url('repos_groups')) @@ -223,9 +223,15 @@ class ReposGroupsController(BaseControll def edit(self, id, format='html'): """GET /repos_groups/id/edit: Form to edit an existing item""" # url('edit_repos_group', id=ID) + + id = int(id) + c.repos_group = Group.get(id) defaults = self.__load_data(id) + # we need to exclude this group from the group list for editing + c.repo_groups = filter(lambda x:x[0] != id, c.repo_groups) + return htmlfill.render( render('admin/repos_groups/repos_groups_edit.html'), defaults=defaults, diff --git a/rhodecode/model/db.py b/rhodecode/model/db.py --- a/rhodecode/model/db.py +++ b/rhodecode/model/db.py @@ -301,7 +301,8 @@ class Repository(Base): class Group(Base): __tablename__ = 'groups' - __table_args__ = (UniqueConstraint('group_name', 'group_parent_id'), {'useexisting':True},) + __table_args__ = (UniqueConstraint('group_name', 'group_parent_id'), + CheckConstraint('group_id != group_parent_id'), {'useexisting':True},) __mapper_args__ = {'order_by':'group_name'} group_id = Column("group_id", Integer(), nullable=False, unique=True, default=None, primary_key=True) @@ -320,23 +321,31 @@ class Group(Base): return "<%s('%s:%s')>" % (self.__class__.__name__, self.group_id, self.group_name) - @classmethod def url_sep(cls): return '/' @property def parents(self): + parents_limit = 5 groups = [] if self.parent_group is None: return groups cur_gr = self.parent_group groups.insert(0, cur_gr) + cnt = 0 while 1: + cnt += 1 gr = getattr(cur_gr, 'parent_group', None) cur_gr = cur_gr.parent_group if gr is None: break + if cnt == parents_limit: + # this will prevent accidental infinit loops + log.error('group nested more than %s' % + parents_limit) + break + groups.insert(0, gr) return groups diff --git a/rhodecode/model/forms.py b/rhodecode/model/forms.py --- a/rhodecode/model/forms.py +++ b/rhodecode/model/forms.py @@ -122,11 +122,18 @@ def ValidReposGroup(edit, old_data): def validate_python(self, value, state): #TODO WRITE VALIDATIONS group_name = value.get('group_name') - group_parent_id = value.get('group_parent_id') + group_parent_id = int(value.get('group_parent_id') or - 1) # slugify repo group just in case :) slug = repo_name_slug(group_name) + # check for parent of self + if old_data['group_id'] == group_parent_id: + e_dict = {'group_parent_id':_('Cannot assign this group ' + 'as parent')} + raise formencode.Invalid('', value, state, + error_dict=e_dict) + old_gname = None if edit: old_gname = Group.get( diff --git a/rhodecode/model/repos_group.py b/rhodecode/model/repos_group.py --- a/rhodecode/model/repos_group.py +++ b/rhodecode/model/repos_group.py @@ -74,20 +74,34 @@ class ReposGroupModel(BaseModel): os.makedirs(create_path) - def __rename_group(self, old, new): + def __rename_group(self, old, old_parent_id, new, new_parent_id): """ Renames a group on filesystem :param group_name: """ - log.info('renaming repos group from %s to %s', old, - old) + log.debug('renaming repos group from %s to %s', old, new) + + if new_parent_id: + paths = Group.get(new_parent_id).full_path.split(Group.url_sep()) + new_parent_path = os.sep.join(paths) + else: + new_parent_path = '' - old_path = os.path.join(self.repos_path, old) - new_path = os.path.join(self.repos_path, new) + if old_parent_id: + paths = Group.get(old_parent_id).full_path.split(Group.url_sep()) + old_parent_path = os.sep.join(paths) + else: + old_parent_path = '' + + old_path = os.path.join(self.repos_path, old_parent_path, old) + new_path = os.path.join(self.repos_path, new_parent_path, new) + + log.debug('renaming repos paths from %s to %s', old_path, new_path) + if os.path.isdir(new_path): - raise Exception('Was trying to rename to already existing dir %s', - new_path) + raise Exception('Was trying to rename to already ' + 'existing dir %s' % new_path) shutil.move(old_path, new_path) def __delete_group(self, group): @@ -126,6 +140,7 @@ class ReposGroupModel(BaseModel): try: repos_group = Group.get(repos_group_id) old_name = repos_group.group_name + old_parent_id = repos_group.group_parent_id repos_group.group_name = form_data['group_name'] repos_group.group_description = \ @@ -134,8 +149,11 @@ class ReposGroupModel(BaseModel): self.sa.add(repos_group) - if old_name != form_data['group_name']: - self.__rename_group(old=old_name, new=form_data['group_name']) + if old_name != form_data['group_name'] or (old_parent_id != + form_data['group_parent_id']): + self.__rename_group(old=old_name, old_parent_id=old_parent_id, + new=form_data['group_name'], + new_parent_id=form_data['group_parent_id']) self.sa.commit() except: