--- versionedFile/versionedFile.py 2005/08/15 09:48:35 1.42 +++ versionedFile/versionedFile.py 2010/05/12 10:58:21 1.74 @@ -1,33 +1,42 @@ - from OFS.Folder import Folder from OFS.Image import File from OFS.Image import cookId +from OFS.DTMLDocument import DTMLDocument from Globals import DTMLFile, InitializeClass,package_home from Products.PageTemplates.PageTemplateFile import PageTemplateFile from AccessControl import getSecurityManager from Products.PageTemplates.PageTemplate import PageTemplate from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate from AccessControl import ClassSecurityInfo -from Products.ImageArchive.ImageArchive import manage_AddImageZogiLib +from difflib import Differ +from pprint import pprint +from Products.ZCatalog.CatalogPathAwareness import CatalogAware + +try: + from Products.ImageArchive.ImageArchive import manage_AddImageZogiLib +except: + print "no images" + from threading import Thread import shutil import tempfile import os.path - - +import urllib +import logging import time try: from Products.ECHO_content.ECHO_collection import ECHO_basis except: print "ECHO Elements not imported" class ECHO_basis: - """leer""" - manage_options=() + """leer""" + manage_options=() - + def sortv(x,y): return cmp(x[0],y[0]) + tdir = "/tmp/downloadVersionedFiles" class generateDownloadZip: @@ -43,7 +52,7 @@ class generateDownloadZip: def __call__(self): """call generate download zip""" storeTempDir=tempfile.tempdir - tempfile.tempdir=tdir + tempfile.tempdir=tdir tmpPath=tempfile.mktemp() tmpZip=tempfile.mktemp()+".gtz" @@ -53,9 +62,10 @@ class generateDownloadZip: os.mkdir(tempfile.tempdir) if not os.path.exists(tmpPath): - os.mkdir(tmpPath) - - self.response="

1. step: getting the files

" + os.mkdir(tmpPath) + + tempfile.tempdir=storeTempDir + self.response="

1. step: getting the files

" for files in self.folder.ZopeFind(self.folder,obj_metatypes=['versionedFile']): lastV=files[1].getLastVersion() @@ -63,11 +73,20 @@ class generateDownloadZip: savePath=os.path.join(tmpPath,lastV.title) fh=file(savePath,"w") - fh.write(lastV.data) + logging.debug("writing file %1 data in %s"%(lastV.title,savePath)) + # loop to get OFS.Image.File data + data=lastV.data + if isinstance(data, str): + fh.write(data) + else: + while data is not None: + fh.write(data.data) + data=data.next + fh.close() self.response+="

2. step: creating the downloadable file

" - self.response+="

Create gtar
" + self.response+="

Create gtar
" self.response+="

This can take a while....
\n" fh=os.popen2("tar zcvf %s %s/*"%(tmpZip,tmpPath),1)[1] @@ -76,8 +95,8 @@ class generateDownloadZip: self.response+=c if c==")": self.response+="
\n" - - + + shutil.rmtree(tmpPath) @@ -89,7 +108,7 @@ class generateDownloadZip: self.response+="""

Click here for download ( %i Byte)

\n"""%(tmpFn,len) self.response+="""

The file you receive is a tar (gnutar) compressed file, after unpacking you will find a new folder tmp where the files are stored in.

""" self.response+="""

The file will be stored for a while, you can download it later, the URL is:

-

%s?fn=%s\n"""%(tmpFn,downloadUrl,tmpFn) +

%s?fn=%s\n"""%(tmpFn,downloadUrl,tmpFn) self.done=True @@ -113,20 +132,69 @@ class versionedFileFolder(Folder,ECHO_ba security= ClassSecurityInfo() security.declareProtected('AUTHENTICATED_USER','addFileForm') - + filesMetaType=['versionedFile'] if ECHO_basis: optTMP= Folder.manage_options+ECHO_basis.manage_options else: optTMP= Folder.manage_options manage_options =optTMP+( - {'label':'Generate Index.html','action':'generateIndexHTML'}, + {'label':'Generate Index.html','action':'generateIndexHTML'}, {'label':'Generate Image Index.html','action':'generateIndexHTML_image'}, {'label':'Generate history_template.html','action':'generateHistoryHTML'}, + {'label':'Generate addFileForm','action':'generateAddFileForm'}, {'label':'Import Folder','action':'importFolderForm'}, {'label':'Export Folder','action':'exportFolder'}, + {'label':'Position of version number','action':'changeHistoryFileNamesForm'}, ) + + def changeHistoryFileNamesForm(self): + """change position of version num""" + pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','changeHistoryFileNamesForm.zpt')).__of__(self) + return pt() + + + def changeHistoryFileNames(self,positionVersionNum="front",RESPONSE=None): + """change position of version num""" + + + + versions=self.ZopeFind(self,obj_metatypes=['versionedFileObject'],search_sub=1) + + if not (getattr(self,'positionVersionNum','front')==positionVersionNum): + + for version in versions: + + if positionVersionNum=="front": + + titleTmp=os.path.splitext(version[1].title) + titleTmp2="_".join(titleTmp[0].split("_")[0:-1]) + if len(titleTmp)>1: + id=titleTmp[0].split("_")[-1]+"_"+titleTmp2+"."+titleTmp[1] + else: + id=titleTmp[0].split("_")[-1]+"_"+titleTmp2 + + else: + titleTmp="_".join(version[1].getId().split("_")[1:]) + tmp=os.path.splitext(titleTmp) + if len(tmp)>1: + id=tmp[0]+"_"+version[1].getId().split("_")[0]+tmp[1] + else: + id=tmp[0]+"_"+version[1].getId().split("_")[0] + + + + version[1].aq_parent.manage_renameObjects(ids=[version[1].getId()],new_ids=[id]) + version[1].title=id + + + self.positionVersionNum=positionVersionNum + if RESPONSE: + RESPONSE.redirect("manage_main") + + + def importFolderForm(self): """form fuer folder import""" pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','importFolderForm.zpt')).__of__(self) @@ -206,19 +274,19 @@ class versionedFileFolder(Folder,ECHO_ba ## return pt() def downloadSet(self,fn): - """download prepared set""" - filename=os.path.join(tdir,fn) + """download prepared set""" + filename=os.path.join(tdir,fn) - - self.REQUEST.RESPONSE.setHeader("Content-Disposition","""attachement; filename="%s" """%"downloadFileFolder.tgz") - self.REQUEST.RESPONSE.setHeader("Content-Type","application/octet-stream") - len=os.stat(filename)[6] - self.REQUEST.RESPONSE.setHeader("Content-Length",len) - images=file(filename).read() - self.REQUEST.RESPONSE.write(images) - self.REQUEST.RESPONSE.close() + + self.REQUEST.RESPONSE.setHeader("Content-Disposition","""attachement; filename="%s" """%"downloadFileFolder.tgz") + self.REQUEST.RESPONSE.setHeader("Content-Type","application/octet-stream") + len=os.stat(filename)[6] + self.REQUEST.RESPONSE.setHeader("Content-Length",len) + images=file(filename).read() + self.REQUEST.RESPONSE.write(images) + self.REQUEST.RESPONSE.close() - + def helpDownload(self): """download help""" @@ -244,6 +312,26 @@ class versionedFileFolder(Folder,ECHO_ba if RESPONSE is not None: RESPONSE.redirect('manage_main') + + def generateAddFileForm(self,RESPONSE=None): + """lege standard addfileform an""" + #TODO: write generateaddfileform only a dummy at them moment + + if not self.ZopeFind(self,obj_ids=['addFileForm.html']): + zt=ZopePageTemplate('addFileForm.html') + self._setObject('addFileForm.html',zt) + default_content_fn = os.path.join(package_home(globals()), + 'zpt/fileAdd_template.zpt') + text = open(default_content_fn).read() + zt.pt_edit(text, 'text/html') + + else: + return "already exists!" + + if RESPONSE is not None: + RESPONSE.redirect('manage_main') + + def generateIndexHTML(self,RESPONSE=None): """lege standard index.html an""" @@ -299,12 +387,12 @@ class versionedFileFolder(Folder,ECHO_ba - try: - xc=getattr(x[1],'comment','ZZZZZZZZZZZZZ').lower() - except: - xc='ZZZZZZZZZZZZZ'.lower() - try: - yc=getattr(y[1],'comment','ZZZZZZZZZZZZZ').lower() + try: + xc=getattr(x[1],'comment','ZZZZZZZZZZZZZ').lower() + except: + xc='ZZZZZZZZZZZZZ'.lower() + try: + yc=getattr(y[1],'comment','ZZZZZZZZZZZZZ').lower() except: yc='ZZZZZZZZZZZZZ'.lower() @@ -329,8 +417,9 @@ class versionedFileFolder(Folder,ECHO_ba return cmp(x[1].getLastVersion().lastEditor().lower(),y[1].getLastVersion().lastEditor().lower()) - versionedFiles=self.ZopeFind(self,obj_metatypes=['versionedFile']) - + versionedFiles=self.ZopeFind(self,obj_metatypes=self.filesMetaType) + + if sortField=='title': versionedFiles.sort(sortName) elif sortField=='date': @@ -344,7 +433,7 @@ class versionedFileFolder(Folder,ECHO_ba def header_html(self): - """zusätzlicher header""" + """zusaetzlicher header""" ext=self.ZopeFind(self,obj_ids=["header.html"]) if ext: return ext[0][1]() @@ -363,13 +452,16 @@ class versionedFileFolder(Folder,ECHO_ba return pt() + def addFileForm(self): """add a file""" - ext=self.ZopeFind(self,obj_ids=["addFileForm.dtml"]) + ext=getattr(self,'addFileForm.html',None) + logging.error("Found %s"%ext) if ext: - return ext[0][1]('',globals(),version='1',AUTHENTICATED_USER=self.REQUEST.AUTHENTICATED_USER) + out=ext + else: - out=DTMLFile('dtml/newFileAdd', globals(),Kind='VersionedFileObject',kind='versionedFileObject',version='1').__of__(self) + out=DTMLFile('dtml/newFileAdd', globals(),Kind='VersionedFileObject',kind='versionedFileObject',version='1').__of__(self) return out() @@ -454,14 +546,23 @@ def manage_addVersionedFileFolder(self, class versionedFileObject(File): """File Object im Folder""" - + security= ClassSecurityInfo() meta_type = "versionedFileObject" manage_editForm =DTMLFile('dtml/fileEdit',globals(), Kind='File',kind='file') manage_editForm._setName('manage_editForm') - security= ClassSecurityInfo() + + + + security.declarePublic('getTitle') + + def getTitle(self): + """get title""" + return self.title + + security.declarePublic('getVComment') def getVComment(self): """get the comment of this file""" if not hasattr(self,'vComment') or (not self.vComment) or (self.vComment.lstrip()==""): @@ -496,39 +597,55 @@ class versionedFileObject(File): return REQUEST.RESPONSE.redirect(self.REQUEST.SESSION['refer']) return REQUEST.RESPONSE.redirect(self.aq_parent.absolute_url()+"/history") - security.declarePublic('getTime') + + security.declarePublic('getVersionComment') + def getVersionComment(self): + """getversioncomment""" + return self.versionComment + + security.declarePublic('getTime') + def getTime(self): """getTime""" #return self.bobobase_modification_time().ISO() if hasattr(self,'time'): return time.strftime("%Y-%m-%d %H:%M:%S",self.time) - elif hasattr(self,'timefixed'): - return self.timefixed + elif hasattr(self,'timefixed'): + return self.timefixed else: - setattr(self,'timefixed',self.bobobase_modification_time().ISO()) + setattr(self,'timefixed',self.bobobase_modification_time().ISO()) return self.bobobase_modification_time().ISO() - def download(self): + def download(self,REQUEST=None,RESPONSE=None): """download and lock""" self.REQUEST.RESPONSE.setHeader("Content-Disposition","""attachement; filename=%s"""%self.getId()) - self.REQUEST.RESPONSE.setHeader("Content-Type","application/octet-stream") - + self.REQUEST.RESPONSE.setHeader("Content-Type","application/octet-stream") + #try: + # txt=self.index_html() + #except: + # txt=self.index_html(REQUEST,RESPONSE) + # + #self.REQUEST.RESPONSE.setHeader("Content-Length","str(len(txt)+1000)") + self.content_type="application/octet-stream" - #self.REQUEST.RESPONSE.redirect(self.absolute_url()) - self.REQUEST.RESPONSE.write(self.index_html()) - #self.REQUEST.RESPONSE.write("bl") - self.REQUEST.close() + self.REQUEST.RESPONSE.redirect(self.absolute_url()) + #txt=urllib.urlopen(self.absolute_url()).read() + #self.REQUEST.RESPONSE.write(txt) + + + #self.REQUEST.close() + security.declareProtected('AUTHENTICATED_USER','downloadLocked') def downloadLocked(self): """download and lock""" - if self.REQUEST['AUTHENTICATED_USER']=='Anonymous User': + if repr(self.REQUEST['AUTHENTICATED_USER'])=='Anonymous User': return "please login first" if not self.aq_parent.lockedBy=="": return "cannot be locked because is already locked by %s"%self.lockedBy @@ -541,24 +658,27 @@ class versionedFileObject(File): """set version""" self.versionNumber=versionNumber + security.declarePublic('getVersionNumber') + def getVersionNumber(self): """get version""" return self.versionNumber - security.declarePublic('getVersionComment') - def getVersionComment(self): - """get version""" - return self.versionComment - security.declarePublic('lastEditor') + + security.declarePublic('lastEditor') + def lastEditor(self): """last Editor""" if hasattr(self,'author'): - ret=self.author.replace("-","\n") + try: + ret=self.author.replace("-","\n") + except:#old version of versionded file sometimes stored the user object and not only the name the following corrects this + ret=str(self.author).replace("-","\n") ret=ret.replace("\r","\n") - return ret + return ret.lstrip().rstrip() else: jar=self._p_jar @@ -608,9 +728,22 @@ def manage_addVersionedFileObject(self,i -class versionedFile(Folder): +class versionedFile(CatalogAware,Folder): """Versioniertes File""" + default_catalog='fileCatalog' + + security= ClassSecurityInfo() + + security.declarePublic('getTitle') + def getTitle(self): + """get title""" + return self.title + + def PrincipiaSearchSource(self): + """Return cataloguable key for ourselves.""" + return str(self) + def __init__(self, id, title, lockedBy,author): """init""" self.id=id @@ -667,7 +800,7 @@ class versionedFile(Folder): else: return images - + security.declarePublic('getComment') def getComment(self): """get the comment of this file""" if not hasattr(self,'comment') or (not self.comment) or (self.comment.lstrip()==""): @@ -696,12 +829,32 @@ class versionedFile(Folder): self.comment_date=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) return REQUEST.RESPONSE.redirect(self.aq_parent.absolute_url()) + + security.declarePublic('getLastChangeDate') + def getLastChangeDate(self): + """get last change date""" + lv=self.getLastVersion() + time=lv.getTime() + return time + + def getLastEditor(self): + """get last change date""" + lv=self.getLastVersion() + le=lv.lastEditor() + return le + + def getLockedBy(self): + """get locked by""" + return str(self.lockedBy) + + security.declarePublic('getLastVersion') def getLastVersion(self): """Last Version""" tmp=0 lastVersion=None + for version in self.ZopeFind(self): if hasattr(version[1],'versionNumber'): @@ -709,14 +862,63 @@ class versionedFile(Folder): if int(version[1].versionNumber) > tmp: tmp=int(version[1].versionNumber,) lastVersion=version[1] + if lastVersion==None: + lastVersion=version[1] + lastVersion.versionNumber=1 return lastVersion + def getVersionNr(self,nr): + """Last Version""" + tmp=0 + lastVersion=None + + + for version in self.ZopeFind(self): + + if hasattr(version[1],'versionNumber'): + + if int(version[1].versionNumber) ==nr : + return version[1] + + return None + def diff(self,data): + """differenz between lastversion and data""" + d=Differ() + data=data.rstrip() + try: + tmp=self.getLastVersion().data.rstrip() + except: + tmp=str(self.getLastVersion().data).rstrip() + #print "XX",data,tmp + + try: + l=list(d.compare(data.splitlines(1),tmp.splitlines(1))) + except: + try: + l=list(d.compare(repr(data).splitlines(1),tmp.splitlines(1))) + except: + return 9999,[] + + plus=0 + minus=0 + + for a in l: + if a[0]=='+': + plus+=1 + if a[0]=='-': + minus+=1 + + + return max([plus,minus]),l + security.declarePublic('index_html') def index_html(self): """main view""" lastVersion=self.getLastVersion() #return "File:"+self.title+" Version:%i"%lastVersion.versionNumber," modified:",lastVersion.bobobase_modification_time()," size:",lastVersion.getSize(),"modified by:",lastVersion.lastEditor() return "File: %s Version:%i modified:%s size:%s modified by:%s"%(self.title,lastVersion.versionNumber,lastVersion.getTime(),lastVersion.getSize(),lastVersion.lastEditor()) - security= ClassSecurityInfo() + + + security.declarePublic('getVersion') def getVersion(self): tmp=0 @@ -751,16 +953,22 @@ class versionedFile(Folder): return ret security.declareProtected('AUTHENTICATED_USER','forceunlock') - def forceunlock(self,RESPONSE): + def forceunlock(self,RESPONSE=None): """unlock""" + #safe who had the lock + if self.lockedBy: + self.brokenLock=str(self.lockedBy) + else: + self.brokenLock="" self.lockedBy='' + return self.brokenLock security.declareProtected('AUTHENTICATED_USER','unlock') def unlock(self,RESPONSE): """unlock""" if str(self.lockedBy) in [str(self.REQUEST['AUTHENTICATED_USER'])]: self.lockedBy='' - RESPONSE.redirect(self.REQUEST['URL2']) + RESPONSE.redirect(self.REQUEST['HTTP_REFERER']) else: return "Sorry, not locked by you! (%s,%s)"%(self.lockedBy,self.REQUEST['AUTHENTICATED_USER']) @@ -773,9 +981,13 @@ class versionedFile(Folder): if str(self.REQUEST['AUTHENTICATED_USER']) in ["Anonymous User"]: return "please login first" - if (self.lockedBy==self.REQUEST['AUTHENTICATED_USER']) or (self.lockedBy==''): - out=DTMLFile('dtml/fileAdd', globals(),Kind='VersionedFileObject',kind='versionedFileObject',version=self.getVersion()).__of__(self) - return out() + if (self.lockedBy==self.REQUEST['AUTHENTICATED_USER']) or (self.lockedBy=='') or (self.lockedBy==None): + ext=self.ZopeFind(self.aq_parent,obj_ids=["addNewVersion.dtml"]) + if ext: + return ext[0][1]('',globals(),version=self.getVersion(),lastComment=self.getLastVersion().getVersionComment(),AUTHENTICATED_USER=self.REQUEST.AUTHENTICATED_USER) + else: + out=DTMLFile('dtml/fileAdd', globals(),Kind='VersionedFileObject',kind='versionedFileObject',version=self.getVersion()).__of__(self) + return out() else: return "Sorry file is locked by somebody else" @@ -798,14 +1010,31 @@ class versionedFile(Folder): if not newName=='': self.title=newName[0:] - print self.title - id="V%i"%self.getVersion()+"_"+self.title - manage_addVersionedFileObject(self,id,vC,author,file,"V%i"%self.getVersion()+"_"+self.title,precondition, content_type) + + + + positionVersionNum=getattr(self,'positionVersionNum','front') + + if positionVersionNum=='front': + id="V%i"%self.getVersion()+"_"+self.title + else: + tmp=os.path.splitext(self.title) + if len(tmp)>1: + id=tmp[0]+"_V%i"%self.getVersion()+tmp[1] + else: + id=tmp[0]+"_V%i"%self.getVersion() + + + manage_addVersionedFileObject(self,id,vC,author,file,id,precondition, content_type) objs=self.ZopeFind(self,obj_ids=[id])[0][1].setVersionNumber(int(self.getVersion())) self.REQUEST.SESSION['objID_parent']=self.getId() + if getattr(self,'defaultFileCatalog',None): + + self.reindex_object() + if RESPONSE: obj=self.ZopeFind(self,obj_ids=[id])[0][1] if obj.getSize()==0: @@ -825,19 +1054,28 @@ class versionedFile(Folder): """download and lock""" self.REQUEST.RESPONSE.setHeader("Content-Disposition","""attachement; filename=%s"""%self.getLastVersion().getId()) - self.REQUEST.RESPONSE.setHeader("Content-Type","application/octet-stream") + self.REQUEST.RESPONSE.setHeader("Content-Type","application/octet-stream") + #try: + # txt=self.getLastVersion.index_html() + #except: + # txt=self.getLastVersion.index_html(REQUEST,RESPONSE) + + #self.REQUEST.RESPONSE.setHeader("Content-Length","str(len(txt)+1000)") + self.content_type="application/octet-stream" #self.REQUEST.RESPONSE.write("bl") - self.REQUEST.RESPONSE.write(self.getLastVersion().index_html()) - self.REQUEST.close() + #self.REQUEST.RESPONSE.write(txt) + #self.REQUEST.close() #self.getLastVersion().content_type="application/octet-stream" - #self.REQUEST.RESPONSE.redirect(self.REQUEST['URL1']+'/'+self.getId()+'/'+self.getLastVersion().getId()) + self.REQUEST.RESPONSE.redirect(self.REQUEST['URL1']+'/'+self.getId()+'/'+self.getLastVersion().getId()) + security.declareProtected('AUTHENTICATED_USER','downloadLocked') def downloadLocked(self): """download and lock""" - if self.REQUEST['AUTHENTICATED_USER']=='Anonymous User': + + if repr(self.REQUEST['AUTHENTICATED_USER'])=='Anonymous User': return "please login first" if not self.lockedBy=="": return "cannot be locked because is already locked by %s"%self.lockedBy