From bd3162c977782208ccec9ab2576aa6817e0d9f9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Tue, 14 Jun 2016 13:33:33 +0200 Subject: [PATCH] workflows: make sure attachments are saved only once (#11345) --- tests/test_workflows.py | 38 ++++++++++++++++++++++++++++++++++++++ wcs/workflows.py | 13 ++++++++----- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/tests/test_workflows.py b/tests/test_workflows.py index ddb47d6..6c44be4 100644 --- a/tests/test_workflows.py +++ b/tests/test_workflows.py @@ -1,6 +1,7 @@ import datetime import pytest import shutil +import StringIO import time import urllib2 import urlparse @@ -518,6 +519,43 @@ def test_register_comment(pub): item.perform(formdata) assert formdata.evolution[-1].display_parts()[-1] == '
1 < 3
' +def test_register_comment_attachment(pub): + pub.substitutions.feed(MockSubstitutionVariables()) + + formdef = FormDef() + formdef.name = 'baz' + formdef.fields = [] + formdef.store() + + formdata = formdef.data_class()() + formdata.just_created() + formdata.store() + + item = RegisterCommenterWorkflowStatusItem() + item.perform(formdata) + assert formdata.evolution[-1].display_parts()[-1] == '' + + if os.path.exists(os.path.join(get_publisher().app_dir, 'attachments')): + shutil.rmtree(os.path.join(get_publisher().app_dir, 'attachments')) + + formdata.evolution[-1].parts = [AttachmentEvolutionPart('hello.txt', + fp=StringIO.StringIO('hello world'), varname='testfile')] + formdata.store() + assert len(os.listdir(os.path.join(get_publisher().app_dir, 'attachments'))) == 1 + + item.comment = '[attachments.testfile.url]' + + pub.substitutions.feed(formdata) + item.perform(formdata) + url1 = formdata.evolution[-1].parts[-1].content + + pub.substitutions.feed(formdata) + item.perform(formdata) + url2 = formdata.evolution[-1].parts[-1].content + + assert len(os.listdir(os.path.join(get_publisher().app_dir, 'attachments'))) == 1 + assert url1 == url2 + def test_email(pub): pub.substitutions.feed(MockSubstitutionVariables()) diff --git a/wcs/workflows.py b/wcs/workflows.py index c762d2d..5c0c1eb 100644 --- a/wcs/workflows.py +++ b/wcs/workflows.py @@ -199,11 +199,14 @@ class AttachmentEvolutionPart: #pylint: disable=C1001 if not os.path.exists(filename): return filename - odict['filename'] = get_new_filename() - self.fp.seek(0) - fd = file(odict['filename'], 'w') - fd.write(self.fp.read()) - fd.close() + if not 'filename' in odict: + odict['filename'] = get_new_filename() + self.filename = odict['filename'] + self.fp.seek(0) + fd = file(odict['filename'], 'w') + fd.write(self.fp.read()) + fd.close() + return odict def view(self): -- 2.8.1