--- versionedFile/versionedFile.py 2004/07/05 11:24:01 1.5 +++ versionedFile/versionedFile.py 2004/11/04 08:53:38 1.18 @@ -8,7 +8,7 @@ from Products.PageTemplates.PageTemplate from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate from AccessControl import ClassSecurityInfo import os.path - +import time def sortv(x,y): return cmp(x[0],y[0]) @@ -16,6 +16,7 @@ def sortv(x,y): class versionedFileFolder(Folder): """Folder with versioned files""" + meta_type = "versionedFileFolder" security= ClassSecurityInfo() @@ -23,8 +24,15 @@ class versionedFileFolder(Folder): manage_options = Folder.manage_options+( {'label':'Generate Index.html','action':'generateIndexHTML'}, + {'label':'Generate history_template.html','action':'generateHistoryHTML'}, ) + def helpDownload(self): + """download help""" + + pt=PageTemplateFile('Products/versionedFile/zpt/helpDownload').__of__(self) + return pt() + def generateIndexHTML(self,RESPONSE=None): """lege standard index.html an""" @@ -45,12 +53,49 @@ class versionedFileFolder(Folder): if RESPONSE is not None: RESPONSE.redirect('manage_main') + + def generateHistoryHTML(self,RESPONSE=None): + """lege standard index.html an""" + + + + + if not hasattr(self,'history_template.html'): + zt=ZopePageTemplate('history_template.html') + self._setObject('history_template.html',zt) + default_content_fn = os.path.join(package_home(globals()), + 'zpt/versionHistory.zpt') + text = open(default_content_fn).read() + zt.pt_edit(text, 'text/html') + + else: + return "already exists!" - def getVersionedFiles(self): + if RESPONSE is not None: + RESPONSE.redirect('manage_main') + + def getVersionedFiles(self,sortField='title'): """get all versioned files""" + + def sortName(x,y): + return cmp(x[1].title.lower(),y[1].title.lower()) + + def sortDate(x,y): + return cmp(y[1].getLastVersion().getTime(),x[1].getLastVersion().getTime()) + + def sortAuthor(x,y): + + return cmp(x[1].getLastVersion().lastEditor(),y[1].getLastVersion().lastEditor()) versionedFiles=self.ZopeFind(self,obj_metatypes=['versionedFile']) - + + if sortField=='title': + versionedFiles.sort(sortName) + elif sortField=='date': + versionedFiles.sort(sortDate) + elif sortField=='author': + versionedFiles.sort(sortAuthor) + return versionedFiles @@ -78,9 +123,12 @@ class versionedFileFolder(Folder): return out() - def addFile(self,vC,file,author,content_type='',RESPONSE=None): + def addFile(self,vC,file,author,newName='',content_type='',RESPONSE=None): """ add a new file""" - id=file.filename + if newName=='': + id=file.filename + else: + id=newName vC=self.REQUEST.form['vC'] manage_addVersionedFile(self,id,'','') @@ -135,7 +183,41 @@ class versionedFileObject(File): Kind='File',kind='file') manage_editForm._setName('manage_editForm') + 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 + else: + setattr(self,'timefixed',self.bobobase_modification_time().ISO()) + return self.bobobase_modification_time().ISO() + + + + + def download(self): + """download and lock""" + + + self.content_type="application/octet-stream" + self.REQUEST.RESPONSE.redirect(self.absolute_url()) + + def downloadLocked(self): + """download and lock""" + + + if 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 + self.aq_parent.lockedBy=self.REQUEST['AUTHENTICATED_USER'] + + self.content_type="application/octet-stream" + self.REQUEST.RESPONSE.redirect(self.absolute_url()) + def setVersionNumber(self,versionNumber): """set version""" self.versionNumber=versionNumber @@ -179,6 +261,8 @@ def manage_addVersionedFileObject(self,i # First, we create the file without data: self._setObject(id, versionedFileObject(id,title,'',content_type, precondition)) self._getOb(id).versionComment=str(vC) + self._getOb(id).time=time.localtime() + setattr(self._getOb(id),'author',author) # Now we "upload" the data. By doing this in two steps, we @@ -203,9 +287,11 @@ class versionedFile(Folder): self.title=title self.lockedBy=lockedBy self.author=author + meta_type="versionedFile" + def getLastVersion(self): """Last Version""" tmp=0 @@ -224,7 +310,7 @@ class versionedFile(Folder): """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.bobobase_modification_time(),lastVersion.getSize(),lastVersion.lastEditor()) + return "File: %s Version:%i modified:%s size:%s modified by:%s"%(self.title,lastVersion.versionNumber,lastVersion.getTime(),lastVersion.getSize(),lastVersion.lastEditor()) def getVersion(self): tmp=0 @@ -240,7 +326,12 @@ class versionedFile(Folder): security.declareProtected('AUTHENTICATED_USER','unlock') def history(self): - """history""" + """history""" + + ext=self.ZopeFind(self.aq_parent,obj_ids=["history_template.html"]) + if ext: + return getattr(self,ext[0][1].getId())() + pt=PageTemplateFile('Products/versionedFile/zpt/versionHistory').__of__(self) return pt() @@ -253,7 +344,7 @@ class versionedFile(Folder): ret.sort(sortv) return ret - + security.declareProtected('AUTHENTICATED_USER','unlock') def unlock(self,RESPONSE): """unlock""" if str(self.lockedBy) in [str(self.REQUEST['AUTHENTICATED_USER'])]: @@ -262,7 +353,7 @@ class versionedFile(Folder): else: return "Sorry, not locked by you! (%s,%s)"%(self.lockedBy,self.REQUEST['AUTHENTICATED_USER']) - security= ClassSecurityInfo() + security.declareProtected('AUTHENTICATED_USER','addVersionedFileObjectForm') def addVersionedFileObjectForm(self): @@ -276,11 +367,18 @@ class versionedFile(Folder): else: return "Sorry file is locked by somebody else" - def manage_addVersionedFileObject(self,id,vC,author,file='',title='',precondition='', content_type='',RESPONSE=None): + def manage_addVersionedFileObject(self,id,vC,author,file='',title='',precondition='', content_type='',changeName='no',newName='', RESPONSE=None): """add""" vC=self.REQUEST['vC'] author=self.REQUEST['author'] + + if changeName=="yes": + self.title=file.filename[0:] + + if not newName=='': + self.title=newName[0:] + id="V%i"%self.getVersion()+"_"+self.title manage_addVersionedFileObject(self,id,vC,author,file,"V%i"%self.getVersion()+"_"+self.title,precondition, content_type) objs=self.ZopeFind(self,obj_ids=[id])[0][1].setVersionNumber(int(self.getVersion())) @@ -290,6 +388,11 @@ class versionedFile(Folder): security.declareProtected('AUTHENTICATED_USER','downloadLocked') + def download(self): + """download and lock""" + self.getLastVersion().content_type="application/octet-stream" + self.REQUEST.RESPONSE.redirect(self.REQUEST['URL1']+'/'+self.getId()+'/'+self.getLastVersion().getId()) + def downloadLocked(self): """download and lock""" if self.REQUEST['AUTHENTICATED_USER']=='Anonymous User': @@ -297,7 +400,7 @@ class versionedFile(Folder): if not self.lockedBy=="": return "cannot be locked because is already locked by %s"%self.lockedBy self.lockedBy=self.REQUEST['AUTHENTICATED_USER'] - + self.getLastVersion().content_type="application/octet-stream" self.REQUEST.RESPONSE.redirect(self.REQUEST['URL1']+'/'+self.getId()+'/'+self.getLastVersion().getId()) def manage_addVersionedFileForm(self):