How to decode dataTables Editor form in python flask? -


i have flask application receiving request datatables editor. upon receipt @ server, request.form looks (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 thinking use similar ugly code decode it. there better way?

from collections import defaultdict  # request.form comes in multidict [('data[id][field]',value), ...] # need exec string turn python data structure data = defaultdict(lambda: {})   # default empty dict  # need define text each field 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 formkey in request.form.keys():     exec '{} = {}'.format(d,repr(request.form[formkey])) 

i decided on way more secure using exec:

from collections import defaultdict  def get_request_data(form):     '''     return dict list data request.form      :param form: multidict `request.form`     :rtype: {id1: {field1:val1, ...}, ...} [fieldn , valn strings]     '''      # request.form comes in multidict [('data[id][field]',value), ...]      # fill in id field automatically     data = defaultdict(lambda: {})      # fill in data[id][field] = value     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 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -