formdef: add pre-json serialization of struct_time workflow options (#36515)

(python3 json serializer would otherwise catch them and serialize them
as tuples)
This commit is contained in:
Frédéric Péters 2019-11-19 09:35:08 +01:00
parent 4f488c1d57
commit 4f3dc60311
2 changed files with 8 additions and 3 deletions

View File

@ -160,8 +160,8 @@ def test_schema_with_date_variable(pub):
DateField(label='Test', type='date', varname='foo'))
wf.store()
formdef.workflow_id = wf.id
formdef.workflow_options = {'foo': time.gmtime(time.mktime((2016, 4, 2, 0, 0, 0, 0, 0, 0)))}
assert json.loads(formdef.export_to_json())['options']['foo'].startswith('2016-04')
formdef.workflow_options = {'foo': datetime.datetime(2016, 4, 2).timetuple()}
assert json.loads(formdef.export_to_json())['options']['foo'].startswith('2016-04-02')
def test_substitution_variables_object(pub):
formdef = FormDef()

View File

@ -765,7 +765,12 @@ class FormDef(StorableObject):
root['geolocations'] = self.geolocations.copy()
if self.workflow_options:
root['options'] = self.workflow_options
root['options'] = self.workflow_options.copy()
for k, v in list(root['options'].items()):
# convert time.struct_time to strings as python3 would
# serialize it as tuple.
if isinstance(v, time.struct_time):
root['options'][k] = time.strftime('%Y-%m-%dT%H:%M:%S', v)
if self.required_authentication_contexts:
root['required_authentication_contexts'] = self.required_authentication_contexts[:]