--- versionedFile/extVersionedFile.py 2007/09/10 16:34:16 1.7 +++ versionedFile/extVersionedFile.py 2008/06/26 16:38:25 1.20 @@ -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'}, ) @@ -186,17 +188,68 @@ class extVersionedFileFolder(Folder,ECHO return pt() def importFolder(self,path,comment="",author=None,lockedBy=None,RESPONSE=None): - """importiere inhalt eines folders""" - + """import contents of a folder on the server""" for fileName in os.listdir(path): - if os.path.isfile(os.path.join(path,fileName)): - manage_addextVersionedFile(self,fileName,'','') - id=fileName - ob=self._getOb(fileName) - ob.title=id - file2=file(os.path.join(path,fileName)) + fn = os.path.join(path,fileName) + if os.path.isfile(fn): + f = file(fn) + self.addFile(vC=comment, file=f, author=author) - obj=ob.manage_addextVersionedFileObject(id,comment,author,file2,content_type='') + 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']) @@ -216,6 +269,7 @@ class extVersionedFileFolder(Folder,ECHO downloadZip=generateDownloadZip(self,self.absolute_url()) downloadZip() return downloadZip.getResult() + ## if not threadName or threadName=="": ## threadStart=generateDownloadZip(self,self.absolute_url()) ## thread=Thread(target=threadStart) @@ -435,10 +489,18 @@ class extVersionedFileFolder(Folder,ECHO return out() - def addFile(self,vC,file,author,newName='',content_type='',RESPONSE=None): + 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(':'), @@ -447,24 +509,34 @@ class extVersionedFileFolder(Folder,ECHO else: id=newName - vC=self.REQUEST.form['vC'] - manage_addextVersionedFile(self,id,'','') - #if (getattr(self,'commentNonEmpty',0)==1) and vC.strip()=="": - - ob=self._getOb(id) - ob.title=id - file2=file - - logging.info("ADD: %s"%repr(ob)) - obj=ob.manage_addVersionedFileObject(id,vC,author,file2,content_type=content_type) - self.REQUEST.SESSION['objID']=ob.getId() - self.REQUEST.SESSION['objID_parent']=None + if vC is None: + vC=self.REQUEST.form['vC'] + + # get new extVersionedFile + vf = self._newVersionedFile(id,title=id) + 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) + + 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): + """factory for versioned files. to be overridden in derived classes.""" + return extVersionedFile(id, title, lockedBy=lockedBy, author=author) def deleteEmptyObject(self,submit,RESPONSE=None): @@ -485,7 +557,11 @@ 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()) @@ -616,6 +692,8 @@ class extVersionedFileObject(ExtFile): #self.REQUEST.close() + view = download + security.declareProtected('AUTHENTICATED_USER','downloadLocked') def downloadLocked(self): """download and lock""" @@ -699,8 +777,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): @@ -794,8 +876,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""" @@ -847,14 +931,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): @@ -865,6 +950,15 @@ class extVersionedFile(CatalogAware,Fold else: return None + security.declarePublic('view') + def view(self,REQUEST=None,RESPONSE=None): + """Returns the last version's view""" + ob = self.getContentObject() + if ob is not None: + return ob.view(REQUEST=REQUEST,RESPONSE=RESPONSE) + else: + return None + def diff(self,data): """differenz between lastversion and data""" d=Differ() @@ -924,6 +1018,17 @@ class extVersionedFile(CatalogAware,Fold ret.sort(sortv) return ret + def getVersionList(self): + """get a list of dicts with author, comment, filename, etc, of all versions""" + vl = [] + for v in self.objectValues(self.content_meta_type): + vl.append({'versionNumber':v.get('versionNumber',0), + 'title':v.getTitle(), + 'date':v.getTime(), + 'editor':v.getLastEditor(), + 'comment':v.get('versionComment','') + }) + security.declareProtected('AUTHENTICATED_USER','forceunlock') def forceunlock(self,RESPONSE=None): """unlock""" @@ -950,7 +1055,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""" @@ -998,10 +1103,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 @@ -1044,17 +1150,38 @@ class extVersionedFile(CatalogAware,Fold return pt() else: - RESPONSE.redirect(self.REQUEST['URL2']) + RESPONSE.redirect(self.absolute_url()+'/history') 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):