My question is what would be the best way to supply POST vars in PHP
notation, that would be converted to a dictionary (in Pyramid):
var[foo]=123 -> becomes request.POST["var"]["foo"] with value 123
var[bar]=456 -> becomes request.POST["var"]["bar"] with value 456
I can write my own function that extracts these from request.POST, but I
was wondering if I've missed some documentation or an already existing
lib? Now, I know I can handle hierarchical post data with, say, a JSON
content type post, but I'm looking for a solution to support client
developed for PHP backend, without having to rewrite it.
Thanks!
--
.oO V Oo.
.oO V Oo.
--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To view this discussion on the web visit https://groups.google.com/d/msg/pylons-discuss/-/5pKb0dr1Pp0J.
While I'm at it, though, I wonder how others are solving the same
problem. In PHP there are no sequences and dicts, just one associative
array entity, so things like:
var['a']['b'] = c
are perfectly valid ad hoc, even if var was never used before, while
Python would complain about missing key 'a' in dict var.
So the actual problem is this. I have a form with a table. Columns are
entity fields, rows are individual entities, just like in a database.
For a PHP backend the form would be constructed as:
<input type="text" name="foo[1]" value="123" />
<input type="text" name="bar[1]" value="123" />
<input type="text" name="baz[1]" value="123" />
<input type="text" name="gah[1]" value="123" />
with that being single row, entities having columns foo, bar, baz and
gah, and the value in brackets is the row ID. One solution would be:
<input type="text" name="foo" value="123" />
<input type="text" name="bar" value="123" />
<input type="text" name="baz" value="123" />
<input type="text" name="gah" value="123" />
<input type="hidden" name="id" value="1" />
But I can't trust that the order given in the form is the same given
through POST and especially the same reconstructed via WebOb's multidict
entities (since Python's dicts are not ordered).
Another solution would be to serialize the form using, say, jQuery and
do a JSON content type POST, then simply do json.loads(request.body) on
the server side, but as I said, I don't want to modify the client if I
don't have to. The best I can do is regex replace foo[id] into foo-id
for easier parsing (field.split("-")).
Suggestions (I mean, other than using a lib or function that parses
foo[id], bar[id], ...)?
.oO V Oo.
I'd strongly recommend you look at colander:
http://docs.pylonsproject.org/projects/colander/dev/
...if you just want extraction of structured data from requests.
If you want full form generation, then move on to using deform.
cheers,
Chris
> But I can't trust that the order given in the form is the same given
> through POST
Yes, you can.
> and especially the same reconstructed via WebOb's multidict
> entities (since Python's dicts are not ordered).
WebOb's multidicts are ordered.
> Suggestions (I mean, other than using a lib or function that parses
> foo[id], bar[id], ...)?
If you have to re-write a server without changing a client form then
yes, your best bet *is* to write a function that mimics what PHP's
extraction does.
cheers,
Chris
--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk
This is actually not quite true. See http://plope.com/peppercorn (which
might be something you'd want to use).
- C
On 06/26/2011 08:18 PM, Chris McDonough wrote:
> This is actually not quite true. See http://plope.com/peppercorn (which
> might be something you'd want to use).
>
Yes, I wrongly accused MultiDict of being unordered, so I ran through
the source to see how it works. And indeed, peppercorn is something that
would fit my needs perfectly, so thanks for that!
V