build a query string out of a dictionary (REQUEST.form)
Hi all, does somebody know if there is a method/function built in Zope converting a dictionary to a query string? I'd like to access it in a DTML method or Python Script. I just tried to program a small Python Script, but methods out of urlib can not be used :-( The purpose is to append the actual query string to a link. But I like to replace parts of the query! I'd like to build a link the following way (DTML snippet): <dtml-let qs="buildQueryString(REQUEST.form, replace={'foo':'bar'})"> <a href="dtml-absolute_url;>?<dtml-qs>">foobar</a> </dtml-let> So a form value of e.g. 'foo=xxx' would be replaced by 'foo=bar'. I saw discussions about this topic in the list, but no answers with usable solutions. Did I miss it? I don't like to make a Product to support this! Ok, if I need I'll would have to :-( Greg _____________________________________ Grégoire Weber mailto:gregoire.weber@switzerland.org
I'd like to access it in a DTML method or Python Script. I just tried to program a small Python Script, but methods out of urlib can not be used :-(
Use an External Method then.
<dtml-let qs="buildQueryString(REQUEST.form, replace={'foo':'bar'})"> <a href="dtml-absolute_url;>?<dtml-qs>">foobar</a> </dtml-let>
A simple python method that loops through the form doesnt need urllib. eg: for k, v in context.REQUEST.form.items(): if k == 'foo': # do something else: somestr = '%s&%s=%s' % (somestr, k, v) Cheers. -- Andy McKay.
The purpose is to append the actual query string to a link. But I like to replace parts of the query! I'd like to build a link the following way (DTML snippet):
<dtml-let qs="buildQueryString(REQUEST.form, replace={'foo':'bar'})"> <a href="dtml-absolute_url;>?<dtml-qs>">foobar</a> </dtml-let>
So a form value of e.g. 'foo=xxx' would be replaced by 'foo=bar'.
I saw discussions about this topic in the list, but no answers with usable solutions. Did I miss it?
I don't like to make a Product to support this! Ok, if I need I'll would have to :-(
Greg _____________________________________ Grégoire Weber mailto:gregoire.weber@switzerland.org
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
Andy McKay writes:
for k, v in context.REQUEST.form.items(): if k == 'foo': # do something else: somestr = '%s&%s=%s' % (somestr, k, v) Do not forget quoting:
Query string fragments must be "urllib.quote_plus"ed. from urllib import quote_plus ..... somestr = "%s&%s=%s" % (somestr, quote_plus(k), quote_plus(v)) Dieter
Hi Andy, Hallo Dieter, Thanks a lot for your prompt answers! It gave me the good feeling about having support from competent people! As I supposed there did not exist a clean solution (I don't like extrenal python methods -- Sorry Andy!) for that problem. It's even not in builtin in Zope :-( So I programmed my first hotfix product which adds the method 'query_string' to the HTTPRequest class. Have a look at: http://www.zope.org/Members/gregweb/HTTPRequestHotfix What do you think? Should I file this to the tracker? Greg ------------------------------------------------------------------------ HTTPRequestHotix Product This so called 'Hotfix' adds the new method 'query_string' to the HTTPRequest class. The method is then accessible through the 'REQUEST' object. It builds a url quoted (quote_plus) query string from a dictionary (usually from the form dictionary which is based on QUERY_STRING). Usage query_string([[replace]=replace_dict], [[original]=original_dict]]) Updates 'original_dict' by the values in 'replace_dict' and returns the result as url quoted query string. 'original_dict' defaults to 'REQUEST.form' if no parameter is passed to it. Keys in 'replace_dict' with a value of 'None' (or _.None in DTML) will be deleted from 'original_dicts' before being quoted. The original 'REQUEST.form' will remain unchanged. Examples (assuming 'REQUEST.form' is {'foobar':'no translation', 'foo':'bar'}) 1. query_string(replace={'foobar':'furchtbar in german'}) returns 'foobar=furchtbar+in+german&foo=bar' 2. query_string(replace={'gaga':'&%+'}, original={'bar', 'foo'}) returns 'gaga=26%25%2b&bar=foo' 3. query_string(replace={'bar':'foo', 'foo', _.None}) returns 'foobar=no+translation&bar=foo' 4. The same in DTML: <dtml-var "REQUEST.query_string(replace={'bar':'foo', 'foo', _.None})"> returns 'foobar=no+translation&bar=foo' Have fun! Grégoire Weber, 2001-07-12 At 08:10 11.07.01 -0700, Andy McKay wrote:
A simple python method that loops through the form doesnt need urllib.
eg:
for k, v in context.REQUEST.form.items(): if k == 'foo': # do something else: somestr = '%s&%s=%s' % (somestr, k, v)
At 20:46 11.07.01 +0200, Dieter Maurer wrote:
Andy McKay writes:
for k, v in context.REQUEST.form.items(): if k == 'foo': # do something else: somestr = '%s&%s=%s' % (somestr, k, v) Do not forget quoting:
Query string fragments must be "urllib.quote_plus"ed.
_____________________________________ Grégoire Weber mailto:gregoire.weber@switzerland.org
Gr=?iso-8859-1?Q?=E9goire?= Weber writes:
Thanks a lot for your prompt answers! It gave me the good feeling about having support from competent people!
As I supposed there did not exist a clean solution (I don't like extrenal python methods -- Sorry Andy!) for that problem. It's even not in builtin in Zope :-( ??? What does that mean "not builtin in Zope"? I did not see a Zope version without the External Method product.
So I programmed my first hotfix product which adds the method 'query_string' to the HTTPRequest class. While you do not like External Methods, I do not like "Hotfix" products. But that is personal taste...
Have a look at: http://www.zope.org/Members/gregweb/HTTPRequestHotfix
What do you think? Should I file this to the tracker? Yes, try it.
Dieter
Hallo Dieter,
As I supposed there did not exist a clean solution (I don't like extrenal python methods -- Sorry Andy!) for that problem. It's even not in builtin in Zope :-( ??? What does that mean "not builtin in Zope"? I did not see a Zope version without the External Method product. Oh, sorry this is a misunderstanding caused by my english! I meant the possibility to get a manipulated query string to use them in <a href=""> tags out of the box. So I meant the method (or something similar) I programed in the hotfix product.
So I programmed my first hotfix product which adds the method 'query_string' to the HTTPRequest class. While you do not like External Methods, I do not like "Hotfix" products. But that is personal taste... Ok, a hotfix manipulates the innards of a stable system which external methods do not...
Gruss, Gregoire _____________________________________ Grégoire Weber mailto:gregoire.weber@switzerland.org
participants (3)
-
Andy McKay -
Dieter Maurer -
Grégoire Weber