Changeset - 526120c77a38
[Not reviewed]
beta
0 4 0
Marcin Kuzminski - 14 years ago 2011-05-26 01:28:06
marcin@python-works.com
#47 added editing of groups, and moving them between. Added check constraint for groups
to prevent of loops
4 files changed with 55 insertions and 15 deletions:
0 comments (0 inline, 0 general)
rhodecode/controllers/admin/repos_groups.py
Show inline comments
 
@@ -53,15 +53,15 @@ class ReposGroupsController(BaseControll
 
        :param group_id:
 
        """
 
        self.__load_defaults()
 

	
 
        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'):
 
        """GET /repos_groups: All items in the collection"""
 
        # url('repos_groups')
 

	
 
@@ -137,13 +137,13 @@ class ReposGroupsController(BaseControll
 
                defaults=errors.value,
 
                errors=errors.error_dict or {},
 
                prefix_error=False,
 
                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'))
 

	
 

	
 
    @HasPermissionAnyDecorator('hg.admin')
 
@@ -220,15 +220,21 @@ class ReposGroupsController(BaseControll
 
        return render('admin/repos_groups/repos_groups.html')
 

	
 
    @HasPermissionAnyDecorator('hg.admin')
 
    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,
 
            encoding="UTF-8",
 
            force_defaults=False
 
        )
rhodecode/model/db.py
Show inline comments
 
@@ -298,13 +298,14 @@ class Repository(Base):
 
    def groups_and_repo(self):
 
        return self.groups_with_parents, self.just_name
 

	
 

	
 
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)
 
    group_name = Column("group_name", String(length=255, convert_unicode=False, assert_unicode=None), nullable=False, unique=True, default=None)
 
    group_parent_id = Column("group_parent_id", Integer(), ForeignKey('groups.group_id'), nullable=True, unique=None, default=None)
 
    group_description = Column("group_description", String(length=10000, convert_unicode=False, assert_unicode=None), nullable=True, unique=None, default=None)
 
@@ -317,29 +318,37 @@ class Group(Base):
 
        self.parent_group = parent_group
 

	
 
    def __repr__(self):
 
        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
 

	
 

	
 
    @property
 
    def full_path(self):
rhodecode/model/forms.py
Show inline comments
 
@@ -119,17 +119,24 @@ def ValidUsersGroup(edit, old_data):
 
def ValidReposGroup(edit, old_data):
 
    class _ValidReposGroup(formencode.validators.FancyValidator):
 

	
 
        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(
 
                            old_data.get('group_id')).group_name
 

	
 
            if old_gname != group_name or not edit:
rhodecode/model/repos_group.py
Show inline comments
 
@@ -71,26 +71,40 @@ class ReposGroupModel(BaseModel):
 
            raise Exception('That directory already exists !')
 

	
 

	
 
        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):
 
        """
 
        Deletes a group from a filesystem
 
        
 
@@ -123,22 +137,26 @@ class ReposGroupModel(BaseModel):
 

	
 
    def update(self, repos_group_id, form_data):
 

	
 
        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 = \
 
                form_data['group_description']
 
            repos_group.group_parent_id = form_data['group_parent_id']
 

	
 
            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:
 
            log.error(traceback.format_exc())
 
            self.sa.rollback()
 
            raise
0 comments (0 inline, 0 general)