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

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

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