eggification
[ZTUtils_hotfix.git] / Products / ZTUtils_hotfix / __init__.py
diff --git a/Products/ZTUtils_hotfix/__init__.py b/Products/ZTUtils_hotfix/__init__.py
new file mode 100644 (file)
index 0000000..b9cbc1b
--- /dev/null
@@ -0,0 +1,139 @@
+"""
+This hotfix patch ZTUtils.Zope.make_query and make_hidden_input
+to make a recursive marshal from a REQUEST.form data.
+
+pinbe 2004/09/23
+"""
+
+import urllib, cgi
+from DateTime import DateTime
+
+def make_query(*args, **kwargs):
+    '''Construct a URL query string, with marshalling markup.
+
+    If there are positional arguments, they must be dictionaries.
+    They are combined with the dictionary of keyword arguments to form
+    a dictionary of query names and values.
+
+    Query names (the keys) must be strings.  Values may be strings,
+    integers, floats, or DateTimes, and they may also be lists or
+    namespaces containing these types.  Names and string values
+    should not be URL-quoted.  All arguments are marshalled with
+    complex_marshal().
+    '''
+    d = {}
+    for arg in args:
+        d.update(arg)
+    d.update(kwargs)
+    
+    qlist = []
+    uq = urllib.quote
+    for k, v in d.items() :
+        for field in recurse_marshal(k, v) :
+            qlist.append( '%s%s=%s' % (uq(field['k']), field['m'], uq(str(field['v']))) )
+
+    return '&'.join(qlist)
+
+def make_hidden_input(*args, **kwargs):
+    '''Construct a set of hidden input elements, with marshalling markup.
+
+    If there are positional arguments, they must be dictionaries.
+    They are combined with the dictionary of keyword arguments to form
+    a dictionary of query names and values.
+
+    Query names (the keys) must be strings.  Values may be strings,
+    integers, floats, or DateTimes, and they may also be lists or
+    namespaces containing these types.  All arguments are marshalled with
+    complex_marshal().
+    '''
+
+    d = {}
+    for arg in args:
+        d.update(arg)
+    d.update(kwargs)
+
+    hq = cgi.escape
+    qlist = []
+    for k, v in d.items() :
+        for field in recurse_marshal(k, v) :
+            qlist.append( ('<input type="hidden" name="%s%s" value="%s"/>'
+                            % (hq(field['k']), field['m'], hq(str(field['v'])))) )
+
+    return '\n'.join(qlist)
+
+
+def _simple_marshal(v) : # ! It's not a simple_marshal patch, the returned value is not the same.
+    
+    if isinstance(v, bool) :
+        m = ':boolean'
+    elif isinstance(v, int) :
+        m = ':int'
+    elif isinstance(v, float) :
+        m = ':float'
+    elif isinstance(v, long) :
+        m = ':long'
+    elif isinstance(v, DateTime) :
+        m = ':date'
+    elif isinstance(v, unicode) :
+        m = ':utf8:ustring'
+        v = v.encode('utf8')
+    elif hasattr(v, 'items') :
+        m = ':record'
+    elif isinstance(v, list) :
+        if len(v) > 0 and hasattr(v[0], 'items') :
+            m = ':records'
+        else :
+            m = ':list'
+    else :
+        m = ''
+    
+    return m, v
+    
+    
+def recurse_marshal(k, v, nested='') :
+
+    marshalList = []
+    m, v = _simple_marshal(v)
+    n = nested
+    if m == ':list' :
+        n = ':list' + n
+        for sv in v :
+            if isinstance(sv, list) : # only strings into sv
+                white_space_found = False
+                for ssv in sv :
+                    if ssv.find(' ') >=0 : white_space_found = True
+                if white_space_found :
+                    sv = '\n'.join(sv)
+                    insertM = ':lines'
+                else :
+                    sv = ' '.join(sv)
+                    insertM = ':tokens'
+                
+                if isinstance(sv, unicode) :
+                    sv = sv.encode('utf8')
+                    insertM = ':utf8:u' + insertM[1:]
+                marshalList.extend(recurse_marshal(k, sv, nested=insertM + n))
+            else :
+                marshalList.extend(recurse_marshal(k, sv, n))
+    elif m == ':record' :
+        if n.find(':records') < 0 :
+            n = ':record' + n
+        for sk, sv in v.items() :
+            marshalList.extend(recurse_marshal('%s.%s' % (k, sk), sv, nested=n))
+    elif m == ':records' :
+        n = ':records' + n
+        for sv in v :
+            for ssk, ssv in sv.items() :
+                marshalList.extend(recurse_marshal('%s.%s' % (k, ssk), ssv, nested=n))
+    else :
+        marshalList.append({'k' : k,
+                            'm' : m + nested,
+                            'v' : v})
+    
+    return marshalList
+
+
+def initialize(context) :
+    import ZTUtils as ztu
+    ztu.make_query = ztu.Zope.make_query = make_query
+    ztu.make_hidden_input = ztu.Zope.make_hidden_input = make_hidden_input
\ No newline at end of file