b9cbc1b0f1ee80f3d7f85f35d733cb3d84bf5b09
2 This hotfix patch ZTUtils.Zope.make_query and make_hidden_input
3 to make a recursive marshal from a REQUEST.form data.
9 from DateTime
import DateTime
11 def make_query(*args
, **kwargs
):
12 '''Construct a URL query string, with marshalling markup.
14 If there are positional arguments, they must be dictionaries.
15 They are combined with the dictionary of keyword arguments to form
16 a dictionary of query names and values.
18 Query names (the keys) must be strings. Values may be strings,
19 integers, floats, or DateTimes, and they may also be lists or
20 namespaces containing these types. Names and string values
21 should not be URL-quoted. All arguments are marshalled with
31 for k
, v
in d
.items() :
32 for field
in recurse_marshal(k
, v
) :
33 qlist
.append( '%s%s=%s' % (uq(field
['k']), field
['m'], uq(str(field
['v']))) )
35 return '&'.join(qlist
)
37 def make_hidden_input(*args
, **kwargs
):
38 '''Construct a set of hidden input elements, with marshalling markup.
40 If there are positional arguments, they must be dictionaries.
41 They are combined with the dictionary of keyword arguments to form
42 a dictionary of query names and values.
44 Query names (the keys) must be strings. Values may be strings,
45 integers, floats, or DateTimes, and they may also be lists or
46 namespaces containing these types. All arguments are marshalled with
57 for k
, v
in d
.items() :
58 for field
in recurse_marshal(k
, v
) :
59 qlist
.append( ('<input type="hidden" name="%s%s" value="%s"/>'
60 % (hq(field
['k']), field
['m'], hq(str(field
['v'])))) )
62 return '\n'.join(qlist
)
65 def _simple_marshal(v
) : # ! It's not a simple_marshal patch, the returned value is not the same.
67 if isinstance(v
, bool) :
69 elif isinstance(v
, int) :
71 elif isinstance(v
, float) :
73 elif isinstance(v
, long) :
75 elif isinstance(v
, DateTime
) :
77 elif isinstance(v
, unicode) :
80 elif hasattr(v
, 'items') :
82 elif isinstance(v
, list) :
83 if len(v
) > 0 and hasattr(v
[0], 'items') :
93 def recurse_marshal(k
, v
, nested
='') :
96 m
, v
= _simple_marshal(v
)
101 if isinstance(sv
, list) : # only strings into sv
102 white_space_found
= False
104 if ssv
.find(' ') >=0 : white_space_found
= True
105 if white_space_found
:
112 if isinstance(sv
, unicode) :
113 sv
= sv
.encode('utf8')
114 insertM
= ':utf8:u' + insertM
[1:]
115 marshalList
.extend(recurse_marshal(k
, sv
, nested
=insertM
+ n
))
117 marshalList
.extend(recurse_marshal(k
, sv
, n
))
118 elif m
== ':record' :
119 if n
.find(':records') < 0 :
121 for sk
, sv
in v
.items() :
122 marshalList
.extend(recurse_marshal('%s.%s' % (k
, sk
), sv
, nested
=n
))
123 elif m
== ':records' :
126 for ssk
, ssv
in sv
.items() :
127 marshalList
.extend(recurse_marshal('%s.%s' % (k
, ssk
), ssv
, nested
=n
))
129 marshalList
.append({'k' : k
,
136 def initialize(context
) :
137 import ZTUtils
as ztu
138 ztu
.make_query
= ztu
.Zope
.make_query
= make_query
139 ztu
.make_hidden_input
= ztu
.Zope
.make_hidden_input
= make_hidden_input