File:  [Repository] / versionedFile / versionedFile.py
Revision 1.12: download - view: text, annotated - select for diffs - revision graph
Mon Jul 26 17:01:29 2004 UTC (19 years, 10 months ago) by dwinter
Branches: MAIN
CVS tags: HEAD
sorting added

    1: from OFS.Folder import Folder
    2: from OFS.Image import File
    3: from OFS.Image import cookId
    4: from Globals import DTMLFile, InitializeClass,package_home
    5: from Products.PageTemplates.PageTemplateFile import PageTemplateFile
    6: from AccessControl import getSecurityManager
    7: from Products.PageTemplates.PageTemplate import PageTemplate
    8: from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
    9: from AccessControl import ClassSecurityInfo
   10: import os.path
   11: 
   12: 
   13: def sortv(x,y):
   14:     return cmp(x[0],y[0])
   15:  
   16: class versionedFileFolder(Folder):
   17:     """Folder with versioned files"""
   18: 
   19:     meta_type = "versionedFileFolder"
   20: 
   21:     security= ClassSecurityInfo()
   22:     security.declareProtected('AUTHENTICATED_USER','addFileForm')
   23:     
   24:     manage_options = Folder.manage_options+(
   25: 		{'label':'Generate Index.html','action':'generateIndexHTML'},
   26:                 {'label':'Generate history_template.html','action':'generateHistoryHTML'},
   27: 		)
   28: 
   29:     def generateIndexHTML(self,RESPONSE=None):
   30:         """lege standard index.html an"""
   31: 
   32:         
   33: 
   34:         
   35:         if not hasattr(self,'index.html'):
   36:             zt=ZopePageTemplate('index.html')
   37:             self._setObject('index.html',zt)
   38:             default_content_fn = os.path.join(package_home(globals()),
   39:                                                'zpt/versionFileFolderMain.zpt')
   40:             text = open(default_content_fn).read()
   41:             zt.pt_edit(text, 'text/html')
   42: 
   43:         else:
   44:             return "already exists!"
   45:         
   46:         if RESPONSE is not None:
   47:             RESPONSE.redirect('manage_main')
   48: 
   49: 
   50:     def generateHistoryHTML(self,RESPONSE=None):
   51:         """lege standard index.html an"""
   52: 
   53:         
   54: 
   55:         
   56:         if not hasattr(self,'history_template.html'):
   57:             zt=ZopePageTemplate('history_template.html')
   58:             self._setObject('history_template.html',zt)
   59:             default_content_fn = os.path.join(package_home(globals()),
   60:                                                'zpt/versionHistory.zpt')
   61:             text = open(default_content_fn).read()
   62:             zt.pt_edit(text, 'text/html')
   63: 
   64:         else:
   65:             return "already exists!"
   66:         
   67:         if RESPONSE is not None:
   68:             RESPONSE.redirect('manage_main')
   69: 
   70:     def getVersionedFiles(self,sortField='title'):
   71:         """get all versioned files"""
   72: 
   73:         def sortName(x,y):
   74:             return cmp(x[1].title,y[1].title)
   75: 
   76:         def sortDate(x,y):
   77:             return cmp(x[1].getLastVersion().bobobase_modification_time,y[1].getLastVersion().bobobase_modification_time)
   78: 
   79:         def sortAuthor(x,y):
   80:             return cmp(y[1].getLastVersion().lastEditor,x[1].getLastVersion().lastEditor)
   81:         
   82: 	versionedFiles=self.ZopeFind(self,obj_metatypes=['versionedFile'])
   83: 
   84:         if sortField=='title':
   85:             versionedFiles.sort(sortName)
   86:         elif sortField=='date':
   87:             versionedFiles.sort(sortDate)
   88:         elif sortField=='author':
   89:             versionedFiles.sort(sortAuthor)
   90: 
   91:         return versionedFiles
   92: 
   93: 
   94:     def header_html(self):
   95:         """zusätzlicher header"""
   96:         ext=self.ZopeFind(self,obj_ids=["header.html"])
   97:         if ext:
   98:             return ext[0][1]()
   99:         else:
  100:             return ""
  101:         
  102:     def index_html(self):
  103:         """main"""
  104:         ext=self.ZopeFind(self,obj_ids=["index.html"])
  105:         if ext:
  106:             return ext[0][1]()
  107:         
  108:         pt=PageTemplateFile('Products/versionedFile/zpt/versionFileFolderMain').__of__(self)
  109:         return pt()
  110: 
  111: 
  112:     def addFileForm(self):
  113:         """add a file"""
  114:         out=DTMLFile('dtml/newFileAdd', globals(),Kind='VersionedFileObject',kind='versionedFileObject',version='1').__of__(self)
  115:         return out()
  116: 
  117: 
  118:     def addFile(self,vC,file,author,newName='',content_type='',RESPONSE=None):
  119:         """ add a new file"""
  120:         if newName=='':
  121:             id=file.filename
  122:         else:
  123:             id=newName
  124:         
  125:         vC=self.REQUEST.form['vC']
  126:         manage_addVersionedFile(self,id,'','')
  127:         ob=self._getOb(id)
  128:         ob.title=id
  129:         file2=file
  130:         ob.manage_addVersionedFileObject(id,vC,author,file2,content_type=content_type)
  131: 
  132:         RESPONSE.redirect(self.REQUEST['URL1'])
  133: 
  134:         
  135: manage_addVersionedFileFolderForm=DTMLFile('dtml/folderAdd', globals())
  136: 
  137: 
  138: def manage_addVersionedFileFolder(self, id, title='',
  139:                      createPublic=0,
  140:                      createUserF=0,
  141:                      REQUEST=None):
  142:     """Add a new Folder object with id *id*.
  143: 
  144:     If the 'createPublic' and 'createUserF' parameters are set to any true
  145:     value, an 'index_html' and a 'UserFolder' objects are created respectively
  146:     in the new folder.
  147:     """
  148:     ob=versionedFileFolder()
  149:     ob.id=str(id)
  150:     ob.title=title
  151:     self._setObject(id, ob)
  152:     ob=self._getOb(id)
  153: 
  154:     checkPermission=getSecurityManager().checkPermission
  155: 
  156:     if createUserF:
  157:         if not checkPermission('Add User Folders', ob):
  158:             raise Unauthorized, (
  159:                   'You are not authorized to add User Folders.'
  160:                   )
  161:         ob.manage_addUserFolder()
  162: 
  163:   
  164:     if REQUEST is not None:
  165:         return self.manage_main(self, REQUEST, update_menu=1)
  166: 
  167: 
  168: 
  169: class versionedFileObject(File):
  170:     """File Object im Folder"""
  171:     
  172:     meta_type = "versionedFileObject"
  173:     
  174:     manage_editForm  =DTMLFile('dtml/fileEdit',globals(),
  175:                                Kind='File',kind='file')
  176:     manage_editForm._setName('manage_editForm')
  177: 
  178:     def download(self):
  179:         """download and lock"""
  180:         
  181:         
  182:         self.content_type="application/octet-stream"
  183:         self.REQUEST.RESPONSE.redirect(self.absolute_url())
  184:     
  185:     def downloadLocked(self):
  186:         """download and lock"""
  187:         
  188:         
  189:         if self.REQUEST['AUTHENTICATED_USER']=='Anonymous User':
  190:             return "please login first"
  191:         if not self.aq_parent.lockedBy=="":
  192:             return "cannot be locked because is already locked by %s"%self.lockedBy
  193:         self.aq_parent.lockedBy=self.REQUEST['AUTHENTICATED_USER']
  194: 
  195:         self.content_type="application/octet-stream"
  196:         self.REQUEST.RESPONSE.redirect(self.absolute_url())
  197:     
  198:     def setVersionNumber(self,versionNumber):
  199:         """set version"""
  200:         self.versionNumber=versionNumber
  201: 
  202:     def getVersionNumber(self):
  203:         """get version"""
  204:         return self.versionNumber
  205: 
  206:     def lastEditor(self):
  207:         """last Editor"""
  208:         if hasattr(self,'author'):
  209:             return self.author            
  210:         else:
  211:             jar=self._p_jar
  212:             oid=self._p_oid
  213: 
  214:             if jar is None or oid is None: return None
  215: 
  216:             return jar.db().history(oid)[0]['user_name']
  217: 
  218:     
  219:     
  220:         
  221: manage_addVersionedFileObjectForm=DTMLFile('dtml/fileAdd', globals(),Kind='VersionedFileObject',kind='versionedFileObject', version='1')
  222: 
  223: def manage_addVersionedFileObject(self,id,vC='',author='', file='',title='',precondition='', content_type='',
  224:                    REQUEST=None):
  225:     """Add a new File object.
  226: 
  227:     Creates a new File object 'id' with the contents of 'file'"""
  228: 
  229:     id=str(id)
  230:     title=str(title)
  231:     content_type=str(content_type)
  232:     precondition=str(precondition)
  233:     
  234:     id, title = cookId(id, title, file)
  235: 
  236:     self=self.this()
  237: 
  238:     # First, we create the file without data:
  239:     self._setObject(id, versionedFileObject(id,title,'',content_type, precondition))
  240:     self._getOb(id).versionComment=str(vC)
  241:     setattr(self._getOb(id),'author',author)
  242:     
  243:     # Now we "upload" the data.  By doing this in two steps, we
  244:     # can use a database trick to make the upload more efficient.
  245:     if file:
  246:         self._getOb(id).manage_upload(file)
  247:     if content_type:
  248:         self._getOb(id).content_type=content_type
  249: 
  250:     if REQUEST is not None:
  251:         REQUEST['RESPONSE'].redirect(self.absolute_url()+'/manage_main')
  252: 
  253: 
  254: 
  255: 
  256: class versionedFile(Folder):
  257:     """Versioniertes File"""
  258: 
  259:     def __init__(self, id, title, lockedBy,author):
  260:         """init"""
  261:         self.id=id
  262:         self.title=title
  263:         self.lockedBy=lockedBy
  264:         self.author=author
  265:         
  266:     meta_type="versionedFile"
  267: 
  268:     def getLastVersion(self):
  269:         """Last Version"""
  270:         tmp=0
  271:         lastVersion=None
  272:         
  273:         for version in self.ZopeFind(self):
  274:             
  275:             if hasattr(version[1],'versionNumber'):
  276:                 
  277:                 if int(version[1].versionNumber) > tmp:
  278:                     tmp=int(version[1].versionNumber,)
  279:                     lastVersion=version[1]
  280:         return lastVersion
  281:     
  282:     def index_html(self):
  283:         """main view"""
  284:         lastVersion=self.getLastVersion()
  285:         #return "File:"+self.title+"  Version:%i"%lastVersion.versionNumber," modified:",lastVersion.bobobase_modification_time()," size:",lastVersion.getSize(),"modified by:",lastVersion.lastEditor()
  286:         return "File: %s Version:%i modified:%s size:%s modified by:%s"%(self.title,lastVersion.versionNumber,lastVersion.bobobase_modification_time(),lastVersion.getSize(),lastVersion.lastEditor())
  287:                                                                          
  288:     def getVersion(self):
  289:         tmp=0
  290:         for version in self.ZopeFind(self):
  291:             
  292:             if hasattr(version[1],'versionNumber'):
  293:                 
  294:                 if int(version[1].versionNumber) > tmp:
  295:                     tmp=int(version[1].versionNumber,)
  296:         return tmp+1
  297: 
  298:     security= ClassSecurityInfo()
  299:     security.declareProtected('AUTHENTICATED_USER','unlock')
  300: 
  301:     def history(self):
  302:         """history"""  
  303: 
  304:         ext=self.ZopeFind(self.aq_parent,obj_ids=["history_template.html"])
  305:         if ext:
  306:             return getattr(self,ext[0][1].getId())()
  307:         
  308:         pt=PageTemplateFile('Products/versionedFile/zpt/versionHistory').__of__(self)
  309:         return pt()
  310: 
  311:     def getVersions(self):
  312:         """get all versions"""
  313:         ret=[]
  314:         for version in self.ZopeFind(self):
  315:             if hasattr(version[1],'versionNumber'):
  316:                 ret.append((version[1].versionNumber,version[1]))
  317:         ret.sort(sortv)
  318:         return ret
  319: 
  320:     security.declareProtected('AUTHENTICATED_USER','unlock')   
  321:     def unlock(self,RESPONSE):
  322:         """unlock"""
  323:         if str(self.lockedBy) in [str(self.REQUEST['AUTHENTICATED_USER'])]:
  324:             self.lockedBy=''
  325:             RESPONSE.redirect(self.REQUEST['URL2'])
  326:         else:
  327:             return "Sorry, not locked by you! (%s,%s)"%(self.lockedBy,self.REQUEST['AUTHENTICATED_USER'])
  328:         
  329:     
  330:     security.declareProtected('AUTHENTICATED_USER','addVersionedFileObjectForm')
  331: 
  332:     def addVersionedFileObjectForm(self):
  333:         """add a new version"""
  334:         
  335:         if str(self.REQUEST['AUTHENTICATED_USER']) in ["Anonymous User"]:
  336:             return "please login first"
  337:         if (self.lockedBy==self.REQUEST['AUTHENTICATED_USER']) or (self.lockedBy==''):
  338:             out=DTMLFile('dtml/fileAdd', globals(),Kind='VersionedFileObject',kind='versionedFileObject',version=self.getVersion()).__of__(self)
  339:             return out()
  340:         else:
  341:             return "Sorry file is locked by somebody else"
  342:         
  343:     def manage_addVersionedFileObject(self,id,vC,author,file='',title='',precondition='', content_type='',changeName='no',newName='', RESPONSE=None):
  344:         """add"""
  345:         
  346:         vC=self.REQUEST['vC']
  347:         author=self.REQUEST['author']
  348:         
  349:         if changeName=="yes":
  350:             self.title=file.filename[0:]
  351: 
  352:         if not newName=='':
  353:             self.title=newName[0:]
  354: 
  355:         id="V%i"%self.getVersion()+"_"+self.title
  356:         manage_addVersionedFileObject(self,id,vC,author,file,"V%i"%self.getVersion()+"_"+self.title,precondition, content_type)
  357:         objs=self.ZopeFind(self,obj_ids=[id])[0][1].setVersionNumber(int(self.getVersion()))
  358: 
  359:         if RESPONSE:
  360:             RESPONSE.redirect(self.REQUEST['URL2'])
  361: 
  362:     security.declareProtected('AUTHENTICATED_USER','downloadLocked')
  363: 
  364:     def download(self):
  365:         """download and lock"""
  366:         self.getLastVersion().content_type="application/octet-stream"
  367:         self.REQUEST.RESPONSE.redirect(self.REQUEST['URL1']+'/'+self.getId()+'/'+self.getLastVersion().getId())
  368:     
  369:     def downloadLocked(self):
  370:         """download and lock"""
  371:         if self.REQUEST['AUTHENTICATED_USER']=='Anonymous User':
  372:             return "please login first"
  373:         if not self.lockedBy=="":
  374:             return "cannot be locked because is already locked by %s"%self.lockedBy
  375:         self.lockedBy=self.REQUEST['AUTHENTICATED_USER']
  376:         self.getLastVersion().content_type="application/octet-stream"
  377:         self.REQUEST.RESPONSE.redirect(self.REQUEST['URL1']+'/'+self.getId()+'/'+self.getLastVersion().getId())
  378:     
  379: def manage_addVersionedFileForm(self):
  380:     """interface for adding the OSAS_root"""
  381:     pt=PageTemplateFile('Products/versionedFile/zpt/addVersionedFile.zpt').__of__(self)
  382:     return pt()
  383: 
  384: def manage_addVersionedFile(self,id,title,lockedBy, author=None, RESPONSE=None):
  385:     """add the OSAS_root"""
  386:     newObj=versionedFile(id,title,lockedBy,author)
  387:     self._setObject(id,newObj)
  388:    
  389:     if RESPONSE is not None:
  390:         RESPONSE.redirect('manage_main')
  391: 
  392: 
  393: InitializeClass(versionedFile)
  394: InitializeClass(versionedFileFolder)

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>