2

dataTables Editor からリクエストを受け取るフラスコ アプリケーションがあります。サーバーで受信すると、request.form次のようになります(例)

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'')])

この非常に醜いコードに似たものを使用してデコードすることを考えています。より良い方法はありますか?

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]))
4

2 に答える 2

0

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
于 2016-02-14T14:25:23.210 に答える