[Zope] What about adding a download tab to Document?

Ross J. Reedstrom reedstrm@rice.edu
Thu, 04 Feb 1999 10:36:32 -0600


This is a multi-part message in MIME format.
--------------13E01A98268768AF82465A04
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

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
--------------13E01A98268768AF82465A04
Content-Type: text/plain; charset=us-ascii; name="Document.py.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="Document.py.diff"

--- 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(
                    '\(\('

--------------13E01A98268768AF82465A04--