Borrowing code from SQLFORM.dictform I think I'm close to rendering nested 
dicts dumped from json:

The browser is simply writing a bunch of the 
<gluon.html.(SPAN|FIELDSET)objects. I did try to return xml(form) but 
received an error saying:
 Generator expression must be parenthesized if not sole argument

from contrib.simplejson import loads, dumps
from types import *
AUTOTYPES = {
        type(u''): ('unicode', None),   ##Added
        type(''): ('string', None),
        type(True): ('boolean', None),
        type(1): ('integer', IS_INT_IN_RANGE(-1e12, +1e12)),
        type(1.0): ('double', IS_FLOAT_IN_RANGE()),
        type([]): ('list:string', None),
        type({}): ('text', IS_JSON()), ##Added
        type(datetime.date.today()): ('date', IS_DATE()),
        type(datetime.datetime.today()): ('datetime', IS_DATETIME())
    }
def makefields(dictionary):
""" Code modified from SQLFORM.dictform to recursivley process nested dicts 
"""    
    fields = []
    for key, value in sorted(dictionary.items()):
        #dbg.set_trace()
        t, requires = AUTOTYPES.get(type(value), (None, None))
        if t and isinstance(value,dict): ## added the type checking for 
value to see if is a dict
            fields.append(SPAN('{}'.format(key))) ## Needed to identify the 
nested dict
            fields.extend(makefields(value))      ## basic recursion
        elif t:
            fields.append(FIELDSET('{}'.format(key),
                                   INPUT(_name='{}'.format(key),
                                         _type='{}'.format('text'), 
#perhaps the AUTOTYPES should set t?
                                         _value='{}'.format(value),
                                         value='{}'.format(value),
                                         requires=requires)))
        
    return fields

@auth.requires_login()
def edit_parameters():
"""Display a form to edit the parameters rendered from a json object """

    table=db[request.args[0]]
    record=table(request.args[1])
    dictionary = record.parameters ## a json field type

    form = FORM('Edit Parameters', INPUT(_type='submit'), _action='', 
_method='post')
    fields=makefields(dictionary)
    form.insert(1,fields)
    
    dbg.set_trace()
    if form.validate(keepvalues=True):
        record.parameters.update(form.vars)
        table.update(record)
    
    return form


-- 



Reply via email to