# HG changeset patch # User RhodeCode Admin # Date 2023-05-11 10:08:21 # Node ID c2969528ab1db630aa9f657cc21e0a3854e1614b # Parent 7f6920cead73260b4a22bef2a8f41ae25999a9a1 form-validators: fixed usage of deprecated methods, and few py3 fixes diff --git a/rhodecode/model/validators.py b/rhodecode/model/validators.py --- a/rhodecode/model/validators.py +++ b/rhodecode/model/validators.py @@ -44,7 +44,9 @@ from rhodecode.authentication.base impor from rhodecode.apps._base import ADMIN_PREFIX from rhodecode.lib.auth import HasRepoGroupPermissionAny, HasPermissionAny from rhodecode.lib.utils import repo_name_slug, make_db_config -from rhodecode.lib.utils2 import safe_int, str2bool, aslist, md5, safe_unicode +from rhodecode.lib.utils2 import safe_int, str2bool, aslist +from rhodecode.lib.str_utils import safe_str +from rhodecode.lib.hash_utils import md5_safe from rhodecode.lib.vcs.backends.git.repository import GitRepository from rhodecode.lib.vcs.backends.hg.repository import MercurialRepository from rhodecode.lib.vcs.backends.svn.repository import SubversionRepository @@ -93,7 +95,7 @@ def UniqueList(localizer, convert=None): 'missing_value': _('Value cannot be an empty list'), } - def _to_python(self, value, state): + def _convert_to_python(self, value, state): ret_val = [] def make_unique(value): @@ -112,7 +114,7 @@ def UniqueList(localizer, convert=None): ret_val = [value] if convert: - ret_val = map(convert, ret_val) + ret_val = list(map(convert, ret_val)) return ret_val def empty_value(self, value): @@ -124,10 +126,10 @@ def UniqueListFromString(localizer): _ = localizer class _validator(UniqueList(localizer)): - def _to_python(self, value, state): + def _convert_to_python(self, value, state): if isinstance(value, str): value = aslist(value, ',') - return super(_validator, self)._to_python(value, state) + return super(_validator, self)._convert_to_python(value, state) return _validator @@ -139,7 +141,7 @@ def ValidSvnPattern(localizer, section, 'pattern_exists': _('Pattern already exists'), } - def validate_python(self, value, state): + def _validate_python(self, value, state): if not value: return model = VcsSettingsModel(repo=repo_name) @@ -166,7 +168,7 @@ def ValidUsername(localizer, edit=False, 'alphanumeric character or underscore') } - def validate_python(self, value, state): + def _validate_python(self, value, state): if value in ['default', 'new_user']: msg = M(self, 'system_invalid_username', state, username=value) raise formencode.Invalid(msg, value, state) @@ -196,7 +198,7 @@ def ValidRepoUser(localizer, allow_disab 'disabled_username': _('Username %(username)s is disabled') } - def validate_python(self, value, state): + def _validate_python(self, value, state): try: user = User.query().filter(User.username == value).one() except Exception: @@ -226,7 +228,7 @@ def ValidUserGroup(localizer, edit=False 'with alphanumeric character') } - def validate_python(self, value, state): + def _validate_python(self, value, state): if value in ['default']: msg = M(self, 'invalid_group', state) raise formencode.Invalid( @@ -272,7 +274,7 @@ def ValidRepoGroup(localizer, edit=False "in root location") } - def _to_python(self, value, state): + def _convert_to_python(self, value, state): group_name = repo_name_slug(value.get('group_name', '')) group_parent_id = safe_int(value.get('group_parent_id')) gr = RepoGroup.get(group_parent_id) @@ -291,7 +293,7 @@ def ValidRepoGroup(localizer, edit=False value['group_parent_id'] = group_parent_id return value - def validate_python(self, value, state): + def _validate_python(self, value, state): old_group_name = None group_name = value.get('group_name') @@ -387,10 +389,8 @@ def ValidPassword(localizer): _('Invalid characters (non-ascii) in password') } - def validate_python(self, value, state): - try: - (value or '').decode('ascii') - except UnicodeError: + def _validate_python(self, value, state): + if value and not value.isascii(): msg = M(self, 'invalid_password', state) raise formencode.Invalid(msg, value, state,) return _validator @@ -406,7 +406,7 @@ def ValidPasswordsMatch( 'password_mismatch': _('Passwords do not match'), } - def validate_python(self, value, state): + def _validate_python(self, value, state): pass_val = value.get('password') or value.get(passwd) if pass_val != value[passwd_confirmation]: @@ -428,7 +428,7 @@ def ValidAuth(localizer): 'disabled_account': _('Your account is disabled') } - def validate_python(self, value, state): + def _validate_python(self, value, state): from rhodecode.authentication.base import authenticate, HTTP_TYPE password = value['password'] @@ -475,7 +475,7 @@ def ValidRepoName(localizer, edit=False, 'exists in group "%(group)s"'), } - def _to_python(self, value, state): + def _convert_to_python(self, value, state): repo_name = repo_name_slug(value.get('repo_name', '')) repo_group = value.get('repo_group') if repo_group: @@ -496,7 +496,7 @@ def ValidRepoName(localizer, edit=False, value['group_name'] = group_name return value - def validate_python(self, value, state): + def _validate_python(self, value, state): repo_name = value.get('repo_name') repo_name_full = value.get('repo_name_full') @@ -549,10 +549,10 @@ def SlugifyName(localizer): class _validator(formencode.validators.FancyValidator): - def _to_python(self, value, state): + def _convert_to_python(self, value, state): return repo_name_slug(value) - def validate_python(self, value, state): + def _validate_python(self, value, state): pass return _validator @@ -566,10 +566,10 @@ def CannotHaveGitSuffix(localizer): _('Repository name cannot end with .git'), } - def _to_python(self, value, state): + def _convert_to_python(self, value, state): return value - def validate_python(self, value, state): + def _validate_python(self, value, state): if value and value.endswith('.git'): msg = M( self, 'has_git_suffix', state) @@ -629,7 +629,7 @@ def ValidCloneUri(localizer): 'url starting with one of %(allowed_prefixes)s') } - def validate_python(self, value, state): + def _validate_python(self, value, state): repo_type = value.get('repo_type') url = value.get('clone_uri') @@ -659,7 +659,7 @@ def ValidForkType(localizer, old_data=No 'invalid_fork_type': _('Fork have to be the same type as parent') } - def validate_python(self, value, state): + def _validate_python(self, value, state): if old_data['repo_type'] != value: msg = M(self, 'invalid_fork_type', state) raise formencode.Invalid( @@ -681,13 +681,13 @@ def CanWriteGroup(localizer, old_data=No "the root location.") } - def _to_python(self, value, state): + def _convert_to_python(self, value, state): # root location if value in [-1, "-1"]: return None return value - def validate_python(self, value, state): + def _validate_python(self, value, state): gr = RepoGroup.get(value) gr_name = gr.group_name if gr else None # None means ROOT location # create repositories with write permission on group is set to true @@ -739,7 +739,7 @@ def ValidPerms(localizer, type_='repo'): _('This username or user group name is not valid') } - def _to_python(self, value, state): + def _convert_to_python(self, value, state): perm_updates = OrderedSet() perm_additions = OrderedSet() perm_deletions = OrderedSet() @@ -749,7 +749,7 @@ def ValidPerms(localizer, type_='repo'): # them by they IDs new_perms_group = collections.defaultdict(dict) del_perms_group = collections.defaultdict(dict) - for k, v in value.copy().items(): + for k, v in list(value.copy().items()): if k.startswith('perm_del_member'): # delete from org storage so we don't process that later del value[k] @@ -792,7 +792,7 @@ def ValidPerms(localizer, type_='repo'): # (read the existing radio button states) default_user_id = User.get_default_user_id() - for k, update_value in value.items(): + for k, update_value in list(value.items()): if k.startswith('u_perm_') or k.startswith('g_perm_'): obj_type = k[0] obj_id = k[7:] @@ -852,7 +852,7 @@ def ValidPath(localizer): 'invalid_path': _('This is not a valid path') } - def validate_python(self, value, state): + def _validate_python(self, value, state): if not os.path.isdir(value): msg = M(self, 'invalid_path', state) raise formencode.Invalid( @@ -870,10 +870,10 @@ def UniqSystemEmail(localizer, old_data= 'email_taken': _('This e-mail address is already taken') } - def _to_python(self, value, state): + def _convert_to_python(self, value, state): return value.lower() - def validate_python(self, value, state): + def _validate_python(self, value, state): if (old_data.get('email') or '').lower() != value: user = User.get_by_email(value, case_insensitive=True) if user: @@ -892,10 +892,10 @@ def ValidSystemEmail(localizer): 'non_existing_email': _('e-mail "%(email)s" does not exist.') } - def _to_python(self, value, state): + def _convert_to_python(self, value, state): return value.lower() - def validate_python(self, value, state): + def _validate_python(self, value, state): user = User.get_by_email(value, case_insensitive=True) if user is None: msg = M(self, 'non_existing_email', state, email=value) @@ -914,7 +914,7 @@ def NotReviewedRevisions(localizer, repo 'or have set status'), } - def validate_python(self, value, state): + def _validate_python(self, value, state): # check revisions if they are not reviewed, or a part of another # pull request statuses = ChangesetStatus.query()\ @@ -949,16 +949,16 @@ def ValidIp(localizer): 'of 0-32 (not %(bits)r)'), } - # we ovveride the default to_python() call + # we override the default to_python() call def to_python(self, value, state): v = super(_validator, self).to_python(value, state) - v = safe_unicode(v.strip()) + v = safe_str(v.strip()) net = ipaddress.ip_network(address=v, strict=False) return str(net) - def validate_python(self, value, state): + def _validate_python(self, value, state): try: - addr = safe_unicode(value.strip()) + addr = safe_str(value.strip()) # this raises an ValueError if address is not IpV4 or IpV6 ipaddress.ip_network(addr, strict=False) except ValueError: @@ -977,7 +977,7 @@ def FieldKey(localizer): 'underscore, dash or numbers'), } - def validate_python(self, value, state): + def _validate_python(self, value, state): if not re.match('[a-zA-Z0-9_-]+$', value): raise formencode.Invalid(self.message('badFormat', state), value, state) @@ -1001,9 +1001,9 @@ def ValidAuthPlugins(localizer): 'No plugin available with ID "%(plugin_id)s"'), } - def _to_python(self, value, state): + def _convert_to_python(self, value, state): # filter empty values - return filter(lambda s: s not in [None, ''], value) + return [s for s in value if s not in [None, '']] def _validate_legacy_plugin_id(self, plugin_id, value, state): """ @@ -1039,7 +1039,7 @@ def ValidAuthPlugins(localizer): return plugin - def validate_python(self, value, state): + def _validate_python(self, value, state): unique_names = {} for plugin_id in value: @@ -1068,11 +1068,11 @@ def ValidPattern(localizer): 'bad_format': _('Url must start with http or /'), } - def _to_python(self, value, state): + def _convert_to_python(self, value, state): patterns = [] prefix = 'new_pattern' - for name, v in value.items(): + for name, v in list(value.items()): pattern_name = '_'.join((prefix, 'pattern')) if name.startswith(pattern_name): new_item_id = name[len(pattern_name)+1:] @@ -1086,7 +1086,7 @@ def ValidPattern(localizer): 'issuetracker_pref': value.get(_field('prefix')), 'issuetracker_desc': value.get(_field('description')) } - new_uid = md5(values['issuetracker_pat']) + new_uid = md5_safe(values['issuetracker_pat']) has_required_fields = ( values['issuetracker_pat']