comparison 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
comparison
equal deleted inserted replaced
3879:51596d9ef2f8 3960:5293d4bbb1ea
9 from pylons.i18n.translation import _ 9 from pylons.i18n.translation import _
10 from webhelpers.pylonslib.secure_form import authentication_token 10 from webhelpers.pylonslib.secure_form import authentication_token
11 11
12 from formencode.validators import ( 12 from formencode.validators import (
13 UnicodeString, OneOf, Int, Number, Regex, Email, Bool, StringBoolean, Set, 13 UnicodeString, OneOf, Int, Number, Regex, Email, Bool, StringBoolean, Set,
14 NotEmpty, IPAddress, CIDR 14 NotEmpty, IPAddress, CIDR, String, FancyValidator
15 ) 15 )
16 from rhodecode.lib.compat import OrderedSet 16 from rhodecode.lib.compat import OrderedSet
17 from rhodecode.lib import ipaddr 17 from rhodecode.lib import ipaddr
18 from rhodecode.lib.utils import repo_name_slug 18 from rhodecode.lib.utils import repo_name_slug
19 from rhodecode.lib.utils2 import safe_int, str2bool 19 from rhodecode.lib.utils2 import safe_int, str2bool
23 from rhodecode.config.routing import ADMIN_PREFIX 23 from rhodecode.config.routing import ADMIN_PREFIX
24 from rhodecode.lib.auth import HasReposGroupPermissionAny, HasPermissionAny 24 from rhodecode.lib.auth import HasReposGroupPermissionAny, HasPermissionAny
25 25
26 # silence warnings and pylint 26 # silence warnings and pylint
27 UnicodeString, OneOf, Int, Number, Regex, Email, Bool, StringBoolean, Set, \ 27 UnicodeString, OneOf, Int, Number, Regex, Email, Bool, StringBoolean, Set, \
28 NotEmpty, IPAddress, CIDR 28 NotEmpty, IPAddress, CIDR, String, FancyValidator
29 29
30 log = logging.getLogger(__name__) 30 log = logging.getLogger(__name__)
31 31
32 32
33 class UniqueList(formencode.FancyValidator): 33 class UniqueList(formencode.FancyValidator):
85 'username_exists': _(u'Username "%(username)s" already exists'), 85 'username_exists': _(u'Username "%(username)s" already exists'),
86 'system_invalid_username': 86 'system_invalid_username':
87 _(u'Username "%(username)s" is forbidden'), 87 _(u'Username "%(username)s" is forbidden'),
88 'invalid_username': 88 'invalid_username':
89 _(u'Username may only contain alphanumeric characters ' 89 _(u'Username may only contain alphanumeric characters '
90 'underscores, periods or dashes and must begin with ' 90 'underscores, periods or dashes and must begin with '
91 'alphanumeric character') 91 'alphanumeric character or underscore')
92 } 92 }
93 93
94 def validate_python(self, value, state): 94 def validate_python(self, value, state):
95 if value in ['default', 'new_user']: 95 if value in ['default', 'new_user']:
96 msg = M(self, 'system_invalid_username', state, username=value) 96 msg = M(self, 'system_invalid_username', state, username=value)
103 if old_un != value or not edit: 103 if old_un != value or not edit:
104 if User.get_by_username(value, case_insensitive=True): 104 if User.get_by_username(value, case_insensitive=True):
105 msg = M(self, 'username_exists', state, username=value) 105 msg = M(self, 'username_exists', state, username=value)
106 raise formencode.Invalid(msg, value, state) 106 raise formencode.Invalid(msg, value, state)
107 107
108 if re.match(r'^[a-zA-Z0-9]{1}[a-zA-Z0-9\-\_\.]*$', value) is None: 108 if re.match(r'^[a-zA-Z0-9\_]{1}[a-zA-Z0-9\-\_\.]*$', value) is None:
109 msg = M(self, 'invalid_username', state) 109 msg = M(self, 'invalid_username', state)
110 raise formencode.Invalid(msg, value, state) 110 raise formencode.Invalid(msg, value, state)
111 return _validator 111 return _validator
112 112
113 113
404 from rhodecode.lib.utils import make_ui 404 from rhodecode.lib.utils import make_ui
405 405
406 def url_handler(repo_type, url, ui=None): 406 def url_handler(repo_type, url, ui=None):
407 if repo_type == 'hg': 407 if repo_type == 'hg':
408 from rhodecode.lib.vcs.backends.hg.repository import MercurialRepository 408 from rhodecode.lib.vcs.backends.hg.repository import MercurialRepository
409 from mercurial.httppeer import httppeer 409 from rhodecode.lib.vcs.utils.hgcompat import httppeer
410 if url.startswith('http'): 410 if url.startswith('http'):
411 ## initially check if it's at least the proper URL 411 ## initially check if it's at least the proper URL
412 ## or does it pass basic auth 412 ## or does it pass basic auth
413 MercurialRepository._check_url(url) 413 MercurialRepository._check_url(url)
414 httppeer(ui, url)._capabilities() 414 httppeer(ui, url)._capabilities()
416 from hgsubversion.svnrepo import svnremoterepo 416 from hgsubversion.svnrepo import svnremoterepo
417 svnremoterepo(ui, url).capabilities 417 svnremoterepo(ui, url).capabilities
418 elif url.startswith('git+http'): 418 elif url.startswith('git+http'):
419 raise NotImplementedError() 419 raise NotImplementedError()
420 else: 420 else:
421 raise Exception('clone from URI %s not allowed' % (url)) 421 raise Exception('clone from URI %s not allowed' % (url,))
422 422
423 elif repo_type == 'git': 423 elif repo_type == 'git':
424 from rhodecode.lib.vcs.backends.git.repository import GitRepository 424 from rhodecode.lib.vcs.backends.git.repository import GitRepository
425 if url.startswith('http'): 425 if url.startswith('http'):
426 ## initially check if it's at least the proper URL 426 ## initially check if it's at least the proper URL
544 544
545 return _validator 545 return _validator
546 546
547 547
548 def ValidPerms(type_='repo'): 548 def ValidPerms(type_='repo'):
549 if type_ == 'group': 549 if type_ == 'repo_group':
550 EMPTY_PERM = 'group.none' 550 EMPTY_PERM = 'group.none'
551 elif type_ == 'repo': 551 elif type_ == 'repo':
552 EMPTY_PERM = 'repository.none' 552 EMPTY_PERM = 'repository.none'
553 elif type_ == 'user_group':
554 EMPTY_PERM = 'usergroup.none'
553 555
554 class _validator(formencode.validators.FancyValidator): 556 class _validator(formencode.validators.FancyValidator):
555 messages = { 557 messages = {
556 'perm_new_member_name': 558 'perm_new_member_name':
557 _(u'This username or user group name is not valid') 559 _(u'This username or user group name is not valid')
764 def ValidIp(): 766 def ValidIp():
765 class _validator(CIDR): 767 class _validator(CIDR):
766 messages = dict( 768 messages = dict(
767 badFormat=_('Please enter a valid IPv4 or IpV6 address'), 769 badFormat=_('Please enter a valid IPv4 or IpV6 address'),
768 illegalBits=_('The network size (bits) must be within the range' 770 illegalBits=_('The network size (bits) must be within the range'
769 ' of 0-32 (not %(bits)r)')) 771 ' of 0-32 (not %(bits)r)')
772 )
770 773
771 def to_python(self, value, state): 774 def to_python(self, value, state):
772 v = super(_validator, self).to_python(value, state) 775 v = super(_validator, self).to_python(value, state)
773 v = v.strip() 776 v = v.strip()
774 net = ipaddr.IPNetwork(address=v) 777 net = ipaddr.IPNetwork(address=v)
796 799
797 def FieldKey(): 800 def FieldKey():
798 class _validator(formencode.validators.FancyValidator): 801 class _validator(formencode.validators.FancyValidator):
799 messages = dict( 802 messages = dict(
800 badFormat=_('Key name can only consist of letters, ' 803 badFormat=_('Key name can only consist of letters, '
801 'underscore, dash or numbers'),) 804 'underscore, dash or numbers')
805 )
802 806
803 def validate_python(self, value, state): 807 def validate_python(self, value, state):
804 if not re.match('[a-zA-Z0-9_-]+$', value): 808 if not re.match('[a-zA-Z0-9_-]+$', value):
805 raise formencode.Invalid(self.message('badFormat', state), 809 raise formencode.Invalid(self.message('badFormat', state),
806 value, state) 810 value, state)
807 return _validator 811 return _validator
812
813
814 def BasePath():
815 class _validator(formencode.validators.FancyValidator):
816 messages = dict(
817 badPath=_('Filename cannot be inside a directory')
818 )
819
820 def _to_python(self, value, state):
821 return value
822
823 def validate_python(self, value, state):
824 if value != os.path.basename(value):
825 raise formencode.Invalid(self.message('badPath', state),
826 value, state)
827 return _validator