Mercurial > kallithea
changeset 1898:a7dfe823933a beta
added validation to repo groups to check for conflicting repository name fixes #337
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Sat, 14 Jan 2012 00:56:37 +0200 |
parents | 0f22dedb588f |
children | 50546fdf75c2 |
files | rhodecode/controllers/admin/repos_groups.py rhodecode/model/forms.py |
diffstat | 2 files changed, 52 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/rhodecode/controllers/admin/repos_groups.py Fri Jan 13 21:31:01 2012 +0200 +++ b/rhodecode/controllers/admin/repos_groups.py Sat Jan 14 00:56:37 2012 +0200 @@ -81,7 +81,7 @@ """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 @@ """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 @@ return redirect(url('repos_groups')) - @HasPermissionAnyDecorator('hg.admin') def new(self, format='html'): """GET /repos_groups/new: Form to create a new item"""
--- a/rhodecode/model/forms.py Fri Jan 13 21:31:01 2012 +0200 +++ b/rhodecode/model/forms.py Sat Jan 14 00:56:37 2012 +0200 @@ -41,10 +41,12 @@ log = logging.getLogger(__name__) + #this is needed to translate the messages using _() in validators class State_obj(object): _ = staticmethod(_) + #============================================================================== # VALIDATORS #============================================================================== @@ -57,6 +59,7 @@ 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 @@ '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 @@ 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 @@ 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 @@ return value + class ValidPasswordsMatch(formencode.validators.FancyValidator): def validate_python(self, value, state): @@ -192,6 +215,7 @@ _('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 @@ 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 @@ value, state) return value + def ValidRepoName(edit, old_data): class _ValidRepoName(formencode.validators.FancyValidator): def to_python(self, value, state): @@ -289,6 +315,7 @@ return _ValidRepoName + def ValidForkName(*args, **kwargs): return ValidRepoName(*args, **kwargs) @@ -301,6 +328,7 @@ return _SlugifyName + def ValidCloneUri(): from mercurial.httprepo import httprepository, httpsrepository from rhodecode.lib.utils import make_ui @@ -332,6 +360,7 @@ return _ValidCloneUri + def ValidForkType(old_data): class _ValidForkType(formencode.validators.FancyValidator): @@ -343,6 +372,7 @@ 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 @@ error_dict={'perm_new_member_name':msg}) return value + class ValidSettings(formencode.validators.FancyValidator): def to_python(self, value, state): @@ -402,6 +433,7 @@ return value + class ValidPath(formencode.validators.FancyValidator): def to_python(self, value, state): @@ -411,6 +443,7 @@ 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 @@ return _UniqSystemEmail + class ValidSystemEmail(formencode.validators.FancyValidator): def to_python(self, value, state): value = value.lower() @@ -435,6 +469,7 @@ return value + class LdapLibValidator(formencode.validators.FancyValidator): def to_python(self, value, state): @@ -445,6 +480,7 @@ raise LdapImportError return value + class AttrLoginValidator(formencode.validators.FancyValidator): def to_python(self, value, state): @@ -458,9 +494,9 @@ return value -#=============================================================================== +#============================================================================== # FORMS -#=============================================================================== +#============================================================================== class LoginForm(formencode.Schema): allow_extra_fields = True filter_extra_fields = True @@ -486,6 +522,7 @@ chained_validators = [ValidAuth] + def UserForm(edit=False, old_data={}): class _UserForm(formencode.Schema): allow_extra_fields = True @@ -527,6 +564,7 @@ return _UsersGroupForm + def ReposGroupForm(edit=False, old_data={}, available_groups=[]): class _ReposGroupForm(formencode.Schema): allow_extra_fields = True @@ -544,6 +582,7 @@ return _ReposGroupForm + def RegisterForm(edit=False, old_data={}): class _RegisterForm(formencode.Schema): allow_extra_fields = True