--- versionedFile/versionedFile.py 2006/05/10 16:58:49 1.57 +++ versionedFile/versionedFile.py 2007/04/03 13:18:47 1.70 @@ -1,6 +1,7 @@ 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 @@ -9,7 +10,6 @@ from Products.PageTemplates.ZopePageTemp from AccessControl import ClassSecurityInfo from difflib import Differ from pprint import pprint - from Products.ZCatalog.CatalogPathAwareness import CatalogAware try: @@ -23,19 +23,20 @@ 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: @@ -51,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" @@ -61,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() @@ -75,7 +77,7 @@ class generateDownloadZip: 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] @@ -84,8 +86,8 @@ class generateDownloadZip: self.response+=c if c==")": self.response+="
\n" - - + + shutil.rmtree(tmpPath) @@ -97,7 +99,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 @@ -128,14 +130,16 @@ class versionedFileFolder(Folder,ECHO_ba 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) @@ -261,19 +265,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""" @@ -299,6 +303,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""" @@ -354,12 +378,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() @@ -419,13 +443,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() @@ -518,6 +545,8 @@ class versionedFileObject(File): manage_editForm._setName('manage_editForm') + + security.declarePublic('getTitle') def getTitle(self): @@ -559,7 +588,7 @@ class versionedFileObject(File): return REQUEST.RESPONSE.redirect(self.REQUEST.SESSION['refer']) return REQUEST.RESPONSE.redirect(self.aq_parent.absolute_url()+"/history") - + security.declarePublic('getVersionComment') def getVersionComment(self): """getversioncomment""" @@ -573,7 +602,7 @@ class versionedFileObject(File): if hasattr(self,'time'): return time.strftime("%Y-%m-%d %H:%M:%S",self.time) elif hasattr(self,'timefixed'): - return self.timefixed + return self.timefixed else: setattr(self,'timefixed',self.bobobase_modification_time().ISO()) return self.bobobase_modification_time().ISO() @@ -587,18 +616,18 @@ class versionedFileObject(File): self.REQUEST.RESPONSE.setHeader("Content-Disposition","""attachement; filename=%s"""%self.getId()) 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)") - + #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()) #txt=urllib.urlopen(self.absolute_url()).read() #self.REQUEST.RESPONSE.write(txt) - + #self.REQUEST.close() @@ -627,14 +656,10 @@ class versionedFileObject(File): """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""" @@ -644,7 +669,7 @@ class versionedFileObject(File): 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 @@ -699,6 +724,9 @@ class versionedFile(CatalogAware,Folder) default_catalog='fileCatalog' + security= ClassSecurityInfo() + + security.declarePublic('getTitle') def getTitle(self): """get title""" return self.title @@ -763,7 +791,7 @@ class versionedFile(CatalogAware,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()==""): @@ -792,7 +820,26 @@ class versionedFile(CatalogAware,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 @@ -815,12 +862,24 @@ class versionedFile(CatalogAware,Folder) def diff(self,data): """differenz between lastversion and data""" d=Differ() - tmp=self.getLastVersion().data + data=data.rstrip() + try: + tmp=self.getLastVersion().data.rstrip() + except: + tmp=str(self.getLastVersion().data).rstrip() #print "XX",data,tmp - l=list(d.compare(data.splitlines(1),tmp.splitlines(1))) - + + 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 @@ -829,13 +888,13 @@ class versionedFile(CatalogAware,Folder) 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') @@ -872,9 +931,15 @@ class versionedFile(CatalogAware,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): @@ -895,8 +960,12 @@ class versionedFile(CatalogAware,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() + 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" @@ -940,6 +1009,10 @@ class versionedFile(CatalogAware,Folder) 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: @@ -961,13 +1034,13 @@ class versionedFile(CatalogAware,Folder) self.REQUEST.RESPONSE.setHeader("Content-Disposition","""attachement; filename=%s"""%self.getLastVersion().getId()) self.REQUEST.RESPONSE.setHeader("Content-Type","application/octet-stream") - #try: - # txt=self.getLastVersion.index_html() - #except: - # txt=self.getLastVersion.index_html(REQUEST,RESPONSE) + #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.REQUEST.RESPONSE.setHeader("Content-Length","str(len(txt)+1000)") + self.content_type="application/octet-stream" #self.REQUEST.RESPONSE.write("bl") #self.REQUEST.RESPONSE.write(txt)