##// END OF EJS Templates
validators/schemas: python3 fixes str vs unicode and few test breaking fixes
super-admin -
r5066:ccd88b7c default
parent child Browse files
Show More
@@ -18,7 +18,14 b''
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 import unicodedata
21 from rhodecode.lib.str_utils import convert_special_chars, safe_int, safe_bytes
22
23
24 def to_bytes_preparer(value):
25
26 if value:
27 value = safe_bytes(value)
28 return value
22 29
23 30
24 31 def strip_preparer(value):
@@ -49,7 +56,7 b' def non_ascii_strip_preparer(value):'
49 56 `ΕΌoΕ‚w` converts into `zolw`
50 57 """
51 58 if value:
52 value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
59 value = convert_special_chars(value)
53 60 return value
54 61
55 62
@@ -85,4 +92,8 b' def unique_list_from_str_preparer(value)'
85 92
86 93 if isinstance(value, str):
87 94 value = aslist(value, ',')
88 return unique_list_preparer(value) No newline at end of file
95 return unique_list_preparer(value)
96
97
98 def ensure_value_is_int(value):
99 return safe_int(value)
@@ -60,7 +60,7 b' class CommentSchema(colander.MappingSche'
60 60 colander.String(),
61 61 validator=colander.OneOf(ChangesetComment.COMMENT_TYPES),
62 62 missing=ChangesetComment.COMMENT_TYPE_NOTE)
63 is_draft = colander.SchemaNode(colander.Boolean(),missing=False)
63 is_draft = colander.SchemaNode(colander.Boolean(), missing=False)
64 64 comment_file = colander.SchemaNode(colander.String(), missing=None)
65 65 comment_line = colander.SchemaNode(colander.String(), missing=None)
66 66 status_change = colander.SchemaNode(
@@ -22,6 +22,7 b' import os'
22 22
23 23 import colander
24 24
25 from rhodecode.lib.str_utils import safe_str
25 26 from rhodecode.translation import _
26 27 from rhodecode.model.validation_schema import preparers
27 28
@@ -34,7 +35,7 b' def nodes_to_sequence(nodes, colander_no'
34 35
35 36 """
36 37 if not isinstance(nodes, dict):
37 msg = 'Nodes needs to be a dict, got {}'.format(type(nodes))
38 msg = f'Nodes needs to be a dict, got {type(nodes)}'
38 39 raise colander.Invalid(colander_node, msg)
39 40 out = []
40 41
@@ -51,6 +52,7 b' def nodes_to_sequence(nodes, colander_no'
51 52
52 53
53 54 def sequence_to_nodes(nodes, colander_node=None):
55
54 56 if not isinstance(nodes, list):
55 57 msg = 'Nodes needs to be a list, got {}'.format(type(nodes))
56 58 raise colander.Invalid(colander_node, msg)
@@ -86,33 +88,41 b' def unique_gist_validator(node, value):'
86 88 from rhodecode.model.db import Gist
87 89 existing = Gist.get_by_access_id(value)
88 90 if existing:
89 msg = _(u'Gist with name {} already exists').format(value)
91 msg = _('Gist with name {} already exists').format(value)
90 92 raise colander.Invalid(node, msg)
91 93
92 94
93 95 def filename_validator(node, value):
94 96 if value != os.path.basename(value):
95 msg = _(u'Filename {} cannot be inside a directory').format(value)
97 msg = _('Filename {} cannot be inside a directory').format(safe_str(value))
96 98 raise colander.Invalid(node, msg)
97 99
98 100
99 101 class NodeSchema(colander.MappingSchema):
100 102 # if we perform rename this will be org filename
101 103 filename_org = colander.SchemaNode(
102 colander.String(),
103 preparer=[preparers.strip_preparer,
104 preparers.non_ascii_strip_preparer],
104 colander.String(encoding='utf-8'),
105 preparer=[
106 preparers.strip_preparer,
107 preparers.non_ascii_strip_preparer,
108 preparers.to_bytes_preparer,
109 ],
105 110 validator=filename_validator,
106 111 missing=None)
107 112
108 113 filename = colander.SchemaNode(
109 colander.String(),
110 preparer=[preparers.strip_preparer,
111 preparers.non_ascii_strip_preparer],
114 colander.String(encoding='utf-8'),
115 preparer=[
116 preparers.strip_preparer,
117 preparers.non_ascii_strip_preparer,
118 preparers.to_bytes_preparer,
119 ],
112 120 validator=filename_validator)
113 121
114 122 content = colander.SchemaNode(
115 colander.String())
123 colander.String(encoding='utf-8'),
124 preparer=[preparers.to_bytes_preparer])
125
116 126 mimetype = colander.SchemaNode(
117 127 colander.String(),
118 128 missing=None)
@@ -164,7 +174,7 b' class GistSchema(colander.MappingSchema)'
164 174
165 175 description = colander.SchemaNode(
166 176 colander.String(),
167 missing=u'')
177 missing='')
168 178
169 179 lifetime = colander.SchemaNode(
170 180 colander.Integer(),
@@ -172,12 +182,13 b' class GistSchema(colander.MappingSchema)'
172 182
173 183 gist_acl_level = colander.SchemaNode(
174 184 colander.String(),
185 missing=Gist.ACL_LEVEL_PRIVATE,
175 186 validator=colander.OneOf([Gist.ACL_LEVEL_PUBLIC,
176 187 Gist.ACL_LEVEL_PRIVATE]))
177 188
178 189 gist_type = colander.SchemaNode(
179 190 colander.String(),
180 missing=Gist.GIST_PUBLIC,
191 missing=Gist.GIST_PRIVATE,
181 192 validator=colander.OneOf([Gist.GIST_PRIVATE, Gist.GIST_PUBLIC]))
182 193
183 194 nodes = Nodes()
@@ -238,9 +238,18 b' def deferred_repo_name_validator(node, k'
238 238
239 239 @colander.deferred
240 240 def deferred_repo_group_validator(node, kw):
241 options = kw.get(
242 'repo_repo_group_options')
243 return colander.OneOf([x for x in options])
241 options = kw.get('repo_repo_group_options')
242
243 def repo_group_validator(node, value):
244 choices = [x for x in options]
245 err = _('Group ID: `${val}` is not one of allowed ${choices}')
246
247 if preparers.ensure_value_is_int(value) not in choices:
248 choices = ', '.join(['%s' % x for x in sorted(choices)])
249 err = _(err, mapping={'val': value, 'choices': choices})
250 raise colander.Invalid(node, err)
251
252 return repo_group_validator
244 253
245 254
246 255 @colander.deferred
@@ -395,6 +404,7 b' class RepoSettingsSchema(RepoSchema):'
395 404 colander.Integer(),
396 405 validator=deferred_repo_group_validator,
397 406 widget=deferred_repo_group_widget,
407 preparers=[preparers.ensure_value_is_int],
398 408 missing='')
399 409
400 410 repo_clone_uri_change = colander.SchemaNode(
@@ -18,16 +18,15 b''
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 import os
22 21 import re
23 22 import logging
24 23
25
26 24 import ipaddress
27 25 import colander
28 26
29 27 from rhodecode.translation import _
30 from rhodecode.lib.utils2 import glob2re, safe_unicode
28 from rhodecode.lib.utils2 import glob2re
29 from rhodecode.lib.str_utils import safe_str
31 30 from rhodecode.lib.ext_json import json
32 31
33 32 log = logging.getLogger(__name__)
@@ -36,9 +35,9 b' log = logging.getLogger(__name__)'
36 35 def ip_addr_validator(node, value):
37 36 try:
38 37 # this raises an ValueError if address is not IpV4 or IpV6
39 ipaddress.ip_network(safe_unicode(value), strict=False)
38 ipaddress.ip_network(safe_str(value), strict=False)
40 39 except ValueError:
41 msg = _(u'Please enter a valid IPv4 or IpV6 address')
40 msg = _('Please enter a valid IPv4 or IpV6 address')
42 41 raise colander.Invalid(node, msg)
43 42
44 43
@@ -49,9 +48,9 b' class IpAddrValidator(object):'
49 48 def __call__(self, node, value):
50 49 try:
51 50 # this raises an ValueError if address is not IpV4 or IpV6
52 ipaddress.ip_network(safe_unicode(value), strict=self.strict)
51 ipaddress.ip_network(safe_str(value), strict=self.strict)
53 52 except ValueError:
54 msg = _(u'Please enter a valid IPv4 or IpV6 address')
53 msg = _('Please enter a valid IPv4 or IpV6 address')
55 54 raise colander.Invalid(node, msg)
56 55
57 56
@@ -59,7 +58,7 b' def glob_validator(node, value):'
59 58 try:
60 59 re.compile('^' + glob2re(value) + '$')
61 60 except Exception:
62 msg = _(u'Invalid glob pattern')
61 msg = _('Invalid glob pattern')
63 62 raise colander.Invalid(node, msg)
64 63
65 64
@@ -136,19 +135,21 b' class CloneUriValidator(object):'
136 135 url_validator(value, self.repo_type, config)
137 136 except InvalidCloneUrl as e:
138 137 log.warning(e)
139 raise colander.Invalid(node, e.message)
140 except Exception:
138 raise colander.Invalid(node, str(e))
139 except Exception as e:
141 140 log.exception('Url validation failed')
142 msg = _(u'invalid clone url or credentials for {repo_type} repository').format(
143 repo_type=self.repo_type)
141 reason = repr(e)
142 reason = reason.replace('<', '&lt;').replace('>', '&gt;')
143 msg = _('invalid clone url or credentials for {repo_type} repository. Reason: {reason}')\
144 .format(reason=reason, repo_type=self.repo_type)
144 145 raise colander.Invalid(node, msg)
145 146
146 147
147 148 def json_validator(node, value):
148 149 try:
149 150 json.loads(value)
150 except (Exception,) as e:
151 msg = _(u'Please enter a valid json object')
151 except (Exception,):
152 msg = _('Please enter a valid json object')
152 153 raise colander.Invalid(node, msg)
153 154
154 155
@@ -156,5 +157,5 b' def json_validator_with_exc(node, value)'
156 157 try:
157 158 json.loads(value)
158 159 except (Exception,) as e:
159 msg = _(u'Please enter a valid json object: `{}`'.format(e))
160 msg = _('Please enter a valid json object: `{}`'.format(e))
160 161 raise colander.Invalid(node, msg)
General Comments 0
You need to be logged in to leave comments. Login now