What about adding a download tab to Document?
Hi, I think it would be nice to have a download tab in the document objects. Today we have to cut'n paste the form contents to an editor and save it with the desired name. Probably the usual development way is to edit a doc in the file system and to upload it. When you want to change, just edit it and upload again. Not very good when you have to edit a document from multiple places, or to edit documents that where automatically generated. Here is a patch to do it: http://samba-choro.com.br/neves/python/zope/Document.py.patch It should be applied to Document.py file in /web/Zope-1.9.0-src/lib/python/OFS dir. There's a kind of a hack in it to make the default file name in the browser download dialog to become "document_id.ztml". To do this a method is created with this name. If someone know a better way to do it, like a magic http header, please tell me. As you can see the default extension is .ztml (I liked it!). The mime type is text/ztml. If you want to change to dtml it is just a char in the code. regards, -- Paulo Eduardo Neves PUC-Rio de Janeiro Pager: Central: 292-4499 cod. 213 99 64 ou use a URL: http://www.learn.fplf.org.br/neves/mensagempager.html
Paulo's excellent patch was reversed - here it is as a unified diff in the forward direction made in the zope root directory - apply with: patch -p 0 <Document.py.diff (Python people - do I need to delete the Document.pyc file, or does python notice the timestamps and recompile? I moved it just to be sure) Ross -- Ross J. Reedstrom, Ph.D., <reedstrm@rice.edu> NSBRI Research Scientist/Programmer Computer and Information Technology Institute Rice University, 6100 S. Main St., Houston, TX 77005 --- lib/python/OFS/Document.py.orig Tue Dec 22 16:39:02 1998 +++ lib/python/OFS/Document.py Thu Feb 4 10:26:05 1999 @@ -96,7 +96,7 @@ ############################################################################## """Document object""" -__version__='$Revision: 1.70 $'[11:-2] +__version__='$Revision:1.71 $'[11:-2] from Globals import HTML, HTMLFile, MessageDialog from string import join,split,strip,rfind,atoi,lower @@ -104,8 +104,10 @@ from SimpleItem import Item_w__name__, pretty_tb from Acquisition import Explicit import regex, Globals, sys +import re from DocumentTemplate.DT_Util import cDocument + class Document(cDocument, HTML, Explicit, RoleManager, Item_w__name__, ): @@ -115,6 +117,8 @@ icon ='p_/doc' _proxy_roles=() + docDefaultExtension = 'ztml' #extension must be ?tml, if not change _downloadName() + download_mime_type='text/' + docDefaultExtension # Documents masquerade as functions: @@ -129,6 +133,9 @@ {'label':'Upload', 'action':'manage_uploadForm', 'target':'manage_main', }, + {'label':'Download', 'action':'manage_download', + 'target':'manage_main', + }, {'label':'View', 'action':'', 'target':'manage_main', }, @@ -143,7 +150,7 @@ __ac_permissions__=( ('View management screens', ['manage', 'manage_main','manage_editForm', 'manage_tabs', - 'manage_uploadForm']), + 'manage_uploadForm', 'manage_download']), ('Change permissions', ['manage_access']), ('Change Documents', ['manage_edit','manage_upload','PUT']), ('Change proxy roles', ['manage_proxyForm','manage_proxy']), @@ -169,7 +176,7 @@ kw['document_id'] =self.id kw['document_title']=self.title if client is None: - # Called as subtemplate, so don't need error propigation! + # Called as subtemplate, so don't need error propagation! r=apply(HTML.__call__, (self, client, REQUEST), kw) if RESPONSE is None: return r return decapitate(r, RESPONSE) @@ -275,6 +282,46 @@ message='Your changes have been saved', action ='manage_main') + def _downloadName(self): + attr = self.__name__ + if not re.search('\..tml$', attr): + attr = attr + '.' + self.docDefaultExtension + return attr + + def _setId(self, id): + """Cleaning garbage + + If we have created a download method named with the doc id + then we should delete it not to leave garbage""" + + if hasattr(self, self._downloadName()): + delattr(self, self._downloadName()) + #Item_w__name__._setId(self, id) + self.__name__ = id + + + def manage_realDownload(self, RESPONSE): + """ + The value of the Id of a class will be made the same as + this method. This is to make the filename appear in browser + download dialog. + """ + RESPONSE['content-type']=self.download_mime_type + + return self.read_raw() + + def manage_download(self, RESPONSE, URL1): + """ + download a document as self.download_mime_type + """ + + attr = self._downloadName() + if not hasattr(self, attr): + setattr(self, attr, self.manage_realDownload) + + RESPONSE.redirect(URL1 + '/' + attr) + + def PUT(self, BODY, REQUEST): """ replaces the contents of the document with the BODY of an HTTP PUT request. @@ -359,6 +406,8 @@ t.append(getattr(self,i['id'])) return t + + def decapitate(html, RESPONSE=None, header_re=regex.compile( '\(\('
"Ross J. Reedstrom" wrote:
Paulo's excellent patch was reversed - here it is as a unified diff in the forward direction made in the zope root directory - apply with: patch -p 0 <Document.py.diff
Thanks Ross. I've updated the patch link: http://samba-choro.com.br/neves/python/zope/Document.py.patch
(Python people - do I need to delete the Document.pyc file, or does python notice the timestamps and recompile? I moved it just to be sure)
No need to delete it. Python will recreate it if the source is present and is newer. You just have to restart Zope. []s -- Paulo Eduardo Neves PUC-Rio de Janeiro Pager: Central: 292-4499 cod. 213 99 64 ou use a URL: http://www.learn.fplf.org.br/neves/mensagempager.html
participants (2)
-
Paulo Eduardo Neves -
Ross J. Reedstrom