workflows: change attachment view to use the factored file code path (#8031)
This commit is contained in:
parent
9a4dba6f0f
commit
20d98208f0
|
@ -1033,5 +1033,7 @@ def test_formdata_attachment_download(pub):
|
|||
|
||||
resp = resp.follow() # back to form page
|
||||
resp = resp.click('test.txt')
|
||||
assert resp.location.endswith('/test.txt')
|
||||
resp = resp.follow()
|
||||
assert resp.content_type == 'text/plain'
|
||||
assert resp.body == 'foobar'
|
||||
|
|
|
@ -15,13 +15,26 @@
|
|||
# along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
from quixote import get_response
|
||||
from quixote.util import FileStream
|
||||
from quixote import get_response, redirect
|
||||
|
||||
from wcs.workflows import *
|
||||
from qommon.errors import *
|
||||
|
||||
from wcs.forms.common import FormStatusPage
|
||||
from wcs.forms.common import FormStatusPage, FileDirectory
|
||||
|
||||
def lookup_wf_attachment(self, filename):
|
||||
# supports for URLs such as /$formdata/$id/files/attachment/test.txt
|
||||
if self.reference.split('-')[0] != 'attachment':
|
||||
return
|
||||
|
||||
for evo in self.formdata.evolution:
|
||||
if evo.parts:
|
||||
for p in evo.parts:
|
||||
if not isinstance(p, AttachmentEvolutionPart):
|
||||
continue
|
||||
if p.base_filename == filename:
|
||||
return p
|
||||
|
||||
|
||||
def form_attachment(self):
|
||||
self.check_receiver()
|
||||
|
@ -31,43 +44,15 @@ def form_attachment(self):
|
|||
except (KeyError, ValueError):
|
||||
raise TraversalError()
|
||||
|
||||
found = False
|
||||
for evo in self.filled.evolution:
|
||||
if evo.parts:
|
||||
for p in evo.parts:
|
||||
if not isinstance(p, AttachmentEvolutionPart):
|
||||
continue
|
||||
if os.path.basename(p.filename) == fn:
|
||||
found = True
|
||||
break
|
||||
if found:
|
||||
break
|
||||
return redirect(self.filled.get_url() + 'files/attachment/' + p.base_filename)
|
||||
|
||||
if not found:
|
||||
raise TraversalError()
|
||||
|
||||
try:
|
||||
fd = open(p.filename, 'rb')
|
||||
except:
|
||||
raise TraversalError()
|
||||
size = os.path.getsize(p.filename)
|
||||
|
||||
response = get_response()
|
||||
if p.content_type:
|
||||
response.set_content_type(p.content_type)
|
||||
else:
|
||||
response.set_content_type('application/octet-stream')
|
||||
if p.charset:
|
||||
response.set_charset(p.charset)
|
||||
if p.base_filename:
|
||||
if p.content_type and p.content_type.startswith('image/'):
|
||||
response.set_header(
|
||||
'content-disposition', 'inline; filename="%s"' % p.base_filename)
|
||||
else:
|
||||
response.set_header(
|
||||
'content-disposition', 'attachment; filename="%s"' % p.base_filename)
|
||||
|
||||
return FileStream(fd, size)
|
||||
raise TraversalError()
|
||||
|
||||
|
||||
class AddAttachmentWorkflowStatusItem(WorkflowStatusItem):
|
||||
|
@ -87,6 +72,9 @@ class AddAttachmentWorkflowStatusItem(WorkflowStatusItem):
|
|||
def init(cls):
|
||||
FormStatusPage._q_extra_exports.append('attachment')
|
||||
FormStatusPage.attachment = form_attachment
|
||||
if not 'lookup_wf_attachment' in FileDirectory._lookup_methods:
|
||||
FileDirectory._lookup_methods.append('lookup_wf_attachment')
|
||||
FileDirectory.lookup_wf_attachment = lookup_wf_attachment
|
||||
init = classmethod(init)
|
||||
|
||||
def render_as_line(self):
|
||||
|
|
|
@ -75,6 +75,9 @@ class AttachmentEvolutionPart: #pylint: disable=C1001
|
|||
upload.charset)
|
||||
from_upload = classmethod(from_upload)
|
||||
|
||||
def get_file_pointer(self):
|
||||
return open(self.filename)
|
||||
|
||||
def __getstate__(self):
|
||||
odict = self.__dict__.copy()
|
||||
if not odict.has_key('fp'):
|
||||
|
|
Loading…
Reference in New Issue