changeset 1349:526120c77a38 beta

#47 added editing of groups, and moving them between. Added check constraint for groups to prevent of loops
author Marcin Kuzminski <marcin@python-works.com>
date Thu, 26 May 2011 01:28:06 +0200
parents dccba44ee176
children eb489bf9d7c7
files rhodecode/controllers/admin/repos_groups.py rhodecode/model/db.py rhodecode/model/forms.py rhodecode/model/repos_group.py
diffstat 4 files changed, 55 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/rhodecode/controllers/admin/repos_groups.py	Thu May 26 01:06:33 2011 +0200
+++ b/rhodecode/controllers/admin/repos_groups.py	Thu May 26 01:28:06 2011 +0200
@@ -56,9 +56,9 @@
 
         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 @@
                 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 @@
     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,
--- a/rhodecode/model/db.py	Thu May 26 01:06:33 2011 +0200
+++ b/rhodecode/model/db.py	Thu May 26 01:28:06 2011 +0200
@@ -301,7 +301,8 @@
 
 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 @@
         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
 
--- a/rhodecode/model/forms.py	Thu May 26 01:06:33 2011 +0200
+++ b/rhodecode/model/forms.py	Thu May 26 01:28:06 2011 +0200
@@ -122,11 +122,18 @@
         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(
--- a/rhodecode/model/repos_group.py	Thu May 26 01:06:33 2011 +0200
+++ b/rhodecode/model/repos_group.py	Thu May 26 01:28:06 2011 +0200
@@ -74,20 +74,34 @@
         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 @@
         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 @@
 
             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: