##// END OF EJS Templates
form-validators: fixed usage of deprecated methods, and few py3 fixes
super-admin -
r5020:c2969528 default
parent child Browse files
Show More
@@ -44,7 +44,9 b' from rhodecode.authentication.base impor'
44 from rhodecode.apps._base import ADMIN_PREFIX
44 from rhodecode.apps._base import ADMIN_PREFIX
45 from rhodecode.lib.auth import HasRepoGroupPermissionAny, HasPermissionAny
45 from rhodecode.lib.auth import HasRepoGroupPermissionAny, HasPermissionAny
46 from rhodecode.lib.utils import repo_name_slug, make_db_config
46 from rhodecode.lib.utils import repo_name_slug, make_db_config
47 from rhodecode.lib.utils2 import safe_int, str2bool, aslist, md5, safe_unicode
47 from rhodecode.lib.utils2 import safe_int, str2bool, aslist
48 from rhodecode.lib.str_utils import safe_str
49 from rhodecode.lib.hash_utils import md5_safe
48 from rhodecode.lib.vcs.backends.git.repository import GitRepository
50 from rhodecode.lib.vcs.backends.git.repository import GitRepository
49 from rhodecode.lib.vcs.backends.hg.repository import MercurialRepository
51 from rhodecode.lib.vcs.backends.hg.repository import MercurialRepository
50 from rhodecode.lib.vcs.backends.svn.repository import SubversionRepository
52 from rhodecode.lib.vcs.backends.svn.repository import SubversionRepository
@@ -93,7 +95,7 b' def UniqueList(localizer, convert=None):'
93 'missing_value': _('Value cannot be an empty list'),
95 'missing_value': _('Value cannot be an empty list'),
94 }
96 }
95
97
96 def _to_python(self, value, state):
98 def _convert_to_python(self, value, state):
97 ret_val = []
99 ret_val = []
98
100
99 def make_unique(value):
101 def make_unique(value):
@@ -112,7 +114,7 b' def UniqueList(localizer, convert=None):'
112 ret_val = [value]
114 ret_val = [value]
113
115
114 if convert:
116 if convert:
115 ret_val = map(convert, ret_val)
117 ret_val = list(map(convert, ret_val))
116 return ret_val
118 return ret_val
117
119
118 def empty_value(self, value):
120 def empty_value(self, value):
@@ -124,10 +126,10 b' def UniqueListFromString(localizer):'
124 _ = localizer
126 _ = localizer
125
127
126 class _validator(UniqueList(localizer)):
128 class _validator(UniqueList(localizer)):
127 def _to_python(self, value, state):
129 def _convert_to_python(self, value, state):
128 if isinstance(value, str):
130 if isinstance(value, str):
129 value = aslist(value, ',')
131 value = aslist(value, ',')
130 return super(_validator, self)._to_python(value, state)
132 return super(_validator, self)._convert_to_python(value, state)
131 return _validator
133 return _validator
132
134
133
135
@@ -139,7 +141,7 b' def ValidSvnPattern(localizer, section, '
139 'pattern_exists': _('Pattern already exists'),
141 'pattern_exists': _('Pattern already exists'),
140 }
142 }
141
143
142 def validate_python(self, value, state):
144 def _validate_python(self, value, state):
143 if not value:
145 if not value:
144 return
146 return
145 model = VcsSettingsModel(repo=repo_name)
147 model = VcsSettingsModel(repo=repo_name)
@@ -166,7 +168,7 b' def ValidUsername(localizer, edit=False,'
166 'alphanumeric character or underscore')
168 'alphanumeric character or underscore')
167 }
169 }
168
170
169 def validate_python(self, value, state):
171 def _validate_python(self, value, state):
170 if value in ['default', 'new_user']:
172 if value in ['default', 'new_user']:
171 msg = M(self, 'system_invalid_username', state, username=value)
173 msg = M(self, 'system_invalid_username', state, username=value)
172 raise formencode.Invalid(msg, value, state)
174 raise formencode.Invalid(msg, value, state)
@@ -196,7 +198,7 b' def ValidRepoUser(localizer, allow_disab'
196 'disabled_username': _('Username %(username)s is disabled')
198 'disabled_username': _('Username %(username)s is disabled')
197 }
199 }
198
200
199 def validate_python(self, value, state):
201 def _validate_python(self, value, state):
200 try:
202 try:
201 user = User.query().filter(User.username == value).one()
203 user = User.query().filter(User.username == value).one()
202 except Exception:
204 except Exception:
@@ -226,7 +228,7 b' def ValidUserGroup(localizer, edit=False'
226 'with alphanumeric character')
228 'with alphanumeric character')
227 }
229 }
228
230
229 def validate_python(self, value, state):
231 def _validate_python(self, value, state):
230 if value in ['default']:
232 if value in ['default']:
231 msg = M(self, 'invalid_group', state)
233 msg = M(self, 'invalid_group', state)
232 raise formencode.Invalid(
234 raise formencode.Invalid(
@@ -272,7 +274,7 b' def ValidRepoGroup(localizer, edit=False'
272 "in root location")
274 "in root location")
273 }
275 }
274
276
275 def _to_python(self, value, state):
277 def _convert_to_python(self, value, state):
276 group_name = repo_name_slug(value.get('group_name', ''))
278 group_name = repo_name_slug(value.get('group_name', ''))
277 group_parent_id = safe_int(value.get('group_parent_id'))
279 group_parent_id = safe_int(value.get('group_parent_id'))
278 gr = RepoGroup.get(group_parent_id)
280 gr = RepoGroup.get(group_parent_id)
@@ -291,7 +293,7 b' def ValidRepoGroup(localizer, edit=False'
291 value['group_parent_id'] = group_parent_id
293 value['group_parent_id'] = group_parent_id
292 return value
294 return value
293
295
294 def validate_python(self, value, state):
296 def _validate_python(self, value, state):
295
297
296 old_group_name = None
298 old_group_name = None
297 group_name = value.get('group_name')
299 group_name = value.get('group_name')
@@ -387,10 +389,8 b' def ValidPassword(localizer):'
387 _('Invalid characters (non-ascii) in password')
389 _('Invalid characters (non-ascii) in password')
388 }
390 }
389
391
390 def validate_python(self, value, state):
392 def _validate_python(self, value, state):
391 try:
393 if value and not value.isascii():
392 (value or '').decode('ascii')
393 except UnicodeError:
394 msg = M(self, 'invalid_password', state)
394 msg = M(self, 'invalid_password', state)
395 raise formencode.Invalid(msg, value, state,)
395 raise formencode.Invalid(msg, value, state,)
396 return _validator
396 return _validator
@@ -406,7 +406,7 b' def ValidPasswordsMatch('
406 'password_mismatch': _('Passwords do not match'),
406 'password_mismatch': _('Passwords do not match'),
407 }
407 }
408
408
409 def validate_python(self, value, state):
409 def _validate_python(self, value, state):
410
410
411 pass_val = value.get('password') or value.get(passwd)
411 pass_val = value.get('password') or value.get(passwd)
412 if pass_val != value[passwd_confirmation]:
412 if pass_val != value[passwd_confirmation]:
@@ -428,7 +428,7 b' def ValidAuth(localizer):'
428 'disabled_account': _('Your account is disabled')
428 'disabled_account': _('Your account is disabled')
429 }
429 }
430
430
431 def validate_python(self, value, state):
431 def _validate_python(self, value, state):
432 from rhodecode.authentication.base import authenticate, HTTP_TYPE
432 from rhodecode.authentication.base import authenticate, HTTP_TYPE
433
433
434 password = value['password']
434 password = value['password']
@@ -475,7 +475,7 b' def ValidRepoName(localizer, edit=False,'
475 'exists in group "%(group)s"'),
475 'exists in group "%(group)s"'),
476 }
476 }
477
477
478 def _to_python(self, value, state):
478 def _convert_to_python(self, value, state):
479 repo_name = repo_name_slug(value.get('repo_name', ''))
479 repo_name = repo_name_slug(value.get('repo_name', ''))
480 repo_group = value.get('repo_group')
480 repo_group = value.get('repo_group')
481 if repo_group:
481 if repo_group:
@@ -496,7 +496,7 b' def ValidRepoName(localizer, edit=False,'
496 value['group_name'] = group_name
496 value['group_name'] = group_name
497 return value
497 return value
498
498
499 def validate_python(self, value, state):
499 def _validate_python(self, value, state):
500
500
501 repo_name = value.get('repo_name')
501 repo_name = value.get('repo_name')
502 repo_name_full = value.get('repo_name_full')
502 repo_name_full = value.get('repo_name_full')
@@ -549,10 +549,10 b' def SlugifyName(localizer):'
549
549
550 class _validator(formencode.validators.FancyValidator):
550 class _validator(formencode.validators.FancyValidator):
551
551
552 def _to_python(self, value, state):
552 def _convert_to_python(self, value, state):
553 return repo_name_slug(value)
553 return repo_name_slug(value)
554
554
555 def validate_python(self, value, state):
555 def _validate_python(self, value, state):
556 pass
556 pass
557 return _validator
557 return _validator
558
558
@@ -566,10 +566,10 b' def CannotHaveGitSuffix(localizer):'
566 _('Repository name cannot end with .git'),
566 _('Repository name cannot end with .git'),
567 }
567 }
568
568
569 def _to_python(self, value, state):
569 def _convert_to_python(self, value, state):
570 return value
570 return value
571
571
572 def validate_python(self, value, state):
572 def _validate_python(self, value, state):
573 if value and value.endswith('.git'):
573 if value and value.endswith('.git'):
574 msg = M(
574 msg = M(
575 self, 'has_git_suffix', state)
575 self, 'has_git_suffix', state)
@@ -629,7 +629,7 b' def ValidCloneUri(localizer):'
629 'url starting with one of %(allowed_prefixes)s')
629 'url starting with one of %(allowed_prefixes)s')
630 }
630 }
631
631
632 def validate_python(self, value, state):
632 def _validate_python(self, value, state):
633 repo_type = value.get('repo_type')
633 repo_type = value.get('repo_type')
634 url = value.get('clone_uri')
634 url = value.get('clone_uri')
635
635
@@ -659,7 +659,7 b' def ValidForkType(localizer, old_data=No'
659 'invalid_fork_type': _('Fork have to be the same type as parent')
659 'invalid_fork_type': _('Fork have to be the same type as parent')
660 }
660 }
661
661
662 def validate_python(self, value, state):
662 def _validate_python(self, value, state):
663 if old_data['repo_type'] != value:
663 if old_data['repo_type'] != value:
664 msg = M(self, 'invalid_fork_type', state)
664 msg = M(self, 'invalid_fork_type', state)
665 raise formencode.Invalid(
665 raise formencode.Invalid(
@@ -681,13 +681,13 b' def CanWriteGroup(localizer, old_data=No'
681 "the root location.")
681 "the root location.")
682 }
682 }
683
683
684 def _to_python(self, value, state):
684 def _convert_to_python(self, value, state):
685 # root location
685 # root location
686 if value in [-1, "-1"]:
686 if value in [-1, "-1"]:
687 return None
687 return None
688 return value
688 return value
689
689
690 def validate_python(self, value, state):
690 def _validate_python(self, value, state):
691 gr = RepoGroup.get(value)
691 gr = RepoGroup.get(value)
692 gr_name = gr.group_name if gr else None # None means ROOT location
692 gr_name = gr.group_name if gr else None # None means ROOT location
693 # create repositories with write permission on group is set to true
693 # create repositories with write permission on group is set to true
@@ -739,7 +739,7 b" def ValidPerms(localizer, type_='repo'):"
739 _('This username or user group name is not valid')
739 _('This username or user group name is not valid')
740 }
740 }
741
741
742 def _to_python(self, value, state):
742 def _convert_to_python(self, value, state):
743 perm_updates = OrderedSet()
743 perm_updates = OrderedSet()
744 perm_additions = OrderedSet()
744 perm_additions = OrderedSet()
745 perm_deletions = OrderedSet()
745 perm_deletions = OrderedSet()
@@ -749,7 +749,7 b" def ValidPerms(localizer, type_='repo'):"
749 # them by they IDs
749 # them by they IDs
750 new_perms_group = collections.defaultdict(dict)
750 new_perms_group = collections.defaultdict(dict)
751 del_perms_group = collections.defaultdict(dict)
751 del_perms_group = collections.defaultdict(dict)
752 for k, v in value.copy().items():
752 for k, v in list(value.copy().items()):
753 if k.startswith('perm_del_member'):
753 if k.startswith('perm_del_member'):
754 # delete from org storage so we don't process that later
754 # delete from org storage so we don't process that later
755 del value[k]
755 del value[k]
@@ -792,7 +792,7 b" def ValidPerms(localizer, type_='repo'):"
792 # (read the existing radio button states)
792 # (read the existing radio button states)
793 default_user_id = User.get_default_user_id()
793 default_user_id = User.get_default_user_id()
794
794
795 for k, update_value in value.items():
795 for k, update_value in list(value.items()):
796 if k.startswith('u_perm_') or k.startswith('g_perm_'):
796 if k.startswith('u_perm_') or k.startswith('g_perm_'):
797 obj_type = k[0]
797 obj_type = k[0]
798 obj_id = k[7:]
798 obj_id = k[7:]
@@ -852,7 +852,7 b' def ValidPath(localizer):'
852 'invalid_path': _('This is not a valid path')
852 'invalid_path': _('This is not a valid path')
853 }
853 }
854
854
855 def validate_python(self, value, state):
855 def _validate_python(self, value, state):
856 if not os.path.isdir(value):
856 if not os.path.isdir(value):
857 msg = M(self, 'invalid_path', state)
857 msg = M(self, 'invalid_path', state)
858 raise formencode.Invalid(
858 raise formencode.Invalid(
@@ -870,10 +870,10 b' def UniqSystemEmail(localizer, old_data='
870 'email_taken': _('This e-mail address is already taken')
870 'email_taken': _('This e-mail address is already taken')
871 }
871 }
872
872
873 def _to_python(self, value, state):
873 def _convert_to_python(self, value, state):
874 return value.lower()
874 return value.lower()
875
875
876 def validate_python(self, value, state):
876 def _validate_python(self, value, state):
877 if (old_data.get('email') or '').lower() != value:
877 if (old_data.get('email') or '').lower() != value:
878 user = User.get_by_email(value, case_insensitive=True)
878 user = User.get_by_email(value, case_insensitive=True)
879 if user:
879 if user:
@@ -892,10 +892,10 b' def ValidSystemEmail(localizer):'
892 'non_existing_email': _('e-mail "%(email)s" does not exist.')
892 'non_existing_email': _('e-mail "%(email)s" does not exist.')
893 }
893 }
894
894
895 def _to_python(self, value, state):
895 def _convert_to_python(self, value, state):
896 return value.lower()
896 return value.lower()
897
897
898 def validate_python(self, value, state):
898 def _validate_python(self, value, state):
899 user = User.get_by_email(value, case_insensitive=True)
899 user = User.get_by_email(value, case_insensitive=True)
900 if user is None:
900 if user is None:
901 msg = M(self, 'non_existing_email', state, email=value)
901 msg = M(self, 'non_existing_email', state, email=value)
@@ -914,7 +914,7 b' def NotReviewedRevisions(localizer, repo'
914 'or have set status'),
914 'or have set status'),
915 }
915 }
916
916
917 def validate_python(self, value, state):
917 def _validate_python(self, value, state):
918 # check revisions if they are not reviewed, or a part of another
918 # check revisions if they are not reviewed, or a part of another
919 # pull request
919 # pull request
920 statuses = ChangesetStatus.query()\
920 statuses = ChangesetStatus.query()\
@@ -949,16 +949,16 b' def ValidIp(localizer):'
949 'of 0-32 (not %(bits)r)'),
949 'of 0-32 (not %(bits)r)'),
950 }
950 }
951
951
952 # we ovveride the default to_python() call
952 # we override the default to_python() call
953 def to_python(self, value, state):
953 def to_python(self, value, state):
954 v = super(_validator, self).to_python(value, state)
954 v = super(_validator, self).to_python(value, state)
955 v = safe_unicode(v.strip())
955 v = safe_str(v.strip())
956 net = ipaddress.ip_network(address=v, strict=False)
956 net = ipaddress.ip_network(address=v, strict=False)
957 return str(net)
957 return str(net)
958
958
959 def validate_python(self, value, state):
959 def _validate_python(self, value, state):
960 try:
960 try:
961 addr = safe_unicode(value.strip())
961 addr = safe_str(value.strip())
962 # this raises an ValueError if address is not IpV4 or IpV6
962 # this raises an ValueError if address is not IpV4 or IpV6
963 ipaddress.ip_network(addr, strict=False)
963 ipaddress.ip_network(addr, strict=False)
964 except ValueError:
964 except ValueError:
@@ -977,7 +977,7 b' def FieldKey(localizer):'
977 'underscore, dash or numbers'),
977 'underscore, dash or numbers'),
978 }
978 }
979
979
980 def validate_python(self, value, state):
980 def _validate_python(self, value, state):
981 if not re.match('[a-zA-Z0-9_-]+$', value):
981 if not re.match('[a-zA-Z0-9_-]+$', value):
982 raise formencode.Invalid(self.message('badFormat', state),
982 raise formencode.Invalid(self.message('badFormat', state),
983 value, state)
983 value, state)
@@ -1001,9 +1001,9 b' def ValidAuthPlugins(localizer):'
1001 'No plugin available with ID "%(plugin_id)s"'),
1001 'No plugin available with ID "%(plugin_id)s"'),
1002 }
1002 }
1003
1003
1004 def _to_python(self, value, state):
1004 def _convert_to_python(self, value, state):
1005 # filter empty values
1005 # filter empty values
1006 return filter(lambda s: s not in [None, ''], value)
1006 return [s for s in value if s not in [None, '']]
1007
1007
1008 def _validate_legacy_plugin_id(self, plugin_id, value, state):
1008 def _validate_legacy_plugin_id(self, plugin_id, value, state):
1009 """
1009 """
@@ -1039,7 +1039,7 b' def ValidAuthPlugins(localizer):'
1039
1039
1040 return plugin
1040 return plugin
1041
1041
1042 def validate_python(self, value, state):
1042 def _validate_python(self, value, state):
1043 unique_names = {}
1043 unique_names = {}
1044 for plugin_id in value:
1044 for plugin_id in value:
1045
1045
@@ -1068,11 +1068,11 b' def ValidPattern(localizer):'
1068 'bad_format': _('Url must start with http or /'),
1068 'bad_format': _('Url must start with http or /'),
1069 }
1069 }
1070
1070
1071 def _to_python(self, value, state):
1071 def _convert_to_python(self, value, state):
1072 patterns = []
1072 patterns = []
1073
1073
1074 prefix = 'new_pattern'
1074 prefix = 'new_pattern'
1075 for name, v in value.items():
1075 for name, v in list(value.items()):
1076 pattern_name = '_'.join((prefix, 'pattern'))
1076 pattern_name = '_'.join((prefix, 'pattern'))
1077 if name.startswith(pattern_name):
1077 if name.startswith(pattern_name):
1078 new_item_id = name[len(pattern_name)+1:]
1078 new_item_id = name[len(pattern_name)+1:]
@@ -1086,7 +1086,7 b' def ValidPattern(localizer):'
1086 'issuetracker_pref': value.get(_field('prefix')),
1086 'issuetracker_pref': value.get(_field('prefix')),
1087 'issuetracker_desc': value.get(_field('description'))
1087 'issuetracker_desc': value.get(_field('description'))
1088 }
1088 }
1089 new_uid = md5(values['issuetracker_pat'])
1089 new_uid = md5_safe(values['issuetracker_pat'])
1090
1090
1091 has_required_fields = (
1091 has_required_fields = (
1092 values['issuetracker_pat']
1092 values['issuetracker_pat']
General Comments 0
You need to be logged in to leave comments. Login now