--- versionedFile/extVersionedFile.py 2007/11/23 18:51:39 1.17 +++ versionedFile/extVersionedFile.py 2010/05/11 09:03:08 1.36 @@ -3,6 +3,7 @@ using the ExtFile Product, this version DW 11.10.2006 """ +import email from OFS.Folder import Folder from OFS.Image import File from OFS.Image import cookId @@ -142,6 +143,20 @@ class extVersionedFileFolder(Folder,ECHO ) + def redirect(self,RESPONSE,url): + """mache ein redirect mit einem angehaengten time stamp um ein reload zu erzwingen""" + + timeStamp=time.time() + + if url.find("?")>-1: #giebt es schon parameter + addStr="&time=%s" + else: + addStr="?time=%s" + + RESPONSE.setHeader('Last-Modified',email.Utils.formatdate().split("-")[0]+'GMT') + logging.error(email.Utils.formatdate()+' GMT') + RESPONSE.redirect(url+addStr%timeStamp) + def changeHistoryFileNamesForm(self): """change position of version num""" pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','changeHistoryFileNamesForm.zpt')).__of__(self) @@ -412,8 +427,9 @@ class extVersionedFileFolder(Folder,ECHO return cmp(x[1].title.lower(),y[1].title.lower()) def sortDate(x,y): - return cmp(y[1].getContentObject().getTime(),x[1].getContentObject().getTime()) - + + return cmp(y[1].getContentObject().getTime(),x[1].getContentObject().getTime()) + def sortComment(x,y): try: xc=getattr(x[1],'comment','ZZZZZZZZZZZZZ').lower() @@ -430,19 +446,26 @@ class extVersionedFileFolder(Folder,ECHO xc=x[1].getContentObject().getVComment().lower() except: xc='ZZZZZZZZZZZZZ'.lower() + if (yc=='') or (yc=='ZZZZZZZZZZZZZ'.lower()): try: yc=y[1].getContentObject().getVComment().lower() except: yc='ZZZZZZZZZZZZZ'.lower() + return cmp(xc,yc) def sortAuthor(x,y): + + return cmp(x[1].getContentObject().lastEditor().lower(),y[1].getContentObject().lastEditor().lower()) + - return cmp(x[1].getContentObject().lastEditor().lower(),y[1].getContentObject().lastEditor().lower()) - + def sortVersionComment(x,y): + + return cmp(x[1].getContentObject().getVersionComment().lower(),y[1].getContentObject().getVersionComment().lower()) + versionedFiles=self.objectItems(self.file_meta_type) logging.debug("versionedfiles: %s of type %s"%(repr(versionedFiles),repr(self.file_meta_type))) @@ -452,9 +475,12 @@ class extVersionedFileFolder(Folder,ECHO versionedFiles.sort(sortDate) elif sortField=='author': versionedFiles.sort(sortAuthor) + elif sortField=='versioncomment': + versionedFiles.sort(sortVersionComment) elif sortField=='comment': versionedFiles.sort(sortComment) + return versionedFiles @@ -514,15 +540,20 @@ class extVersionedFileFolder(Folder,ECHO # get new extVersionedFile vf = self._newVersionedFile(id,title=id) - #if (getattr(self,'commentNonEmpty',0)==1) and vC.strip()=="": - # add file to this folder + logging.error("addFile id=%s vf=%s of %s"%(repr(id),repr(vf),repr(self))) + # add its content (and don't index) self._setObject(id,vf) - # add its content - logging.info("ADD: %s"%repr(vf)) - obj=vf.addContentObject(id,vC,author=author,file=file,content_type=content_type,from_tmp=isRealFile) + vf=getattr(self,id) + + 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) - 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) @@ -611,6 +642,13 @@ class extVersionedFileObject(ExtFile): self.time = time self.author = author + security.declareProtected('manage','changeObject') + def changeObject(self,**args): + """modify any of the objects attributes""" + for arg in args: + if hasattr(self, arg): + logging.debug("changeObject %s: %s=%s"%(repr(self),arg,args[arg])) + setattr(self, arg, args[arg]) security.declarePublic('getTitle') def getTitle(self): @@ -619,8 +657,28 @@ class extVersionedFileObject(ExtFile): def getData(self): """returns object content (calls ExtFile.index_html)""" - return ExtFile.index_html(self) + #logging.debug("+++++++getData1:"+repr(self.get_filename())) + filename = self.get_filename() + #return ExtFile.index_html(self) + try: + logging.info("readfile:"+filename) + return file(filename).read() + except: + logging.info("cannot readfile:"+filename) + return ExtFile.index_html(self) + + def getFileName(self): + """return filename""" + return self.get_filename() + + def addToFile(self,filehandle): + filehandle.write(self.getData()) + + def addToFile2(self,filename): + str="cat %s > %s"%(self.get_filename(),filename) + os.popen(str) + security.declarePublic('getVComment') def getVComment(self): """get the comment of this file""" @@ -683,18 +741,19 @@ class extVersionedFileObject(ExtFile): #self.REQUEST.RESPONSE.setHeader("Content-Length","str(len(txt)+1000)") self.content_type="application/octet-stream" - self.REQUEST.RESPONSE.redirect(self.absolute_url()) + return self.getData() + #self.REQUEST.RESPONSE.redirect(self.absolute_url()) #txt=urllib.urlopen(self.absolute_url()).read() #self.REQUEST.RESPONSE.write(txt) #self.REQUEST.close() + view = download + security.declareProtected('AUTHENTICATED_USER','downloadLocked') def downloadLocked(self): """download and lock""" - - if repr(self.REQUEST['AUTHENTICATED_USER'])=='Anonymous User': return "please login first" if not self.aq_parent.lockedBy=="": @@ -781,14 +840,17 @@ class extVersionedFile(CatalogAware,Fold security=ClassSecurityInfo() - def __init__(self, id, title, lockedBy,author): + def __init__(self, id, title, lockedBy,author,defaultAction='history'): """init""" self.id=id self.title=title self.lockedBy=lockedBy + if self.lockedBy is None: + self.lockedBy = '' self.author=author self.lastVersionNumber=0 self.lastVersionId=None + self.defaultAction = defaultAction security.declarePublic('getTitle') def getTitle(self): @@ -872,8 +934,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""" @@ -883,6 +947,8 @@ class extVersionedFile(CatalogAware,Fold def getLockedBy(self): """get locked by""" + if self.lockedBy is None: + self.lockedBy = '' return str(self.lockedBy) def getLastVersionNumber(self): @@ -911,39 +977,49 @@ class extVersionedFile(CatalogAware,Fold security.declarePublic('getLastVersion') def getLastVersion(self): """Last Version (old)""" - tmp=0 - lv=None - - for v in self.objectValues(self.content_meta_type): - logging.debug("getlastversion: check %s"%v.getId()) - if v.getVersionNumber() > tmp: - tmp=v.getVersionNumber() - lv=v - - logging.debug("getlastversion: got %s"%lv.getId()) - return lv - +# tmp=0 +# lv=None +# +# for v in self.objectValues(self.content_meta_type): +# #logging.debug("getlastversion: check %s"%v.getId()) +# if v.getVersionNumber() > tmp: +# tmp=v.getVersionNumber() +# lv=v +# +# #ogging.debug("getlastversion: got %s"%lv.getId()) +# return lv + return self.getContentObject(); + def getContentObject(self): """returns the last version object""" - if not hasattr(self, 'lastVersionId'): + if (not getattr(self, 'lastVersionId', None)) or (not getattr(self, 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): """Returns the content of the last version""" + logging.debug("+++++++getData2") ob = self.getContentObject() if ob is not None: return ob.getData() 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() @@ -965,13 +1041,30 @@ class extVersionedFile(CatalogAware,Fold security.declarePublic('index_html') - def index_html(self): + def index_html(self,REQUEST=None, RESPONSE=None): """main view""" #lastVersion=self.getContentObject() #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()) - return self.history() + act = getattr(self, 'defaultAction', 'history') + if act == 'download': + return self.getContentObject().download() + elif act == 'view': + #return self.getContentObject().download() + return self.getContentObject().index_html(REQUEST=REQUEST, RESPONSE=RESPONSE) + else: + return self.history() + def getVersionNr(self,nr): + """get version with number nr""" + 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 security.declarePublic('getVersion') def getVersion(self): @@ -1003,15 +1096,37 @@ 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':getattr(v,'versionNumber',0), + 'title':v.getTitle(), + 'id':v.getId(), + 'date':v.getTime(), + 'author':getattr(v,'author',''), + 'comment':getattr(v,'versionComment','') + }) + return vl + security.declareProtected('AUTHENTICATED_USER','forceunlock') - def forceunlock(self,RESPONSE=None): + def forceunlock(self,RESPONSE=None,user=None): """unlock""" #safe who had the lock + logging.debug("extVersionFile: (forceunlock)"+str(user)) if self.lockedBy: - self.brokenLock=str(self.lockedBy) + if user is not None: + if str(self.lockedBy)==user: + self.brokenLock=str(self.lockedBy) + self.lockedBy='' + else: + self.brokenLock="" + else: + self.brokenLock=str(self.lockedBy) + self.lockedBy='' else: self.brokenLock="" - self.lockedBy='' + return self.brokenLock security.declareProtected('AUTHENTICATED_USER','unlock') @@ -1024,12 +1139,13 @@ class extVersionedFile(CatalogAware,Fold return "Sorry, not locked by you! (%s,%s)"%(self.lockedBy,self.REQUEST['AUTHENTICATED_USER']) + def _newContentObject(self, id, title='', versionNumber=0, versionComment=None, time=None, author=None): """factory for content objects. to be overridden in derived classes.""" 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""" @@ -1078,9 +1194,10 @@ class extVersionedFile(CatalogAware,Fold 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()) + if index and self.default_catalog: + logging.debug("reindex1: %s in %s"%(repr(self),repr(self.default_catalog))) + self.reindex_object() return fob @@ -1123,17 +1240,18 @@ 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): + def manage_changeVersionedFile(self,title,vC,author,comment,defaultAction='history',RESPONSE=None): """Change VersionedFile metadata""" self.title = title self.author = author + self.defaultAction = defaultAction cob = self.getContentObject() if cob: if vC: @@ -1150,10 +1268,10 @@ class extVersionedFile(CatalogAware,Fold def download(self): """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()) + + txt=self.REQUEST['URL1']+'/'+self.getId()+'/'+self.getContentObject().getId()+'/download' + + self.REQUEST.RESPONSE.redirect(txt) security.declareProtected('AUTHENTICATED_USER','downloadLocked') @@ -1195,9 +1313,9 @@ def manage_addextVersionedFileForm(self) pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','addextVersionedFile.zpt')).__of__(self) return pt() -def manage_addextVersionedFile(self,id,title,lockedBy, author=None, RESPONSE=None): +def manage_addextVersionedFile(self,id,title,lockedBy, author=None, defaultAction='history', RESPONSE=None): """add the OSAS_root""" - newObj=extVersionedFile(id,title,lockedBy,author) + newObj=extVersionedFile(id,title,lockedBy,author,defaultAction=defaultAction) self._setObject(id,newObj) if RESPONSE is not None: