Lou K February 2016

How to decode dataTables Editor form in python flask?

I have a flask application which is receiving a request from dataTables Editor. Upon receipt at the server, request.form looks like (e.g.)

ImmutableMultiDict([('data[59282][gender]', u'M'), ('data[59282][hometown]', u''), 
('data[59282][disposition]', u''), ('data[59282][id]', u'59282'),
('data[59282][resultname]', u'Joe Doe'), ('data[59282][confirm]', 'true'), 
('data[59282][age]', u'27'), ('data[59282][place]', u'3'), ('action', u'remove'), 
('data[59282][runnerid]', u''), ('data[59282][time]', u'29:49'),
('data[59282][club]', u'')])

I am thinking to use something similar to this really ugly code to decode it. Is there a better way?

from collections import defaultdict

# request.form comes in multidict [('data[id][field]',value), ...]
# so we need to exec this string to turn into python data structure
data = defaultdict(lambda: {})   # default is empty dict

# need to define text for each field to be received in data[id][field]
age = 'age'
club = 'club'
confirm = 'confirm'
disposition = 'disposition'
gender = 'gender'
hometown = 'hometown'
id = 'id'
place = 'place'
resultname = 'resultname'
runnerid = 'runnerid'
time = 'time'

# fill in data[id][field] = value
for formkey in request.form.keys():
    exec '{} = {}'.format(d,repr(request.form[formkey]))

Answers


Lou K February 2016

I decided on a way that is more secure than using exec:

from collections import defaultdict

def get_request_data(form):
    '''
    return dict list with data from request.form

    :param form: MultiDict from `request.form`
    :rtype: {id1: {field1:val1, ...}, ...} [fieldn and valn are strings]
    '''

    # request.form comes in multidict [('data[id][field]',value), ...]

    # fill in id field automatically
    data = defaultdict(lambda: {})

    # fill in data[id][field] = value
    for formkey in form.keys():
        if formkey == 'action': continue
        datapart,idpart,fieldpart = formkey.split('[')
        if datapart != 'data': raise ParameterError, "invalid input in request: {}".format(formkey)

        idvalue = int(idpart[0:-1])
        fieldname = fieldpart[0:-1]

        data[idvalue][fieldname] = form[formkey]

    # return decoded result
    return data

Post Status

Asked in February 2016
Viewed 3,977 times
Voted 6
Answered 1 times

Search




Leave an answer