--- versionedFile/extVersionedFile.py 2007/09/10 17:40:37 1.8 +++ versionedFile/extVersionedFile.py 2008/01/02 16:58:35 1.18 @@ -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,26 +509,29 @@ 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 + logging.debug("addFile id=%s vf=%s of %s"%(repr(id),repr(vf),repr(self))) + # add its content (and don't index) + obj=vf.addContentObject(id,vC,author=author,file=file,content_type=content_type,from_tmp=isRealFile,index=False) + # add file to this folder (this should do the indexing) 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) - self.REQUEST.SESSION['objID']=vf.getId() - self.REQUEST.SESSION['objID_parent']=None + try: + self.REQUEST.SESSION['objID']=vf.getId() + self.REQUEST.SESSION['objID_parent']=None + except: + pass if obj.getSize()==0: 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): @@ -488,6 +557,9 @@ class extVersionedFileFolder(Folder,ECHO """fix last version number of all files""" for (id,vf) in self.getVersionedFiles(): vf.fixVersionNumbers() + # recursively + for (id,vf) in self.objectItems(self.meta_type): + vf.fixVersionNumbers() manage_addextVersionedFileFolderForm=DTMLFile('dtml/extfolderAdd', globals()) @@ -703,8 +775,12 @@ class extVersionedFile(CatalogAware,Fold meta_type = 'extVersionedFile' # meta_type of contained objects content_meta_type = ['extVersionedFileObject'] + # default catalog for extVersionedFile objects default_catalog = 'fileCatalog' + manage_options = Folder.manage_options+({'label':'Main Config','action':'changeVersionedFileForm'},) + + security=ClassSecurityInfo() def __init__(self, id, title, lockedBy,author): @@ -798,8 +874,10 @@ class extVersionedFile(CatalogAware,Fold def getLastChangeDate(self): """get last change date""" lv=self.getContentObject() - time=lv.getTime() - return time + if lv: + time=lv.getTime() + return time + return None def getLastEditor(self): """get last change date""" @@ -851,14 +929,15 @@ class extVersionedFile(CatalogAware,Fold def getContentObject(self): """returns the last version object""" - if not self.lastVersionId: + if not getattr(self, 'lastVersionId', None): + # find last version and save it lv = self.findLastVersion() if lv is None: return None self.lastVersionNumber = lv.getVersionNumber() self.lastVersionId = lv.getId() - return getattr(self, self.lastVersionId) + return getattr(self, self.lastVersionId, None) security.declarePublic('getData') def getData(self): @@ -954,7 +1033,7 @@ class extVersionedFile(CatalogAware,Fold return extVersionedFileObject(id,title,versionNumber=versionNumber,versionComment=versionComment,time=time,author=author) - def addContentObject(self,id,vC,author=None,file=None,title='',changeName='no',newName='',from_tmp=False, + def addContentObject(self,id,vC,author=None,file=None,title='',changeName='no',newName='',from_tmp=False,index=True, precondition='', content_type=''): """add""" @@ -1002,10 +1081,11 @@ class extVersionedFile(CatalogAware,Fold self.lastVersionNumber = versNum self.lastVersionId = id - 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: lastversion=%s"%self.getData()) + #logging.debug("addcontentobject: fob_data=%s"%fob.getData()) + if index and self.default_catalog: + logging.debug("reindex1: %s in %s"%(repr(self),repr(self.default_catalog))) + self.reindex_object() return fob @@ -1052,13 +1132,34 @@ class extVersionedFile(CatalogAware,Fold else: return ob + + changeVersionedFileForm = PageTemplateFile('zpt/changeVersionedFile', globals()) + + def manage_changeVersionedFile(self,title,vC,author,comment,RESPONSE=None): + """Change VersionedFile metadata""" + self.title = title + self.author = author + cob = self.getContentObject() + if cob: + if vC: + cob.vComment=vC + + if comment=='': + cob.versionComment=None + else: + cob.versionComment=comment + + if RESPONSE: + RESPONSE.redirect('manage_main') + def download(self): - """download and lock""" + """download""" self.REQUEST.RESPONSE.setHeader("Content-Disposition","""attachement; filename=%s"""%self.getContentObject().getId()) self.REQUEST.RESPONSE.setHeader("Content-Type","application/octet-stream") self.content_type="application/octet-stream" self.REQUEST.RESPONSE.redirect(self.REQUEST['URL1']+'/'+self.getId()+'/'+self.getContentObject().getId()) + security.declareProtected('AUTHENTICATED_USER','downloadLocked') def downloadLocked(self):