Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #1905 (closed defect: fixed)

Opened 11 years ago

Last modified 9 years ago

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:1 Changed 11 years ago by rick446

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 unpack

The 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

comment:2 Changed 11 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 11 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:4 Changed 10 years ago by mramm

  • Milestone changed from 2.0 to 2.1

comment:5 Changed 10 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 9 years ago by percious

ftr, im -1 on this idea. If people want to decode their vars that way, they can using formencode directly.

comment:7 Changed 9 years ago by mramm

  • Milestone changed from 2.1 to 2.1b2

comment:8 Changed 9 years ago by rick446

  • Status changed from new to assigned
  • Owner changed from anonymous to rick446

comment:9 Changed 9 years ago by rick446

  • Status changed from assigned to closed
  • Resolution set to fixed

Fixed in d1bb21aa4f1b by creating a @variable_decode decorator

Note: See TracTickets for help on using tickets.