0002-api-add-a-transient-formdata-to-evaluated-submitted-.patch
tests/api/test_formdef.py | ||
---|---|---|
922 | 922 |
'value': '[dict(id=i, text=\'label %s\' % i, foo=i) for i in range(10)]', |
923 | 923 |
}, |
924 | 924 |
), |
925 |
fields.ItemField( |
|
926 |
id='2', |
|
927 |
label='foobar2', |
|
928 |
varname='foobar2', |
|
929 |
data_source={ |
|
930 |
'type': 'json', |
|
931 |
'value': 'http://datasource.com/{{form_var_foobar_foo}}', |
|
932 |
}, |
|
933 |
), |
|
925 | 934 |
] |
926 | 935 |
formdef.store() |
927 | 936 |
data_class = formdef.data_class() |
... | ... | |
946 | 955 |
{ |
947 | 956 |
'data': { |
948 | 957 |
'0': '0', |
949 |
"1": '3', |
|
958 |
'1': '3', |
|
959 |
'2': '2', |
|
950 | 960 |
} |
951 | 961 |
}, |
952 | 962 |
) |
963 |
assert len(urlopen.mock_calls) == 2 |
|
964 |
assert urlopen.call_args_list[0][0] == ('http://datasource.com',) |
|
965 |
assert urlopen.call_args_list[1][0] == ('http://datasource.com/bar',) |
|
953 | 966 | |
954 | 967 |
formdata = data_class.get(resp.json['data']['id']) |
955 | 968 |
assert formdata.status == 'wf-new' |
... | ... | |
967 | 980 |
'text': 'label 3', |
968 | 981 |
'foo': 3, |
969 | 982 |
} |
983 |
assert formdata.data['2'] == '2' |
|
984 |
assert formdata.data['2_display'] == 'deux' |
|
985 |
assert formdata.data['2_structured'] == { |
|
986 |
'id': 2, |
|
987 |
'text': 'deux', |
|
988 |
'foo': 'bar2', |
|
989 |
} |
|
970 | 990 | |
971 | 991 |
data_class.wipe() |
wcs/api.py | ||
---|---|---|
74 | 74 |
if field.store_structured_value and structured in data: |
75 | 75 |
data['%s_structured' % field.id] = data.pop(structured) |
76 | 76 | |
77 |
# complete/adapt field values |
|
78 |
for field in formdef.get_all_fields(): |
|
79 |
structured = '%s_structured' % field.id |
|
80 |
display = '%s_display' % field.id |
|
81 |
if data.get(field.id) is None: |
|
82 |
continue |
|
83 |
if hasattr(field, 'from_json_value'): |
|
84 |
data[field.id] = field.from_json_value(data[field.id]) |
|
85 |
# only fill display/structured if both are absent |
|
86 |
if display not in data and structured not in data: |
|
87 |
if field.store_display_value: |
|
88 |
display_value = field.store_display_value(data, field.id) |
|
89 |
if display_value is not None: |
|
90 |
data[display] = display_value |
|
91 |
if field.store_structured_value: |
|
92 |
structured_value = field.store_structured_value(data, field.id) |
|
93 |
if structured_value is not None: |
|
94 |
data[structured] = structured_value |
|
77 |
# create a temporary formdata so datasources using previous fields in |
|
78 |
# parameters can find their values. |
|
79 |
transient_formdata = formdef.data_class()() |
|
80 |
transient_formdata.data = data |
|
81 | ||
82 |
with get_publisher().substitutions.temporary_feed(transient_formdata, force_mode='lazy'): |
|
83 |
# complete/adapt field values |
|
84 |
for field in formdef.get_all_fields(): |
|
85 |
structured = '%s_structured' % field.id |
|
86 |
display = '%s_display' % field.id |
|
87 |
if data.get(field.id) is None: |
|
88 |
continue |
|
89 |
if hasattr(field, 'from_json_value'): |
|
90 |
data[field.id] = field.from_json_value(data[field.id]) |
|
91 |
# only fill display/structured if both are absent |
|
92 |
if display not in data and structured not in data: |
|
93 |
if field.store_display_value: |
|
94 |
display_value = field.store_display_value(data, field.id) |
|
95 |
if display_value is not None: |
|
96 |
data[display] = display_value |
|
97 |
if field.store_structured_value: |
|
98 |
structured_value = field.store_structured_value(data, field.id) |
|
99 |
if structured_value is not None: |
|
100 |
data[structured] = structured_value |
|
95 | 101 |
return data |
96 | 102 | |
97 | 103 | |
98 |
- |