Changeset - a7dfe823933a
[Not reviewed]
beta
0 2 0
Marcin Kuzminski - 14 years ago 2012-01-13 23:56:37
marcin@python-works.com
added validation to repo groups to check for conflicting repository name fixes #337
2 files changed with 52 insertions and 14 deletions:
0 comments (0 inline, 0 general)
rhodecode/controllers/admin/repos_groups.py
Show inline comments
 
@@ -81,7 +81,7 @@ class ReposGroupsController(BaseControll
 
        """GET /repos_groups: All items in the collection"""
 
        # url('repos_groups')
 

	
 
        sk = lambda g:g.parents[0].group_name if g.parents else g.group_name
 
        sk = lambda g: g.parents[0].group_name if g.parents else g.group_name
 
        c.groups = sorted(RepoGroup.query().all(), key=sk)
 
        return render('admin/repos_groups/repos_groups_show.html')
 

	
 
@@ -90,7 +90,7 @@ class ReposGroupsController(BaseControll
 
        """POST /repos_groups: Create a new item"""
 
        # url('repos_groups')
 
        self.__load_defaults()
 
        repos_group_form = ReposGroupForm(available_groups=
 
        repos_group_form = ReposGroupForm(available_groups =
 
                                          c.repo_groups_choices)()
 
        try:
 
            form_result = repos_group_form.to_python(dict(request.POST))
 
@@ -114,7 +114,6 @@ class ReposGroupsController(BaseControll
 

	
 
        return redirect(url('repos_groups'))
 

	
 

	
 
    @HasPermissionAnyDecorator('hg.admin')
 
    def new(self, format='html'):
 
        """GET /repos_groups/new: Form to create a new item"""
rhodecode/model/forms.py
Show inline comments
 
@@ -41,10 +41,12 @@ from rhodecode import BACKENDS
 

	
 
log = logging.getLogger(__name__)
 

	
 

	
 
#this is needed to translate the messages using _() in validators
 
class State_obj(object):
 
    _ = staticmethod(_)
 

	
 

	
 
#==============================================================================
 
# VALIDATORS
 
#==============================================================================
 
@@ -57,6 +59,7 @@ class ValidAuthToken(formencode.validato
 
            raise formencode.Invalid(self.message('invalid_token', state,
 
                                            search_number=value), value, state)
 

	
 

	
 
def ValidUsername(edit, old_data):
 
    class _ValidUsername(formencode.validators.FancyValidator):
 

	
 
@@ -103,7 +106,6 @@ def ValidUsersGroup(edit, old_data):
 
                                               'already exists') , value,
 
                                             state)
 

	
 

	
 
            if re.match(r'^[a-zA-Z0-9]{1}[a-zA-Z0-9\-\_\.]+$', value) is None:
 
                raise formencode.Invalid(_('RepoGroup name may only contain '
 
                                           'alphanumeric characters '
 
@@ -126,11 +128,14 @@ def ValidReposGroup(edit, old_data):
 
            slug = repo_name_slug(group_name)
 

	
 
            # check for parent of self
 
            parent_of_self = lambda:(old_data['group_id'] == int(group_parent_id)
 
                                     if group_parent_id else False)
 
            parent_of_self = lambda: (
 
                old_data['group_id'] == int(group_parent_id)
 
                if group_parent_id else False
 
            )
 
            if edit and parent_of_self():
 
                    e_dict = {'group_parent_id':_('Cannot assign this group '
 
                                                  'as parent')}
 
                    e_dict = {
 
                        'group_parent_id': _('Cannot assign this group as parent')
 
                    }
 
                    raise formencode.Invalid('', value, state,
 
                                             error_dict=e_dict)
 

	
 
@@ -140,17 +145,34 @@ def ValidReposGroup(edit, old_data):
 

	
 
            if old_gname != group_name or not edit:
 

	
 
                # check filesystem
 
                gr = RepoGroup.query().filter(RepoGroup.group_name == slug)\
 
                    .filter(RepoGroup.group_parent_id == group_parent_id).scalar()
 
                # check group
 
                gr = RepoGroup.query()\
 
                      .filter(RepoGroup.group_name == slug)\
 
                      .filter(RepoGroup.group_parent_id == group_parent_id)\
 
                      .scalar()
 

	
 
                if gr:
 
                    e_dict = {'group_name':_('This group already exists')}
 
                    e_dict = {
 
                        'group_name': _('This group already exists')
 
                    }
 
                    raise formencode.Invalid('', value, state,
 
                                             error_dict=e_dict)
 

	
 
                # check for same repo
 
                repo = Repository.query()\
 
                      .filter(Repository.repo_name == slug)\
 
                      .scalar()
 

	
 
                if repo:
 
                    e_dict = {
 
                        'group_name': _('Repository with this name already exists')
 
                    }
 
                    raise formencode.Invalid('', value, state,
 
                                             error_dict=e_dict)
 

	
 
    return _ValidReposGroup
 

	
 

	
 
class ValidPassword(formencode.validators.FancyValidator):
 

	
 
    def to_python(self, value, state):
 
@@ -182,6 +204,7 @@ class ValidPassword(formencode.validator
 

	
 
            return value
 

	
 

	
 
class ValidPasswordsMatch(formencode.validators.FancyValidator):
 

	
 
    def validate_python(self, value, state):
 
@@ -192,6 +215,7 @@ class ValidPasswordsMatch(formencode.val
 
                   _('Passwords do not match')}
 
            raise formencode.Invalid('', value, state, error_dict=e_dict)
 

	
 

	
 
class ValidAuth(formencode.validators.FancyValidator):
 
    messages = {
 
        'invalid_password':_('invalid password'),
 
@@ -224,6 +248,7 @@ class ValidAuth(formencode.validators.Fa
 
                                         state=State_obj), value, state,
 
                                         error_dict=self.e_dict)
 

	
 

	
 
class ValidRepoUser(formencode.validators.FancyValidator):
 

	
 
    def to_python(self, value, state):
 
@@ -235,6 +260,7 @@ class ValidRepoUser(formencode.validator
 
                                     value, state)
 
        return value
 

	
 

	
 
def ValidRepoName(edit, old_data):
 
    class _ValidRepoName(formencode.validators.FancyValidator):
 
        def to_python(self, value, state):
 
@@ -289,6 +315,7 @@ def ValidRepoName(edit, old_data):
 

	
 
    return _ValidRepoName
 

	
 

	
 
def ValidForkName(*args, **kwargs):
 
    return ValidRepoName(*args, **kwargs)
 

	
 
@@ -301,6 +328,7 @@ def SlugifyName():
 

	
 
    return _SlugifyName
 

	
 

	
 
def ValidCloneUri():
 
    from mercurial.httprepo import httprepository, httpsrepository
 
    from rhodecode.lib.utils import make_ui
 
@@ -332,6 +360,7 @@ def ValidCloneUri():
 

	
 
    return _ValidCloneUri
 

	
 

	
 
def ValidForkType(old_data):
 
    class _ValidForkType(formencode.validators.FancyValidator):
 

	
 
@@ -343,6 +372,7 @@ def ValidForkType(old_data):
 
            return value
 
    return _ValidForkType
 

	
 

	
 
class ValidPerms(formencode.validators.FancyValidator):
 
    messages = {'perm_new_member_name':_('This username or users group name'
 
                                         ' is not valid')}
 
@@ -393,6 +423,7 @@ class ValidPerms(formencode.validators.F
 
                                         error_dict={'perm_new_member_name':msg})
 
        return value
 

	
 

	
 
class ValidSettings(formencode.validators.FancyValidator):
 

	
 
    def to_python(self, value, state):
 
@@ -402,6 +433,7 @@ class ValidSettings(formencode.validator
 

	
 
        return value
 

	
 

	
 
class ValidPath(formencode.validators.FancyValidator):
 
    def to_python(self, value, state):
 

	
 
@@ -411,6 +443,7 @@ class ValidPath(formencode.validators.Fa
 
                                     error_dict={'paths_root_path':msg})
 
        return value
 

	
 

	
 
def UniqSystemEmail(old_data):
 
    class _UniqSystemEmail(formencode.validators.FancyValidator):
 
        def to_python(self, value, state):
 
@@ -425,6 +458,7 @@ def UniqSystemEmail(old_data):
 

	
 
    return _UniqSystemEmail
 

	
 

	
 
class ValidSystemEmail(formencode.validators.FancyValidator):
 
    def to_python(self, value, state):
 
        value = value.lower()
 
@@ -435,6 +469,7 @@ class ValidSystemEmail(formencode.valida
 

	
 
        return value
 

	
 

	
 
class LdapLibValidator(formencode.validators.FancyValidator):
 

	
 
    def to_python(self, value, state):
 
@@ -445,6 +480,7 @@ class LdapLibValidator(formencode.valida
 
            raise LdapImportError
 
        return value
 

	
 

	
 
class AttrLoginValidator(formencode.validators.FancyValidator):
 

	
 
    def to_python(self, value, state):
 
@@ -458,9 +494,9 @@ class AttrLoginValidator(formencode.vali
 

	
 
        return value
 

	
 
#===============================================================================
 
#==============================================================================
 
# FORMS
 
#===============================================================================
 
#==============================================================================
 
class LoginForm(formencode.Schema):
 
    allow_extra_fields = True
 
    filter_extra_fields = True
 
@@ -486,6 +522,7 @@ class LoginForm(formencode.Schema):
 

	
 
    chained_validators = [ValidAuth]
 

	
 

	
 
def UserForm(edit=False, old_data={}):
 
    class _UserForm(formencode.Schema):
 
        allow_extra_fields = True
 
@@ -527,6 +564,7 @@ def UsersGroupForm(edit=False, old_data=
 

	
 
    return _UsersGroupForm
 

	
 

	
 
def ReposGroupForm(edit=False, old_data={}, available_groups=[]):
 
    class _ReposGroupForm(formencode.Schema):
 
        allow_extra_fields = True
 
@@ -544,6 +582,7 @@ def ReposGroupForm(edit=False, old_data=
 

	
 
    return _ReposGroupForm
 

	
 

	
 
def RegisterForm(edit=False, old_data={}):
 
    class _RegisterForm(formencode.Schema):
 
        allow_extra_fields = True
0 comments (0 inline, 0 general)