Package coprs :: Package views :: Package apiv3_ns :: Module json2form
[hide private]
[frames] | no frames]

Source Code for Module coprs.views.apiv3_ns.json2form

 1  import flask 
 2  from werkzeug.datastructures import MultiDict 
 3   
 4   
5 -def get_form_compatible_data(preserve=None):
6 input = without_empty_fields(get_input_dict()) 7 output = dict(input).copy() 8 9 for k, v in input.items(): 10 # Preserve the original value and return it unchanged 11 if k in (preserve or []): 12 continue 13 14 # Transform lists to strings separated with spaces 15 if type(v) == list: 16 v = " ".join(map(str, v)) 17 18 output[k] = v 19 20 # Our WTForms expect chroots to be this way 21 for chroot in input.get("chroots") or []: 22 output[chroot] = True 23 24 output.update(flask.request.files or {}) 25 return MultiDict(output)
26 27
28 -def get_input_dict():
29 return flask.request.json or flask.request.form
30 31
32 -def get_input():
33 return MultiDict(get_input_dict())
34 35
36 -def without_empty_fields(input):
37 output = input.copy() 38 for k, v in input.items(): 39 # Field with None value is like if it wasn't send with forms 40 if v is None: 41 del output[k] 42 return output
43