Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Z
Zope
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
Zope
Commits
be1dc1ea
Commit
be1dc1ea
authored
May 27, 2010
by
Martin Aspeli
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Merge c112780 from 2.12 branch
parent
e09400ae
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
13 deletions
+66
-13
src/Products/Five/browser/decode.py
src/Products/Five/browser/decode.py
+30
-13
src/Products/Five/browser/tests/test_decode.py
src/Products/Five/browser/tests/test_decode.py
+36
-0
No files found.
src/Products/Five/browser/decode.py
View file @
be1dc1ea
...
...
@@ -32,23 +32,40 @@ def _decode(text, charsets):
pass
return
text
def
processInputValue
(
value
,
charsets
):
"""Recursively look for values (e.g. elements of lists, tuples or dicts)
and attempt to decode.
"""
if
isinstance
(
value
,
list
):
return
[
processInputValue
(
v
,
charsets
)
for
v
in
value
]
elif
isinstance
(
value
,
tuple
):
return
tuple
([
processInputValue
(
v
,
charsets
)
for
v
in
value
])
elif
isinstance
(
value
,
dict
):
for
k
,
v
in
value
.
items
():
value
[
k
]
=
processInputValue
(
v
,
charsets
)
return
value
elif
isinstance
(
value
,
str
):
return
_decode
(
value
,
charsets
)
else
:
return
value
def
processInputs
(
request
,
charsets
=
None
):
"""Process the values in request.form to decode strings to unicode, using
the passed-in list of charsets. If none are passed in, look up the user's
preferred charsets. The default is to use utf-8.
"""
if
charsets
is
None
:
envadapter
=
IUserPreferredCharsets
(
request
)
charsets
=
envadapter
.
getPreferredCharsets
()
or
[
'utf-8'
]
envadapter
=
IUserPreferredCharsets
(
request
,
None
)
if
envadapter
is
None
:
charsets
=
[
'utf-8'
]
else
:
charsets
=
envadapter
.
getPreferredCharsets
()
or
[
'utf-8'
]
for
name
,
value
in
request
.
form
.
items
():
if
not
(
isCGI_NAME
(
name
)
or
name
.
startswith
(
'HTTP_'
)):
if
isinstance
(
value
,
str
):
request
.
form
[
name
]
=
_decode
(
value
,
charsets
)
elif
isinstance
(
value
,
list
):
request
.
form
[
name
]
=
[
_decode
(
val
,
charsets
)
for
val
in
value
if
isinstance
(
val
,
str
)
]
elif
isinstance
(
value
,
tuple
):
request
.
form
[
name
]
=
tuple
([
_decode
(
val
,
charsets
)
for
val
in
value
if
isinstance
(
val
,
str
)
])
request
.
form
[
name
]
=
processInputValue
(
value
,
charsets
)
def
setPageEncoding
(
request
):
"""Set the encoding of the form page via the Content-Type header.
...
...
src/Products/Five/browser/tests/test_decode.py
View file @
be1dc1ea
...
...
@@ -46,6 +46,42 @@ def test_processInputs():
>>> processInputs(request, charsets)
>>> request.form['foo'] == (u'f
\
xf6
\
xf6
',)
True
Ints in lists are not lost::
>>> request.form['foo'] = [1, 2, 3]
>>> processInputs(request, charsets)
>>> request.form['foo'] == [1, 2, 3]
True
Ints in tuples are not lost::
>>> request.form['foo'] = (1, 2, 3,)
>>> processInputs(request, charsets)
>>> request.form['foo'] == (1, 2, 3)
True
Mixed lists work:
>>> request.form['foo'] = [u'f
\
xf6
\
xf6
'.encode('iso-8859-1'), 2, 3]
>>> processInputs(request, charsets)
>>> request.form['foo'] == [u'f
\
xf6
\
xf6
', 2, 3]
True
Mixed dicts work:
>>> request.form['foo'] = {'foo': u'f
\
xf6
\
xf6
'.encode('iso-8859-1'), 'bar': 2}
>>> processInputs(request, charsets)
>>> request.form['foo'] == {'foo': u'f
\
xf6
\
xf6
', 'bar': 2}
True
Deep recursion works:
>>> request.form['foo'] = [{'foo': u'f
\
xf6
\
xf6
'.encode('iso-8859-1'), 'bar': 2}, {'foo': u"one", 'bar': 3}]
>>> processInputs(request, charsets)
>>> request.form['foo'] == [{'foo': u'f
\
xf6
\
xf6
', 'bar': 2}, {'foo': u"one", 'bar': 3}]
True
"""
def
test_suite
():
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment