Commit 012b782c authored by Jérome Perrin's avatar Jérome Perrin

Introduce a new property "filename" on OOoTemplate that can be used to specify

the filename in content-disposition header.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@30121 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 1dd3ec82
...@@ -166,6 +166,12 @@ class OOoTemplate(ZopePageTemplate): ...@@ -166,6 +166,12 @@ class OOoTemplate(ZopePageTemplate):
) )
) )
_properties= ZopePageTemplate._properties + (
{'id': 'filename',
'type': 'tales',
'mode': 'w',}, )
filename = 'object/title_or_id'
security.declareProtected('View management screens', 'formSettings') security.declareProtected('View management screens', 'formSettings')
formSettings = PageTemplateFile('www/formSettings', globals(), formSettings = PageTemplateFile('www/formSettings', globals(),
__name__='formSettings') __name__='formSettings')
...@@ -525,7 +531,7 @@ class OOoTemplate(ZopePageTemplate): ...@@ -525,7 +531,7 @@ class OOoTemplate(ZopePageTemplate):
request.RESPONSE.setHeader('Content-Type', request.RESPONSE.setHeader('Content-Type',
'%s; charset=utf-8' % self.content_type) '%s; charset=utf-8' % self.content_type)
request.RESPONSE.setHeader('Content-disposition', request.RESPONSE.setHeader('Content-disposition',
'inline;filename="%s%s"' % (self.title_or_id(), 'inline;filename="%s%s"' % (self._getFileName(),
guess_extension(self.content_type))) guess_extension(self.content_type)))
if DevelopmentMode: if DevelopmentMode:
...@@ -572,29 +578,36 @@ class OOoTemplate(ZopePageTemplate): ...@@ -572,29 +578,36 @@ class OOoTemplate(ZopePageTemplate):
if x[1].endswith('pdf')] if x[1].endswith('pdf')]
if len(tgts) > 1: if len(tgts) > 1:
REQUEST.RESPONSE.setHeader('Content-type', 'text/html') REQUEST.RESPONSE.setHeader('Content-type', 'text/html')
REQUEST.RESPONSE.setHeader('Content-disposition',
'inline;filename="%s.pdf"' % self.title_or_id())
raise ValueError, 'multiple pdf formats found - this shouldnt happen' raise ValueError, 'multiple pdf formats found - this shouldnt happen'
if len(tgts) == 0: if len(tgts) == 0:
REQUEST.RESPONSE.setHeader('Content-type', 'text/html') REQUEST.RESPONSE.setHeader('Content-type', 'text/html')
REQUEST.RESPONSE.setHeader('Content-disposition',
'inline;filename="%s.pdf"' % self.title_or_id())
raise ValueError, 'no pdf format found' raise ValueError, 'no pdf format found'
fmt = tgts[0] fmt = tgts[0]
mime, data = tmp_ooo.convert(fmt) mime, data = tmp_ooo.convert(fmt)
if REQUEST is not None: if REQUEST is not None:
REQUEST.RESPONSE.setHeader('Content-type', 'application/pdf') REQUEST.RESPONSE.setHeader('Content-type', 'application/pdf')
REQUEST.RESPONSE.setHeader('Content-disposition', REQUEST.RESPONSE.setHeader('Content-disposition',
'attachment;filename="%s.pdf"' % self.title_or_id()) 'attachment;filename="%s.pdf"' % self._getFileName())
return data return data
mime, data = tmp_ooo.convert(format) mime, data = tmp_ooo.convert(format)
if REQUEST is not None and not batch_mode: if REQUEST is not None and not batch_mode:
REQUEST.RESPONSE.setHeader('Content-type', mime) REQUEST.RESPONSE.setHeader('Content-type', mime)
REQUEST.RESPONSE.setHeader('Content-disposition', REQUEST.RESPONSE.setHeader('Content-disposition',
'attachment;filename="%s.%s"' % (self.title_or_id(),format)) 'attachment;filename="%s.%s"' % (self._getFileName(),format))
# FIXME the above lines should return zip format when html was requested # FIXME the above lines should return zip format when html was requested
return data return data
def _getFileName(self):
"""Returns the filename used for content-disposition header.
"""
# The "filename" property has a TALES type, but getProperty for TALES types
# only works if the context has an ERP5 Site in his acquisition context.
# If it's not the case, we will not evaluate the TALES, but simply use the
# template's title or id as filename.
if getattr(self, 'getPortalObject', None) is None:
return self.title_or_id()
return self.getProperty('filename')
InitializeClass(OOoTemplate) InitializeClass(OOoTemplate)
class FSOOoTemplate(FSPageTemplate, OOoTemplate): class FSOOoTemplate(FSPageTemplate, OOoTemplate):
......
...@@ -101,9 +101,10 @@ class TestDeferredStyle(ERP5TypeTestCase, ZopeTestCase.Functional): ...@@ -101,9 +101,10 @@ class TestDeferredStyle(ERP5TypeTestCase, ZopeTestCase.Functional):
for part in mail_message.walk(): for part in mail_message.walk():
content_type = part.get_content_type() content_type = part.get_content_type()
file_name = part.get_filename() file_name = part.get_filename()
if file_name == 'report_view%s' % self.attachment_file_extension: # "History" is the title of Base_viewHistory form
if file_name == 'History%s' % self.attachment_file_extension:
self.assertEquals(content_type, self.content_type) self.assertEquals(content_type, self.content_type)
self.assertEquals('attachment; filename="report_view%s"' % self.assertEquals('attachment; filename="History%s"' %
self.attachment_file_extension, self.attachment_file_extension,
part.get('Content-Disposition')) part.get('Content-Disposition'))
data = part.get_payload(decode=True) data = part.get_payload(decode=True)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment