--- versionedFile/extVersionedFile.py 2007/09/10 17:40:37 1.8 +++ versionedFile/extVersionedFile.py 2007/09/14 15:08:02 1.12 @@ -29,6 +29,7 @@ import os.path import urllib import time import logging +import types try: from Products.ECHO_content.ECHO_collection import ECHO_basis @@ -134,8 +135,9 @@ class extVersionedFileFolder(Folder,ECHO {'label':'Generate Index.html','action':'generateIndexHTML'}, {'label':'Generate Image Index.html','action':'generateIndexHTML_image'}, {'label':'Generate history_template.html','action':'generateHistoryHTML'}, - {'label':'Import Folder','action':'importFolderForm'}, - {'label':'Export Folder','action':'exportFolder'}, + {'label':'Import directory','action':'importFolderForm'}, + {'label':'Export as file','action':'exportFolder'}, + {'label':'Import versionedFileFolder','action':'importVersionedFileFolderForm'}, {'label':'Position of version number','action':'changeHistoryFileNamesForm'}, ) @@ -196,6 +198,62 @@ class extVersionedFileFolder(Folder,ECHO if RESPONSE: RESPONSE.redirect(self.REQUEST['URL1']) + def importVersionedFileFolderForm(self): + """form fuer versionedFileFolder import""" + pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','importVersionedFileFolderForm.zpt')).__of__(self) + return pt() + + def importVersionedFileFolder(self,path,RESPONSE=None): + """import contents of a versionedFileFolder on the server""" + vff = getattr(self.aq_parent, path, None) + if vff is None: + return "SORRY, unable to import %s"%path + + tmpPath=tempfile.mktemp() + if not os.path.exists(tempfile.tempdir): + os.mkdir(tempfile.tempdir) + + if not os.path.exists(tmpPath): + os.mkdir(tmpPath) + + for (vfn, vf) in vff.getVersionedFiles(): + if vf.meta_type == 'versionedFile': + logging.error("importvff: importing %s of type %s!"%(vfn,vf.meta_type)) + title = vf.title + fob = vf.getLastVersion() + author = fob.getLastEditor() + vc = fob.getVersionComment() + # save file to filesystem + savePath=os.path.join(tmpPath,title) + fh=file(savePath,"w") + data = vf.getLastVersion().data + if isinstance(data, str): + # simple data object + fh.write(data) + else: + # chained data objects + while data is not None: + fh.write(data.data) + data = data.next + fh.close() + # and read in again + fh = file(savePath) + logging.error("importvff: comment=%s author=%s!"%(vc,author)) + self.addFile(vC=vc, file=fh, author=author) + # copy more fields + newfob = getattr(self, vfn).getContentObject() + newfob.vComment = fob.vComment + newfob.time = fob.time + logging.error("importvff: vc=%s time=%s of %s!"%(fob.vComment,fob.time,fob.getId())) + + else: + logging.error("importvff: unable to import %s of type %s!"%(vfn,vf.meta_type)) + + shutil.rmtree(tmpPath) + + if RESPONSE: + RESPONSE.redirect(self.REQUEST['URL1']) + zipThreads={} zipThreads2={} @@ -433,8 +491,16 @@ class extVersionedFileFolder(Folder,ECHO def addFile(self,vC,file,author='',newName='',content_type='',RESPONSE=None): """ add a new file""" + # is file is a real file or a zope download object? + isRealFile = type(file) is types.FileType + if newName=='': - filename=file.filename + logging.debug("fileobject: %s real:%s"%(repr(file),repr(isRealFile))) + if isRealFile: + filename = file.name + else: + filename=file.filename + id=filename[max(filename.rfind('/'), filename.rfind('\\'), filename.rfind(':'), @@ -443,17 +509,17 @@ class extVersionedFileFolder(Folder,ECHO else: id=newName - vC=self.REQUEST.form['vC'] + if vC is None: + vC=self.REQUEST.form['vC'] # get new extVersionedFile vf = self._newVersionedFile(id,title=id) #if (getattr(self,'commentNonEmpty',0)==1) and vC.strip()=="": # add file to this folder self._setObject(id,vf) - file2=file # add its content logging.info("ADD: %s"%repr(vf)) - obj=vf.addContentObject(id,vC,author=author,file=file2,content_type=content_type) + obj=vf.addContentObject(id,vC,author=author,file=file,content_type=content_type,from_tmp=isRealFile) self.REQUEST.SESSION['objID']=vf.getId() self.REQUEST.SESSION['objID_parent']=None @@ -462,7 +528,8 @@ class extVersionedFileFolder(Folder,ECHO pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','errorUploadFile')).__of__(self) return pt() - RESPONSE.redirect(self.REQUEST['URL1']) + if RESPONSE is not None: + RESPONSE.redirect(self.REQUEST['URL1']) def _newVersionedFile(self, id, title='', lockedBy=None, author=None): @@ -1002,10 +1069,10 @@ class extVersionedFile(CatalogAware,Fold self.lastVersionNumber = versNum self.lastVersionId = id - logging.debug("addcontentobject: lastversion=%s"%self.getData()) + #logging.debug("addcontentobject: lastversion=%s"%self.getData()) logging.debug("reindex1: %s in %s"%(repr(self),repr(self.default_catalog))) self.reindex_object() - logging.debug("addcontentobject: fob_data=%s"%fob.getData()) + #logging.debug("addcontentobject: fob_data=%s"%fob.getData()) return fob