workflows: make sure attachments are saved only once (#11345)

This commit is contained in:
Frédéric Péters 2016-06-14 13:33:33 +02:00
parent d10eb2c59c
commit c294c059e3
2 changed files with 46 additions and 5 deletions

View File

@ -1,6 +1,7 @@
import datetime
import pytest
import shutil
import StringIO
import time
import urllib2
import urlparse
@ -517,6 +518,43 @@ def test_register_comment(pub):
item.perform(formdata)
assert formdata.evolution[-1].display_parts()[-1] == '<div>1 &lt; 3</div>'
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())

View File

@ -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):