Ticket #1905 (closed defect: fixed)
use variable_decode to add automagical formparsing like tg1
| Reported by: | mramm | Owned by: | rick446 |
|---|---|---|---|
| Priority: | normal | Milestone: | 2.1b2 |
| Component: | TurboGears | Version: | trunk |
| Severity: | normal | Keywords: | |
| Cc: |
Description
Add formencode variable_decode stuff to get tg1 like automatic nested dictionary generation in tg2. We will also need a decorator that allows us to skip this behavior, since there are some forms that variable_decode doesn't like.
Change History
comment:2 Changed 3 years ago by mramm
I'm actually a little bit hesitant about this, as it's kind of magical -- and because of the failure cases outlined above.
comment:3 Changed 3 years ago by mramm
- Milestone changed from 2.0-preview-3 to 2.0
We can always add this later if people ask for it back, and I'm a bit hesitant to do it if variable_decode isn't cleaned up a bit first.
comment:5 Changed 3 years ago by nickmurdoch
First time I accidentally encountered this in TG1, it was a bit bewildering as I wasn't expecting my form names to be played around with; however once I knew about it, it was very useful for contructing forms that could be easily worked with in the controllers.
A decorator (or argument for expose()?) to optionally provide this functionality would be nice to have, although I suspect it'd be trivial to write one myself! Although it did take me a good few hours to work out what in TG1 was actually responsible for causing this behaviour before being able to even find this ticket -- it's quite a difficult concept to put simply into words.
comment:6 Changed 2 years ago by percious
ftr, im -1 on this idea. If people want to decode their vars that way, they can using formencode directly.
The following trivial dictionaries will crash variable_decode:
>>> from formencode.variabledecode import variable_encode, variable_decode >>> variable_decode({'a-a':0}) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.5/site-packages/FormEncode-0.7.1-py2.5.egg/formencode/variabledecode.py", line 49, in variable_decode new_keys.append(int(index)) ValueError: invalid literal for int() with base 10: 'a' >>> variable_decode({'a-0-0':0}) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.5/site-packages/FormEncode-0.7.1-py2.5.egg/formencode/variabledecode.py", line 46, in variable_decode key, index = key.split(list_char) ValueError: too many values to unpackThe second error is especially troubling because it means variable_decode is not the inverse of variable_encode:
>>> variable_decode(variable_encode({'a':[[0]]})) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.5/site-packages/FormEncode-0.7.1-py2.5.egg/formencode/variabledecode.py", line 46, in variable_decode key, index = key.split(list_char) ValueError: too many values to unpack