From 0ae033039b5f18878a3735b7afe23574cb3becf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Fri, 20 May 2016 09:40:11 +0200 Subject: [PATCH 1/2] misc: check for serializable file using hasattr() (#11000) PicklableUpload may exist in different namespaces, and then there's also an independant UploadedFile class; just check if will duck like a file. --- tests/test_api.py | 9 +++++++-- wcs/qommon/misc.py | 3 +-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/test_api.py b/tests/test_api.py index 3cdacd6..ca808f2 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -872,9 +872,13 @@ def test_formdata_with_workflow_data(pub, local_user): formdata.status = 'wf-new' formdata.evolution[-1].status = 'wf-new' - upload = PicklableUpload('test.txt', 'text/plain', 'ascii') + from qommon.form import PicklableUpload as PicklableUpload2 + from wcs.qommon.form import PicklableUpload as PicklableUpload3 + upload = PicklableUpload2('test.txt', 'text/plain', 'ascii') upload.receive(['test']) - formdata.workflow_data = {'blah': upload, 'xxx': 23} + upload2 = PicklableUpload3('test.txt', 'text/plain', 'ascii') + upload2.receive(['test']) + formdata.workflow_data = {'blah': upload, 'blah2': upload2, 'xxx': 23} formdata.store() resp = get_app(pub).get( @@ -883,6 +887,7 @@ def test_formdata_with_workflow_data(pub, local_user): 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' + assert base64.decodestring(resp.json['workflow']['data']['blah2']['content']) == 'test' def test_user_by_nameid(pub, local_user): resp = get_app(pub).get(sign_uri('/api/users/xyz/', user=local_user), diff --git a/wcs/qommon/misc.py b/wcs/qommon/misc.py index 0ff18d3..7234d9e 100644 --- a/wcs/qommon/misc.py +++ b/wcs/qommon/misc.py @@ -428,8 +428,7 @@ class JSONEncoder(json.JSONEncoder): if isinstance(obj, time.struct_time): return datetime.datetime.utcfromtimestamp(time.mktime(obj)).isoformat() + 'Z' - from .form import PicklableUpload - if isinstance(obj, PicklableUpload): + if hasattr(obj, 'base_filename'): return { 'filename': obj.base_filename, 'content_type': obj.content_type or 'application/octet-stream', -- 2.8.1