api: encode files that may exist in workflow_data (#10769)
This commit is contained in:
parent
00d56c1424
commit
aa617eaf3d
|
@ -846,6 +846,43 @@ def test_formdata_edit(pub, local_user):
|
|||
assert formdef.data_class().select()[0].data['0'] == 'bar2@localhost'
|
||||
assert formdef.data_class().select()[0].status == 'wf-rejected'
|
||||
|
||||
def test_formdata_with_workflow_data(pub, local_user):
|
||||
Role.wipe()
|
||||
role = Role(name='test')
|
||||
role.id = '123'
|
||||
role.store()
|
||||
|
||||
local_user.roles = [role.id]
|
||||
local_user.store()
|
||||
|
||||
FormDef.wipe()
|
||||
formdef = FormDef()
|
||||
formdef.name = 'test'
|
||||
formdef.fields = []
|
||||
workflow = Workflow.get_default_workflow()
|
||||
workflow.id = '2'
|
||||
workflow.store()
|
||||
formdef.workflow_id = workflow.id
|
||||
formdef.workflow_roles = {'_receiver': role.id}
|
||||
formdef.store()
|
||||
|
||||
formdef.data_class().wipe()
|
||||
formdata = formdef.data_class()()
|
||||
formdata.just_created()
|
||||
formdata.status = 'wf-new'
|
||||
formdata.evolution[-1].status = 'wf-new'
|
||||
|
||||
upload = PicklableUpload('test.txt', 'text/plain', 'ascii')
|
||||
upload.receive(['test'])
|
||||
formdata.workflow_data = {'blah': upload, 'xxx': 23}
|
||||
formdata.store()
|
||||
|
||||
resp = get_app(pub).get(
|
||||
sign_uri('/api/forms/test/%s/' % formdata.id, user=local_user))
|
||||
assert resp.json['workflow']['data']['xxx'] == 23
|
||||
assert resp.json['workflow']['data']['blah']['filename'] == 'test.txt'
|
||||
assert resp.json['workflow']['data']['blah']['content_type'] == 'text/plain'
|
||||
assert base64.decodestring(resp.json['workflow']['data']['blah']['content']) == 'test'
|
||||
|
||||
def test_user_by_nameid(pub, local_user):
|
||||
resp = get_app(pub).get(sign_uri('/api/users/xyz/', user=local_user),
|
||||
|
|
|
@ -424,6 +424,15 @@ class JSONEncoder(json.JSONEncoder):
|
|||
def default(self, obj):
|
||||
if isinstance(obj, time.struct_time):
|
||||
return datetime.datetime.utcfromtimestamp(time.mktime(obj)).isoformat() + 'Z'
|
||||
|
||||
from .form import PicklableUpload
|
||||
if isinstance(obj, PicklableUpload):
|
||||
return {
|
||||
'filename': obj.base_filename,
|
||||
'content_type': obj.content_type or 'application/octet-stream',
|
||||
'content': base64.b64encode(obj.get_content()),
|
||||
}
|
||||
|
||||
# Let the base class default method raise the TypeError
|
||||
return json.JSONEncoder.default(self, obj)
|
||||
|
||||
|
|
Loading…
Reference in New Issue