diff rhodecode/model/validators.py @ 3960:5293d4bbb1ea

Merged dev into stable/default/master branch
author Marcin Kuzminski <marcin@python-works.com>
date Fri, 07 Jun 2013 00:31:11 +0200
parents 3563bb7b4b82 3208aaefc9ca
children ffd45b185016
line wrap: on
line diff
--- a/rhodecode/model/validators.py	Mon May 20 12:26:09 2013 +0200
+++ b/rhodecode/model/validators.py	Fri Jun 07 00:31:11 2013 +0200
@@ -11,7 +11,7 @@
 
 from formencode.validators import (
     UnicodeString, OneOf, Int, Number, Regex, Email, Bool, StringBoolean, Set,
-    NotEmpty, IPAddress, CIDR
+    NotEmpty, IPAddress, CIDR, String, FancyValidator
 )
 from rhodecode.lib.compat import OrderedSet
 from rhodecode.lib import ipaddr
@@ -25,7 +25,7 @@
 
 # silence warnings and pylint
 UnicodeString, OneOf, Int, Number, Regex, Email, Bool, StringBoolean, Set, \
-    NotEmpty, IPAddress, CIDR
+    NotEmpty, IPAddress, CIDR, String, FancyValidator
 
 log = logging.getLogger(__name__)
 
@@ -87,8 +87,8 @@
                 _(u'Username "%(username)s" is forbidden'),
             'invalid_username':
                 _(u'Username may only contain alphanumeric characters '
-                  'underscores, periods or dashes and must begin with '
-                  'alphanumeric character')
+                    'underscores, periods or dashes and must begin with '
+                    'alphanumeric character or underscore')
         }
 
         def validate_python(self, value, state):
@@ -105,7 +105,7 @@
                     msg = M(self, 'username_exists', state, username=value)
                     raise formencode.Invalid(msg, value, state)
 
-            if re.match(r'^[a-zA-Z0-9]{1}[a-zA-Z0-9\-\_\.]*$', value) is None:
+            if re.match(r'^[a-zA-Z0-9\_]{1}[a-zA-Z0-9\-\_\.]*$', value) is None:
                 msg = M(self, 'invalid_username', state)
                 raise formencode.Invalid(msg, value, state)
     return _validator
@@ -406,7 +406,7 @@
     def url_handler(repo_type, url, ui=None):
         if repo_type == 'hg':
             from rhodecode.lib.vcs.backends.hg.repository import MercurialRepository
-            from mercurial.httppeer import httppeer
+            from rhodecode.lib.vcs.utils.hgcompat import httppeer
             if url.startswith('http'):
                 ## initially check if it's at least the proper URL
                 ## or does it pass basic auth
@@ -418,7 +418,7 @@
             elif url.startswith('git+http'):
                 raise NotImplementedError()
             else:
-                raise Exception('clone from URI %s not allowed' % (url))
+                raise Exception('clone from URI %s not allowed' % (url,))
 
         elif repo_type == 'git':
             from rhodecode.lib.vcs.backends.git.repository import GitRepository
@@ -546,10 +546,12 @@
 
 
 def ValidPerms(type_='repo'):
-    if type_ == 'group':
+    if type_ == 'repo_group':
         EMPTY_PERM = 'group.none'
     elif type_ == 'repo':
         EMPTY_PERM = 'repository.none'
+    elif type_ == 'user_group':
+        EMPTY_PERM = 'usergroup.none'
 
     class _validator(formencode.validators.FancyValidator):
         messages = {
@@ -766,7 +768,8 @@
         messages = dict(
             badFormat=_('Please enter a valid IPv4 or IpV6 address'),
             illegalBits=_('The network size (bits) must be within the range'
-                ' of 0-32 (not %(bits)r)'))
+                ' of 0-32 (not %(bits)r)')
+        )
 
         def to_python(self, value, state):
             v = super(_validator, self).to_python(value, state)
@@ -798,10 +801,27 @@
     class _validator(formencode.validators.FancyValidator):
         messages = dict(
             badFormat=_('Key name can only consist of letters, '
-                        'underscore, dash or numbers'),)
+                        'underscore, dash or numbers')
+        )
 
         def validate_python(self, value, state):
             if not re.match('[a-zA-Z0-9_-]+$', value):
                 raise formencode.Invalid(self.message('badFormat', state),
                                          value, state)
     return _validator
+
+
+def BasePath():
+    class _validator(formencode.validators.FancyValidator):
+        messages = dict(
+            badPath=_('Filename cannot be inside a directory')
+        )
+
+        def _to_python(self, value, state):
+            return value
+
+        def validate_python(self, value, state):
+            if value != os.path.basename(value):
+                raise formencode.Invalid(self.message('badPath', state),
+                                         value, state)
+    return _validator