Commit 5935e804 authored by Nicolas Delaby's avatar Nicolas Delaby

Add filename property (Tales) on FormPrintout

in order to customise filename of downloadable file.



git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@32418 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 70b00e4d
......@@ -40,6 +40,7 @@ from Products.ERP5Type.Globals import InitializeClass, DTMLFile, Persistent
from AccessControl import ClassSecurityInfo
from AccessControl.Role import RoleManager
from OFS.SimpleItem import Item
from OFS.PropertyManager import PropertyManager
from urllib import quote, quote_plus
from copy import deepcopy
from lxml import etree
......@@ -76,7 +77,8 @@ NSMAP = {
# Constructors
manage_addFormPrintout = DTMLFile("dtml/FormPrintout_add", globals())
def addFormPrintout(self, id, title="", form_name='', template='', REQUEST=None):
def addFormPrintout(self, id, title="", form_name='', template='',
REQUEST=None, filename='object/title_or_id'):
"""Add form printout to folder.
Keyword arguments:
......@@ -86,7 +88,7 @@ def addFormPrintout(self, id, title="", form_name='', template='', REQUEST=None)
template -- the name of a template which describes printout layout
"""
# add actual object
id = self._setObject(id, FormPrintout(id, title, form_name, template))
id = self._setObject(id, FormPrintout(id, title, form_name, template, filename))
# respond to the add_and_edit button if necessary
add_and_edit(self, id, REQUEST)
return ''
......@@ -108,7 +110,7 @@ def add_and_edit(self, id, REQUEST):
u = "%s/%s" % (u, quote(id))
REQUEST.RESPONSE.redirect(u+'/manage_main')
class FormPrintout(Implicit, Persistent, RoleManager, Item):
class FormPrintout(Implicit, Persistent, RoleManager, Item, PropertyManager):
"""Form Printout
FormPrintout is one of a reporting system in ERP5.
......@@ -137,6 +139,15 @@ class FormPrintout(Implicit, Persistent, RoleManager, Item):
property_sheets = ( PropertySheet.Base
, PropertySheet.SimpleItem)
_properties = ( {'id': 'template',
'type': 'string',
'mode': 'w'},
{'id': 'form_name',
'type': 'string',
'mode': 'w'},
{'id': 'filename',
'type': 'tales',
'mode': 'w',},)
# Constructors
constructors = (manage_addFormPrintout, addFormPrintout)
......@@ -157,8 +168,10 @@ class FormPrintout(Implicit, Persistent, RoleManager, Item):
# default attributes
template = None
form_name = None
filename = 'object/title_or_id'
def __init__(self, id, title='', form_name='', template=''):
def __init__(self, id, title='', form_name='', template='',
filename='object/title_or_id'):
"""Initialize id, title, form_name, template.
Keyword arguments:
......@@ -166,11 +179,14 @@ class FormPrintout(Implicit, Persistent, RoleManager, Item):
title -- the title of a form printout
form_name -- the name of a form which as a document content
template -- the name of a template which as a document layout
filename -- Tales expression which return the filename of
downloadable file.
"""
self.id = id
self.title = title
self.form_name = form_name
self.template = template
self.filename = filename
security.declareProtected('View', 'index_html')
def index_html(self, REQUEST, RESPONSE=None, format=None, batch_mode=False):
......@@ -213,13 +229,14 @@ class FormPrintout(Implicit, Persistent, RoleManager, Item):
__call__ = index_html
security.declareProtected('Manage properties', 'doSettings')
def doSettings(self, REQUEST, title='', form_name='', template=''):
"""Change title, form_name, template."""
def doSettings(self, REQUEST, title='', form_name='', template='', filename='object/title_or_id'):
"""Change title, form_name, template, filename."""
if SUPPORTS_WEBDAV_LOCKS and self.wl_isLocked():
raise ResourceLockedError, "File is locked via WebDAV"
self.form_name = form_name
self.template = template
self.title = title
self.filename = filename
message = "Saved changes."
if getattr(self, '_v_warnings', None):
message = ("<strong>Warning:</strong> <i>%s</i>"
......@@ -248,11 +265,13 @@ class FormPrintout(Implicit, Persistent, RoleManager, Item):
"""
if REQUEST is not None and not format:
format = REQUEST.get('format', None)
filename = self.getProperty('filename')
if not format:
if REQUEST is not None and not batch_mode:
REQUEST.RESPONSE.setHeader('Content-Type','%s' % content_type)
REQUEST.RESPONSE.setHeader('Content-disposition',
'inline;filename="%s%s"' % (self.title_or_id(), guess_extension(content_type)))
'inline;filename="%s%s"' % \
(filename, guess_extension(content_type) or ''))
return printout
from Products.ERP5Type.Document import newTempOOoDocument
tmp_ooo = newTempOOoDocument(self, self.title_or_id())
......@@ -265,7 +284,7 @@ class FormPrintout(Implicit, Persistent, RoleManager, Item):
if REQUEST is not None and not batch_mode:
REQUEST.RESPONSE.setHeader('Content-type', mime)
REQUEST.RESPONSE.setHeader('Content-disposition',
'attachment;filename="%s.%s"' % (self.title_or_id(), format))
'attachment;filename="%s.%s"' % (filename, format))
return data
InitializeClass(FormPrintout)
......
......@@ -222,6 +222,15 @@ class TestFormPrintoutAsODT(TestFormPrintoutMixin):
self.assertTrue(content_xml.find("Français test2") > 0)
self._validate(odf_document)
# 7. Change Filename of downloadable file
reference = 'My Reference'
test1.setReference(reference)
foo_printout.filename = 'here/getReference'
odf_document = foo_printout(self.portal.REQUEST)
self.assertEqual(request.RESPONSE.getHeader('content-disposition'),
'inline;filename="%s.odt"' % reference)
test1.setReference(None)
def test_01_Paragraph_07_LinesField(self):
"""test LinesField into multi line"""
foo_printout = self.portal.foo_module.test1.Foo_viewAsPrintout
......
......@@ -20,6 +20,11 @@
<td><input name="template" value="default_template" type="text" size="20"
tal:attributes="value request/template | here/template | nothing"/></td>
</tr>
<tr>
<td class="form-label">Filename</td>
<td><input name="filename" value="default_filename" type="text" size="20"
tal:attributes="value request/filename | here/filename | nothing"/></td>
</tr>
<tr>
<td align="left" valign="top" colspan="2">
......
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