Annotation of cdli/devel/cdli_files.py, revision 1.1

1.1     ! dwinter     1: """CDLI extensions of the filearchive"""    
        !             2: from Products.versionedFile.extVersionedFile import *
        !             3: from Products.ZCatalog.CatalogPathAwareness import CatalogAware
        !             4: import os.path
        !             5: import os
        !             6: import urlparse
        !             7: import urllib
        !             8: import cgi
        !             9: from OFS.OrderedFolder import OrderedFolder
        !            10: from OFS.SimpleItem import SimpleItem
        !            11: import time
        !            12: from OFS.Folder import manage_addFolder
        !            13: import re
        !            14: from AccessControl import ClassSecurityInfo
        !            15: from Acquisition import Implicit
        !            16: from Globals import Persistent
        !            17: from threading import Thread
        !            18: from ZPublisher.HTTPRequest import HTTPRequest
        !            19: from ZPublisher.HTTPResponse import HTTPResponse
        !            20: from ZPublisher.BaseRequest import RequestContainer
        !            21: import threading
        !            22: import logging
        !            23: import transaction
        !            24: import copy
        !            25: import codecs
        !            26: import sys
        !            27: from BTrees.IOBTree import IOBTree 
        !            28: from BTrees.OOBTree import OOBTree 
        !            29: import cdliSplitter
        !            30: from sets import Set
        !            31: import md5
        !            32: from DownloadBasket import DownloadBasketFinallyThread
        !            33: from types import *
        !            34: import pickle
        !            35: import tempfile
        !            36:                       
        !            37: from cdli_helpers import *
        !            38: 
        !            39: class CDLIFileObject(CatalogAware,extVersionedFileObject):
        !            40:     """CDLI file object"""
        !            41:     
        !            42:     meta_type="CDLI File Object"
        !            43:     default_catalog='CDLIObjectsCatalog'
        !            44:     
        !            45:     security=ClassSecurityInfo()
        !            46:     
        !            47:     security.declareProtected('manage','index_html')
        !            48: 
        !            49:     security.declarePublic('view')
        !            50:     view = PageTemplateFile('zpt/viewCDLIFile.zpt', globals())
        !            51: 
        !            52:     security.declarePublic('editATF')
        !            53:     editATF = PageTemplateFile('zpt/editATFFile.zpt', globals())
        !            54: 
        !            55:     def PrincipiaSearchSource(self):
        !            56:            """Return cataloguable key for ourselves."""
        !            57:            return str(self)
        !            58:        
        !            59:     def setAuthor(self, author):
        !            60:         """change the author"""
        !            61:         self.author = author
        !            62:        
        !            63:     def makeThisVersionCurrent_html(self):
        !            64:         """form for mthis version current"""
        !            65:         
        !            66:         pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','makeThisVersionCurrent.zpt')).__of__(self)
        !            67:         return pt()                 
        !            68: 
        !            69:     security.declarePublic('makeThisVersionCurrent')
        !            70:     def makeThisVersionCurrent(self,comment,author,RESPONSE=None):
        !            71:         """copy this version to current"""
        !            72:         parent=self.aq_parent
        !            73:         parent.manage_addVersionedFileObject(id=None,vC=comment,author=author,file=self.getData(),RESPONSE=RESPONSE)
        !            74:         #newversion=parent.manage_addCDLIFileObject('',comment,author)
        !            75:         #newversion.manage_upload(self.getData())
        !            76:                                         
        !            77:         #if RESPONSE is not None:
        !            78:         #    RESPONSE.redirect(self.aq_parent.absolute_url()+'/history')
        !            79: 
        !            80:         return True
        !            81:     
        !            82:     def getFormattedData(self):
        !            83:         """fromat text"""
        !            84:         data=self.getData()
        !            85: #        return re.sub("\s\#lem"," #lem",data) #remove return vor #lem
        !            86:         return re.sub("#lem","       #lem",data) #remove return vor #lem
        !            87:         
        !            88:     
        !            89:     security.declarePublic('getPNumber')
        !            90:     def getPNumber(self):
        !            91:         """get the pnumber"""
        !            92:         try:
        !            93:                 txt=re.match("&[PpSs](\d*)\s*=([^\r\n]*)",self.getData()[0:])
        !            94:         except:
        !            95:                 txt=self.getData()[0:]
        !            96:                 
        !            97:                 return "ERROR"
        !            98:         try:
        !            99:             return "P"+txt.group(1)
        !           100:         except:
        !           101:             return "ERROR"
        !           102: 
        !           103:     security.declarePublic('getDesignation')
        !           104:     def getDesignation(self):
        !           105:         """get the designation out of the file"""
        !           106:         try:
        !           107:                 txt=re.match("&[PpSs](\d*)\s*=([^\r\n]*)",self.getData()[0:])
        !           108:         except:
        !           109:                 txt=self.getData()[0:]
        !           110:                 
        !           111:                 return "ERROR"
        !           112:         try:
        !           113:             return txt.group(2)
        !           114:         except:
        !           115:             return "ERROR"
        !           116: 
        !           117:         
        !           118: manage_addCDLIFileObjectForm=DTMLFile('dtml/fileAdd', globals(),Kind='CDLIFileObject',kind='CDLIFileObject', version='1')
        !           119: 
        !           120: def manage_addCDLIFileObject(self,id,vC='',author='', file='',title='',versionNumber=0,
        !           121:                              precondition='', content_type='',
        !           122:                              from_tmp=False,REQUEST=None):
        !           123:     """Add a new File object.
        !           124:     Creates a new File object 'id' with the contents of 'file'"""
        !           125:  
        !           126:     id=str(id)
        !           127:     title=str(title)
        !           128:     content_type=str(content_type)
        !           129:     precondition=str(precondition)
        !           130:     
        !           131:     id, title = cookId(id, title, file)
        !           132: 
        !           133:     self=self.this()
        !           134: 
        !           135:     # First, we create the file without data:
        !           136:     self._setObject(id, CDLIFileObject(id,title,versionNumber=versionNumber,versionComment=vC,time=time.localtime(),author=author))
        !           137:     fob = self._getOb(id)
        !           138:     
        !           139:     # Now we "upload" the data.  By doing this in two steps, we
        !           140:     # can use a database trick to make the upload more efficient.
        !           141: 
        !           142:     if file and not from_tmp:
        !           143:         fob.manage_upload(file)
        !           144:     elif file and from_tmp:
        !           145:         fob.manage_file_upload(file) # manage_upload_from_tmp doesn't exist in ExtFile2
        !           146:     #    fob.manage_upload_from_tmp(file) # manage_upload_from_tmp doesn't exist in ExtFile2
        !           147:     if content_type:
        !           148:         fob.content_type=content_type
        !           149: 
        !           150:     #logging.debug("manage_add: lastversion=%s"%self.getData())
        !           151:     logging.debug("reindex1: %s in %s"%(repr(self),repr(self.default_catalog)))
        !           152:     self.reindex_object()
        !           153:     #logging.debug("manage_add: fob_data=%s"%fob.getData())
        !           154:     logging.debug("reindex2: %s in %s"%(repr(fob), repr(fob.default_catalog)))
        !           155:     fob.index_object()
        !           156: 
        !           157:     self.CDLIRoot.updateOrAddToFileBTree(ob)
        !           158:     if REQUEST is not None:
        !           159:         REQUEST['RESPONSE'].redirect(self.absolute_url()+'/manage_main')
        !           160:     
        !           161: 
        !           162: class CDLIFile(extVersionedFile,CatalogAware):
        !           163:     """CDLI file"""
        !           164:     
        !           165:     security=ClassSecurityInfo()
        !           166:     meta_type="CDLI file"
        !           167:     content_meta_type = ["CDLI File Object"]
        !           168:     
        !           169:     default_catalog='CDLICatalog'
        !           170:     
        !           171:     security.declareProtected('manage','index_html')
        !           172:     
        !           173:     def getLastVersionData(self):
        !           174:         """get last version data"""
        !           175:         return self.getData()
        !           176: 
        !           177:     def getLastVersionFormattedData(self):
        !           178:         """get last version data"""
        !           179:         return self.getContentObject().getFormattedData()
        !           180: 
        !           181:     def getTextId(self):
        !           182:         """returns P-number of text"""
        !           183:         # assuming that its the beginning of the title
        !           184:         return self.title[:7]
        !           185: 
        !           186:     #security.declarePublic('history')
        !           187:     def history(self):
        !           188:         """history"""  
        !           189: 
        !           190:         ext=self.ZopeFind(self.aq_parent,obj_ids=["history_template.html"])
        !           191:         if ext:
        !           192:             return getattr(self,ext[0][1].getId())()
        !           193:         
        !           194:         pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','versionHistory')).__of__(self)
        !           195:         return pt()
        !           196: 
        !           197: 
        !           198:     def getBasketFromId(self,basketid, context=None):
        !           199:         """get basket from id"""
        !           200: 
        !           201:         if not context:
        !           202:             context=self
        !           203:             
        !           204:         for basket in self.ZopeFind(context,obj_metatypes=["CDLIBasket"]):
        !           205:             if basket[0]==basketid:
        !           206:                 return basket[1]
        !           207:         else:
        !           208:             None
        !           209: 
        !           210:  
        !           211:     def isContainedInBaskets(self,context=None):
        !           212:         """check is this file is part of any basket
        !           213:         @param context: (optional) necessessary if CDLIBasketCatalog is not an (inherited) attribute of self, context.CDLIBasketCatalog
        !           214:                         has to exist.
        !           215:         """
        !           216: 
        !           217:         if not context:
        !           218:             context=self
        !           219:         
        !           220:         ret=[]
        !           221:         for x in context.CDLIBasketCatalog.search({'getFileNamesInLastVersion':self.getId()}):
        !           222:             #if the basket x is deleted it seemes to be that x is sometimes still in the Catalog, why?
        !           223:             try:
        !           224:                 ret.append(x.getObject())
        !           225:             except:
        !           226:                 pass
        !           227:         return ret
        !           228:         #return [x.getObject() for x in context.CDLIBasketCatalog.search({'getFileNamesInLastVersion':self.getId()})]
        !           229:         
        !           230:         
        !           231:     def _newContentObject(self, id, title='', versionNumber=0, versionComment=None, time=None, author=None):
        !           232:         """factory for content objects. to be overridden in derived classes."""
        !           233:         logging.debug("_newContentObject(CDLI)")
        !           234:         return CDLIFileObject(id,title,versionNumber=versionNumber,versionComment=versionComment,time=time,author=author)
        !           235: 
        !           236: 
        !           237:     def addCDLIFileObjectForm(self):
        !           238:         """add a new version"""
        !           239:         
        !           240:         if str(self.REQUEST['AUTHENTICATED_USER']) in ["Anonymous User"]:
        !           241:             return "please login first"
        !           242:         if (self.lockedBy==self.REQUEST['AUTHENTICATED_USER']) or (self.lockedBy==''):
        !           243:             out=DTMLFile('dtml/fileAdd', globals(),Kind='CDLIFileObject',kind='CDLIFileObject',version=self.getVersion()).__of__(self)
        !           244:             return out()
        !           245:         else:
        !           246:             return "Sorry file is locked by somebody else"
        !           247:         
        !           248:     def manage_addCDLIFileObject(self,id,vC,author,
        !           249:                                  file='',title='',
        !           250:                                  precondition='', 
        !           251:                                  content_type='',
        !           252:                                  changeName='no',newName='', 
        !           253:                                  come_from=None,
        !           254:                                  from_tmp=False,RESPONSE=None):
        !           255:         """add"""
        !           256:       
        !           257:         try: #TODO: der ganze vC unsinn muss ueberarbeitet werden
        !           258:             vC=self.REQUEST['vC']
        !           259:         except:
        !           260:             pass
        !           261:         
        !           262:         ob = self.addContentObject(id, vC, author, file, title, changeName=changeName, newName=newName, from_tmp=from_tmp,
        !           263:                                    precondition=precondition, content_type=content_type)
        !           264: 
        !           265:         try:
        !           266:             #FIXME: wozu ist das gut?
        !           267:             self.REQUEST.SESSION['objID_parent']=self.getId()
        !           268:         except:
        !           269:             pass
        !           270:   
        !           271:         #self.cdliRoot.updateOrAddToFileBTree(self)# now update the object in the cache
        !           272:       
        !           273:         
        !           274:         if RESPONSE:
        !           275:             if ob.getSize()==0:
        !           276:                 self.REQUEST.SESSION['objID']=ob.getId()
        !           277:                 pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','errorUploadFile')).__of__(self)
        !           278:                 return pt()
        !           279:             else:
        !           280:                 if come_from and (come_from!=""):
        !           281:                     RESPONSE.redirect(come_from+"?change="+self.getId())
        !           282:                 else:
        !           283:                     RESPONSE.redirect(self.REQUEST['URL2']+'?uploaded=%s'%self.title)
        !           284:         else:
        !           285:             return ob
        !           286:         
        !           287:         
        !           288: def manage_addCDLIFileForm(self):
        !           289:     """interface for adding the OSAS_root"""
        !           290:     pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','addCDLIFile.zpt')).__of__(self)
        !           291:     return pt()
        !           292: 
        !           293: def manage_addCDLIFile(self,id,title,lockedBy, author=None, RESPONSE=None):
        !           294:     """add the OSAS_root"""
        !           295:     newObj=CDLIFile(id,title,lockedBy,author)
        !           296:                                         
        !           297:     tryToggle=True
        !           298:     tryCount=0
        !           299: 
        !           300:     self._setObject(id,newObj)                  
        !           301:     getattr(self,id).reindex_object()
        !           302:         
        !           303:     if RESPONSE is not None:
        !           304:         RESPONSE.redirect('manage_main')
        !           305: 
        !           306: 
        !           307: def checkUTF8(data):
        !           308:     """check utf 8"""
        !           309:     if not isinstance(data, str):
        !           310:         logging.error("checkUTF8 data is not string! (%s)"%repr(data))
        !           311: 
        !           312:     try:
        !           313:         data.decode('utf-8')
        !           314:         logging.debug("checkUTF8: ok!")
        !           315:         return True
        !           316:     except:
        !           317:         logging.debug("checkUTF8: false!")
        !           318:         return False
        !           319:     
        !           320: 
        !           321: def checkFile(filename,data,folder):
        !           322:     """check the files"""
        !           323:     # first check the file name
        !           324:     fn=filename.split(".") # no extension
        !           325: 
        !           326:     if not (fn[0][0]=="P" or fn[0][0]=="S"):
        !           327:         return False,"P/S missing in the filename"
        !           328:     elif len(fn[0])!=7:
        !           329:         return False,"P number has not the right length 6"
        !           330:     elif not checkUTF8(data):
        !           331:         return False,"not utf-8"
        !           332:     else:
        !           333:         return True,""
        !           334:     
        !           335:     
        !           336: def splitatf(fh,dir=None,ext=None):
        !           337:     """split it"""
        !           338:     ret=None
        !           339:     nf=None
        !           340:     i=0
        !           341: 
        !           342:     #ROC: why split \n first and then \r???
        !           343:     if isinstance(fh, basestring):
        !           344:         iter=fh.split("\n")
        !           345:     else:
        !           346:         iter=fh.readlines()
        !           347:         
        !           348:     for lineTmp in iter:
        !           349:         lineTmp=lineTmp.replace(codecs.BOM_UTF8,'') # make sure that all BOM are removed..
        !           350:         for line in lineTmp.split("\r"):
        !           351:             #logging.info("Deal with: %s"%line)
        !           352:             if ext:
        !           353:                 i+=1
        !           354:                 if (i%100)==0:
        !           355:                     ext.result+="."
        !           356:                 if i==10000:
        !           357:                     i=0
        !           358:                     ext.result+="<br>"
        !           359:             #check if basket name is in the first line
        !           360:             if line.find("#atf basket")>=0: #old convention
        !           361:                 ret=line.replace('#atf basket ','')
        !           362:                 ret=ret.split('_')[0]
        !           363:             elif line.find("#basket:")>=0: #new convention
        !           364:                 ret=line.replace('#basket: ','')
        !           365:                 ret=ret.split('_')[0]
        !           366: 
        !           367:             else:
        !           368:                 if (len(line.lstrip())>0) and (line.lstrip()[0]=="&"): #newfile
        !           369:                     if nf:
        !           370:                         nf.close() #close last file
        !           371: 
        !           372: 
        !           373:                     filename=line[1:].split("=")[0].rstrip()+".atf"
        !           374:                     if dir:
        !           375:                         filename=os.path.join(dir,filename)
        !           376:                     nf=file(filename,"w")
        !           377:                     logging.debug("open %s"%filename)
        !           378:                 if nf:    
        !           379:                     nf.write(line.replace("\n","")+"\n")
        !           380: 
        !           381:     try:        
        !           382:         nf.close()
        !           383:     except:
        !           384:         pass
        !           385:     
        !           386:     if not isinstance(fh, basestring):
        !           387:         fh.close()
        !           388:         
        !           389:     return ret,len(os.listdir(dir))
        !           390: 
        !           391: 
        !           392: class CDLIFileFolder(extVersionedFileFolder):
        !           393:     """CDLI File Folder"""
        !           394:     
        !           395:     security=ClassSecurityInfo()
        !           396:     meta_type="CDLI Folder"
        !           397:     file_meta_type=['CDLI file']
        !           398:     folder_meta_type=['CDLI Folder']
        !           399: 
        !           400:     file_catalog='CDLICatalog'
        !           401: 
        !           402:     #downloadCounter=0 # counts how many download for all files currently run, be mehr als 5 wird verweigert.
        !           403:     tmpStore2={}
        !           404: 
        !           405:     def _newVersionedFile(self, id, title='', lockedBy=None, author=None):
        !           406:         """factory for versioned files. to be overridden in derived classes."""
        !           407:         logging.debug("_newVersionedFile(CDLI)")
        !           408:         return CDLIFile(id, title, lockedBy=lockedBy, author=author)
        !           409: 
        !           410:     def setTemp(self,name,value):
        !           411:         """set tmp"""
        !           412: 
        !           413:         setattr(self,name,value)
        !           414:                                         
        !           415:     deleteFileForm = PageTemplateFile("zpt/doDeleteFile", globals())
        !           416:                                        
        !           417:     def delete(self,ids,REQUEST=None):
        !           418:         """delete these files"""
        !           419:         if type(ids) is not ListType:
        !           420:             ids=[ids]
        !           421: 
        !           422:         self.manage_delObjects(ids)
        !           423:         
        !           424:         if REQUEST is not None:
        !           425:             return self.index_html()
        !           426: 
        !           427: 
        !           428:     def getVersionNumbersFromIds(self,ids):
        !           429:         """get the numbers of the current versions of documents described by their ids"""
        !           430:         
        !           431:         ret=[]
        !           432:         searchStr=" OR ".join(ids)
        !           433:         
        !           434:         founds=self.CDLICatalog.search({'title':searchStr})
        !           435:         
        !           436:         for found in founds:
        !           437:             lastVersion=found.getObject().getContentObject()
        !           438:             ret.append((found.getId,lastVersion))
        !           439:         
        !           440:         return ret
        !           441:     
        !           442:     def getFile(self,fn):
        !           443:         """get the content of the file fn"""
        !           444:         logging.debug("getFile: %s"%repr(fn))
        !           445:         if not self.hasObject(fn):
        !           446:             # search deeper
        !           447:             founds=getattr(self, self.file_catalog).search({'textid':fn})
        !           448:             if founds:
        !           449:                 obj=founds[0].getObject().getContentObject()
        !           450:             else:
        !           451:                 return "" 
        !           452:         else:
        !           453:             obj = self[fn].getContentObject()
        !           454: 
        !           455:         return obj.getData()[0:] 
        !           456:  
        !           457:     
        !           458:     def checkCatalog(self,fn):
        !           459:         """check if fn is in the catalog"""
        !           460:         #TODO add checkCatalog
        !           461:         
        !           462:                                    
        !           463:     def findObjectsFromListWithVersion(self,list,author=None):
        !           464:         """find objects from a list with versions
        !           465:         @param list: list of tuples  (cdliFile,version)
        !           466:         """
        !           467:         #self.REQUEST.SESSION['fileIds']=list#store fieldIds in session for further usage
        !           468:         #self.REQUEST.SESSION['searchList']=self.REQUEST.SESSION['fileIds']
        !           469:         
        !           470:         pt=getattr(self,'filelistVersioned.html')
        !           471:             
        !           472:         return pt(search=list,author=author)
        !           473:     
        !           474:     
        !           475:     def getAllPNumbers(self):
        !           476:         """get a list of all files (resp their p-numbers) stored"""
        !           477:         
        !           478:         ret=[x.getId for x in  self.CDLICatalog()]
        !           479:      
        !           480:         return ret
        !           481:     
        !           482:     def expandFile(self,fileId,fileTree):
        !           483:         """wildcard in fileID suche alle Treffer"""
        !           484:         founds=self.CDLICatalog({'title':fileId})
        !           485:         for found in founds:
        !           486:             fileTree.add(found.getId)
        !           487:             logging.debug("ADDD:"+found.getId)
        !           488:          
        !           489:     def findObjectsFromList(self,enterList=None,display=False,start=None,upload=None,list=None,basketName=None,numberOfObjects=None,RESPONSE=None,REQUEST=None,returnHash=False,hash=None):
        !           490:         """findObjectsFromList (, TAB oder LINE separated)"""
        !           491:                                        
        !           492:         logging.debug("start: findObjectsFromList")
        !           493:         #logging.debug("start: findObjectsFromList"+repr(list))
        !           494:         
        !           495:             
        !           496:         if upload: # list from file upload
        !           497:             txt=upload.read()
        !           498:                                        
        !           499:         if enterList:
        !           500:             txt=enterList
        !           501:             
        !           502:         if upload or enterList:
        !           503:             txt=txt.replace(",","\n")
        !           504:             txt=txt.replace("\t","\n")
        !           505:             txt=txt.replace("\r","\n")
        !           506:             idsTmp=txt.split("\n")
        !           507:             ids=[]
        !           508:             for id in idsTmp: # make sure that no empty lines
        !           509:                 idTmp=id.lstrip().rstrip()
        !           510:                 if len(idTmp)>0:
        !           511:                     
        !           512:                     ids.append(idTmp)
        !           513:                     
        !           514:             #self.REQUEST.SESSION['ids']=" OR ".join(ids)
        !           515: 
        !           516:             pt=getattr(self,'filelist.html')
        !           517:             self.REQUEST.SESSION['searchList']=ids
        !           518:             return pt(search=ids)
        !           519:         
        !           520:         if basketName:
        !           521:             #TODO: get rid of one of these..
        !           522:             
        !           523:             pt=getattr(self,'filelist.html')
        !           524:             return pt(basketName=basketName,numberOfObjects=numberOfObjects)
        !           525:         
        !           526:         
        !           527:         result =self.CDLICache.retrieve(hash)
        !           528:         if result:
        !           529:            logging.debug("give result from storage2")
        !           530:            return hash,result
        !           531:   
        !           532:         if list is not None: # got already a list
        !           533:             
        !           534:             logging.debug(" ----List version")
        !           535:             ret=[]
        !           536:             fileTree=Set()
        !           537:             
        !           538:             for fileId in list:
        !           539:                
        !           540:                 if fileId.find("*")>-1: #check for wildcards
        !           541:                         self.expandFile(fileId,fileTree)
        !           542:                         
        !           543:                 elif len(fileId.split("."))==1:
        !           544:                         fileId=fileId+".atf"
        !           545:                         fileTree.add(fileId)
        !           546:                 #logging.debug("   -----:"+fileId)
        !           547:                 #ret+=self.CDLICatalog({'title':fileId})
        !           548:                 #x =self.getFileObject(fileId)
        !           549:                 #if x is not None:
        !           550:                 #    ret.append(x)
        !           551:                 
        !           552:             
        !           553:             
        !           554:             ids = fileTree & self.v_file_ids
        !           555:             #self.REQUEST.SESSION['fileIds']=ids#store fieldIds in session for further usage
        !           556:             l=makelist(fileTree)[0:]
        !           557:             #logging.debug("l-list:"+repr(l))
        !           558:             self.REQUEST.SESSION['fileIds']=l#store fieldIds in session for further usage
        !           559:             self.REQUEST.SESSION['searchList']=l
        !           560:             #self.REQUEST.SESSION['searchList']=['P000001.atf']
        !           561:           
        !           562:             
        !           563:             hash = md5.new(repr(makelist(fileTree))).hexdigest() # erzeuge hash als identification
        !           564:             self.REQUEST.SESSION['hash']=hash
        !           565:             #TODO: do I need garbage collection for v_tmpStore ?
        !           566:             
        !           567:             #logging.debug("Hash:"+repr(hash))
        !           568: #        
        !           569: #            if hasattr(self.cdliRoot,'v_tmpStore') and self.cdliRoot.v_tmpStore.has_key(hash): 
        !           570: #               logging.debug("asking for storage")
        !           571: #               res=self.cdliRoot.v_tmpStore[hash]
        !           572: #               if res:
        !           573: #                   if returnHash == True:
        !           574: #                       return hash,res
        !           575: #                   return res
        !           576:           
        !           577:             #TODO: get rid of one of these..
        !           578:             #ids=[x.getObject().getId() for x in ret]
        !           579:             ret=[(self.getFileObject(x),self.getFileObjectLastVersion(x)) for x in ids]
        !           580:             
        !           581:             #self.REQUEST.SESSION['fileIds']=ids#store fieldIds in session for further usage
        !           582:             #self.REQUEST.SESSION['searchList']=self.REQUEST.SESSION['fileIds']
        !           583:            
        !           584:             if display:
        !           585:                 pt=getattr(self,'filelist.html')
        !           586:                 
        !           587:                 return pt(search=ids)
        !           588:             else:     
        !           589:                 #self.REQUEST.SESSION['hash'] = ret # store in session 
        !           590:                 
        !           591:                 #logging.debug("HHHHHHNEU:"+repr(self.makelist(ids)))
        !           592:                 #logging.debug("HHHHHHNEU:"+repr(hash))
        !           593:                 self.CDLICache.store(hash,ret)
        !           594:                 
        !           595:                 if returnHash == True:
        !           596:                     return hash,ret
        !           597:                 return ret
        !           598:         
        !           599:         
        !           600:         
        !           601:         if start:
        !           602:             RESPONSE.redirect("filelist.html?start:int="+str(start))
        !           603: 
        !           604:     security.declareProtected('Manage','createAllFilesAsSingleFile')
        !           605:     def createAllFilesAsSingleFile(self,RESPONSE=None):
        !           606:         """download all files"""
        !           607:         
        !           608:         def sortF(x,y):
        !           609:             return cmp(x[0],y[0])
        !           610:         
        !           611:         catalog=getattr(self,self.file_catalog)
        !           612:         #tf,tfilename=mkstemp()
        !           613:         if not hasattr(self.temp_folder,'downloadCounter'):
        !           614:             self.temp_folder.downloadCounter=0
        !           615: 
        !           616:         if getattr(self.temp_folder,'downloadCounter',0) > 5:
        !           617:             return """I am sorry, currently the server has to many requests for downloads, please come back later!"""
        !           618: 
        !           619:         self.temp_folder.downloadCounter+=1
        !           620:         self._p_changed=1
        !           621:         transaction.get().commit()
        !           622:        
        !           623:         list=[(x.getId,x) for x in catalog()]
        !           624:         list.sort(sortF)
        !           625:         
        !           626: 
        !           627:         
        !           628:         RESPONSE.setHeader("Content-Disposition","""attachement; filename=%s"""%"all.atf")
        !           629:         RESPONSE.setHeader("Content-Type","application/octet-stream")
        !           630:         tmp=""
        !           631:         for l in list:
        !           632:             obj=l[1].getObject()
        !           633:             
        !           634:             if obj.meta_type=="CDLI file":
        !           635:                 
        !           636:                 #os.write(tf,obj.getLastVersion().data)
        !           637:                 if RESPONSE:
        !           638:                     RESPONSE.write(obj.getData()[0:])
        !           639:                     RESPONSE.write("\n")
        !           640:                 self.temp_folder.downloadCounter-=1 
        !           641:                 self._p_changed=1
        !           642:         transaction.get().commit()
        !           643:         #os.close(tf)
        !           644:         #RESPONSE.redirect(self.absolute_url()+"/downloadFile?fn="%tfilename)
        !           645:         return True
        !           646:     
        !           647:     def downloadFile(self,fn):
        !           648:         """download fn - not used yet"""
        !           649:         self.REQUEST.RESPONSE.setHeader("Content-Disposition","""attachement; filename=%s"""%self.getLastVersion().getId())
        !           650:         self.REQUEST.RESPONSE.setHeader("Content-Type","application/octet-stream")
        !           651:         self.REQUEST.RESPONSE.write(file(fn).read())
        !           652:         
        !           653:       
        !           654:                 
        !           655:     def hasParent(self):
        !           656:         """returns true falls subfolder"""
        !           657:       
        !           658:         if self.aq_parent.meta_type in self.folder_meta_type:
        !           659:             return True
        !           660:         else:
        !           661:             return False
        !           662:         
        !           663:     def getFolders(self):
        !           664:         """get all subfolders"""
        !           665:         ret=[]
        !           666:         folders=self.ZopeFind(self,obj_metatypes=self.folder_meta_type)
        !           667:         for folder in folders:
        !           668:             ret.append((folder[1],
        !           669:                         len(self.ZopeFind(folder[1],obj_metatypes=self.folder_meta_type)),
        !           670:                         len(self.ZopeFind(folder[1],obj_metatypes=self.file_meta_type))
        !           671:                         ))
        !           672:         return ret
        !           673:     
        !           674:             
        !           675:     security.declareProtected('manage','index_html')
        !           676:     def index_html(self):
        !           677:         """main"""
        !           678:         ext=self.ZopeFind(self,obj_ids=["index.html"])
        !           679:         if ext:
        !           680:             return ext[0][1]()
        !           681:         
        !           682:         pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','CDLIFileFolderMain')).__of__(self)
        !           683:         return pt()
        !           684:     
        !           685:     
        !           686: manage_addCDLIFileFolderForm=DTMLFile('dtml/folderAdd', globals())
        !           687: 
        !           688:     
        !           689: def manage_addCDLIFileFolder(self, id, title='',
        !           690:                      createPublic=0,
        !           691:                      createUserF=0,
        !           692:                      REQUEST=None):
        !           693:     """Add a new Folder object with id *id*.
        !           694: 
        !           695:     If the 'createPublic' and 'createUserF' parameters are set to any true
        !           696:     value, an 'index_html' and a 'UserFolder' objects are created respectively
        !           697:     in the new folder.
        !           698:     """
        !           699:     ob=CDLIFileFolder()
        !           700:     ob.id=str(id)
        !           701:     ob.title=title
        !           702:     self._setObject(id, ob)
        !           703:     ob=self._getOb(id)
        !           704: 
        !           705:     checkPermission=getSecurityManager().checkPermission
        !           706: 
        !           707:     if createUserF:
        !           708:         if not checkPermission('Add User Folders', ob):
        !           709:             raise Unauthorized, (
        !           710:                   'You are not authorized to add User Folders.'
        !           711:                   )
        !           712:         ob.manage_addUserFolder()
        !           713: 
        !           714:   
        !           715:     if REQUEST is not None:
        !           716:         return self.manage_main(self, REQUEST, update_menu=1)
        !           717:     
        !           718: class CDLIRoot(Folder):
        !           719:     """main folder for cdli"""
        !           720:     
        !           721:     meta_type="CDLIRoot"
        !           722:     downloadCounterBaskets=0 # counts the current basket downloads if counter > 10 no downloads are possible
        !           723:     
        !           724:     file_catalog = 'CDLICatalog'
        !           725:     
        !           726:     # word splitter for search
        !           727:     splitter = {'words':cdliSplitter.wordSplitter(),
        !           728:                 'graphemes':cdliSplitter.graphemeSplitter()}
        !           729:     
        !           730:     def redirect(self,RESPONSE,url):
        !           731:         """mache ein redirect mit einem angehaengten time stamp um ein reload zu erzwingen"""
        !           732:         
        !           733:         timeStamp=time.time()
        !           734:         
        !           735:         if url.find("?")>-1: #giebt es schon parameter
        !           736:             addStr="&time=%s"
        !           737:         else:
        !           738:             addStr="?time=%s"
        !           739:             
        !           740:         RESPONSE.setHeader('Last-Modified',email.Utils.formatdate().split("-")[0]+'GMT')
        !           741:         logging.error(email.Utils.formatdate()+' GMT')
        !           742:         RESPONSE.redirect(url+addStr%timeStamp)
        !           743:     
        !           744:     def unicodify(self,txt):
        !           745:         return unicodify(txt)
        !           746:     def invalidateOldCacheVersion(self):
        !           747:         """loescht die alte Version des Cache"""
        !           748:         del self.v_tmpStore
        !           749:         return "done"
        !           750:     
        !           751:     def viewATF(self,id,RESPONSE):
        !           752:         """view an Object"""
        !           753:         ob = self.CDLICatalog({'title':id})
        !           754:         logging.debug(ob[0].getObject().getLastVersion().absolute_url()+"/view")
        !           755:         if len(ob)>0:
        !           756:             RESPONSE.redirect(ob[0].getObject().getLastVersion().absolute_url()+"/view")
        !           757:         return "not found"
        !           758:     
        !           759:     def history(self,id,RESPONSE):
        !           760:         """view an Object"""
        !           761:         ob = self.CDLICatalog({'title':id})
        !           762:         if len(ob)>0:
        !           763:             RESPONSE.redirect(ob[0].absolute_url+"/history")
        !           764:         return "not found"
        !           765:     
        !           766: 
        !           767:     def downloadLocked(self,id,RESPONSE):
        !           768:         """view an Object"""
        !           769:         ob = self.CDLICatalog({'title':id})
        !           770:         if len(ob)>0:
        !           771:             RESPONSE.redirect(ob[0].absolute_url+"/downloadLocked")
        !           772:         return "not found"
        !           773:     
        !           774:     def download(self,id,RESPONSE):
        !           775:         """view an Object"""
        !           776:         ob = self.CDLICatalog({'title':id})
        !           777:         if len(ob)>0:
        !           778:             logging.info("objekt:"+repr(ob[0]))
        !           779:             #RESPONSE.redirect(ob[0].getLastVersion().absolute_url())
        !           780:             RESPONSE.redirect(ob[0].absolute_url+"/download")
        !           781:         return "not found"
        !           782:     def addCDLIFileObjectForm(self,id,RESPONSE):
        !           783:         """view an Object"""
        !           784:         ob = self.CDLICatalog({'title':id})
        !           785:         if len(ob)>0:
        !           786:             RESPONSE.redirect(ob[0].absolute_url+"/addCDLIFileObjectForm")
        !           787:         return "not found"
        !           788:     
        !           789:     def addVersionedFileObjectForm(self,id,RESPONSE):
        !           790:         """view an Object"""
        !           791:         ob = self.CDLICatalog({'title':id})
        !           792:         if len(ob)>0:
        !           793:             RESPONSE.redirect(ob[0].absolute_url+"/addVersionedFileObjectForm")
        !           794:         return "not found"
        !           795:     
        !           796:     def unlock(self,id,RESPONSE):
        !           797:         """view an Object"""
        !           798:         ob = self.CDLICatalog({'title':id})
        !           799:         if len(ob)>0:
        !           800:             RESPONSE.redirect(ob[0].absolute_url+"/unlock")
        !           801:         return "not found"
        !           802:     
        !           803:     
        !           804:     def getFileObject(self,fileId):
        !           805:         """get an object"""
        !           806:         logging.debug("getFileObj:"+repr(fileId))
        !           807:         if isinstance(fileId,CDLIFileObject): # support for old baskets
        !           808:                  return fileId
        !           809:         x=self.v_files.get(fileId)
        !           810:         logging.debug("obj: "+repr(x))
        !           811:         if x is None:
        !           812:             logging.debug("fileId"+repr(fileId))
        !           813:             folder=fileId[0:3]
        !           814:             f2=fileId[0:5]
        !           815:             fObj = getattr(self.cdliRoot.cdli_main,folder);
        !           816:             f2Obj = getattr(fObj,f2)
        !           817:             
        !           818:             o = getattr(f2Obj,fileId)
        !           819:             logging.debug(o);
        !           820:             self.updateOrAddToFileBTree(o)
        !           821:             return o
        !           822:         return x
        !           823:     
        !           824:     def getFileObjectLastVersion(self,fileId):
        !           825:         """get an object"""
        !           826:         x=self.v_files_lastVersion.get(fileId)
        !           827:         logging.debug("lastVersion: "+repr(x))
        !           828:         if x==None:
        !           829:             folder=fileId[0:3]
        !           830:             f2=fileId[0:5]
        !           831:             fObj = getattr(self.cdliRoot.cdli_main,folder);
        !           832:             f2Obj = getattr(fObj,f2)
        !           833: 
        !           834:             o =getattr(f2Obj,fileId)
        !           835:             logging.debug(o);
        !           836:             return o.getLastVersion()
        !           837:         
        !           838: 
        !           839:         return x
        !           840:     
        !           841:     def showFileIds(self):
        !           842:         """showIds"""
        !           843:         return self.v_file_ids
        !           844:     
        !           845:     def generateFileBTree(self):
        !           846:         """erzeuge einen Btree aus allen Files"""
        !           847:         self.v_files = OOBTree()
        !           848:         self.v_files_lastVersion = OOBTree()
        !           849:         self.v_file_ids = Set()
        !           850:         
        !           851:         for x in self.CDLICatalog.searchResults():
        !           852:             
        !           853:             self.v_files.update({x.getId:x.getObject()})
        !           854:             self.v_files_lastVersion.update({x.getId:x.getObject().getLastVersion()})
        !           855:             self.v_file_ids.add(x.getId)
        !           856:             logging.debug("add:"+x.getId+"XXX"+repr(x.getObject()))
        !           857:         
        !           858:         return True
        !           859:     
        !           860:     
        !           861:     def updateOrAddToFileBTree(self,obj):
        !           862:         """update a BTree"""
        !           863:         self.v_files.update({obj.getId():obj})
        !           864:         self.v_files_lastVersion.update({obj.getId():obj.getLastVersion()})
        !           865:         
        !           866:         self.v_file_ids.add(obj.getId())
        !           867:         #change everthing around to make it persistent...
        !           868:         tmp = self.v_files
        !           869:         self.v_files=tmp
        !           870:         
        !           871:         tmp2=self.v_file_ids
        !           872:         self.v_file_ids=tmp2
        !           873: 
        !           874:         self.CDLICache.cleanCache() #be sure that the cache is clean
        !           875:         logging.debug("update:"+obj.getId()+"XXX"+repr(obj))
        !           876: 
        !           877: 
        !           878:     def deleteFromBTree(self,objId):
        !           879:         """delete an obj"""
        !           880:         self.v_files.pop(objId)
        !           881:         self.v_files_lastVersion.pop(objId)
        !           882:         self.v_file_ids.remove(objId)
        !           883:         
        !           884: 
        !           885:  
        !           886:     def deleteFiles(self,ids):
        !           887:         """delete files"""
        !           888:         for id in ids:
        !           889:             founds=self.CDLICatalog.search({'title':id.split(".")[0]})
        !           890:             if founds:
        !           891:                 logging.debug("deleting %s"%founds)
        !           892:                 folder=founds[0].getObject().aq_parent #get the parent folder of the object
        !           893:                 logging.debug("deleting from %s"%folder)
        !           894:                 cut=folder.delete([founds[0].getId]) #cut it out
        !           895: 
        !           896: 
        !           897: 
        !           898:     def searchText(self, query, index='graphemes', resultFilter=None):
        !           899:         """searches query in the fulltext index and returns a list of file IDs/P-numbers
        !           900:            resultFilter is matched against the beginning of the file ID"""
        !           901:         # see also: http://www.plope.com/Books/2_7Edition/SearchingZCatalog.stx#2-13
        !           902:         logging.debug("searchtext for '%s' in index %s"%(query,index))
        !           903:         #import Products.ZCTextIndex.QueryParser
        !           904:         #qp = QueryParser.QueryParser()
        !           905:         #logging.debug()
        !           906:         idxQuery = {index:{'query':query}}
        !           907:         idx = getattr(self, self.file_catalog)
        !           908:         # do search
        !           909:         resultset = idx.search(query_request=idxQuery,sort_index='textid')
        !           910:         # put only the P-Number in the result
        !           911:         if resultFilter is None:
        !           912:             results = [res.getId[:7] for res in resultset]
        !           913:         else:
        !           914:             results = [res.getId[:7] for res in resultset if res.getId.startswith(resultFilter)]
        !           915:         logging.debug("searchtext: found %d texts"%len(results))
        !           916:         return results
        !           917: 
        !           918: 
        !           919:     def getFile(self, pnum):
        !           920:         """get the translit file with the given pnum"""
        !           921:         f = getattr(self, self.file_catalog).search({'textid':pnum})
        !           922:         if not f:
        !           923:             return ""
        !           924:         
        !           925:         return f[0].getObject().getData()
        !           926:          
        !           927: 
        !           928:     def showFile(self,fileId,wholePage=False):
        !           929:         """show a file
        !           930:         @param fileId: P-Number of the document to be displayed
        !           931:         """
        !           932:         f=getattr(self, self.file_catalog).search({'textid':fileId})
        !           933:         if not f:
        !           934:             return ""
        !           935:         
        !           936:         if wholePage:
        !           937:             logging.debug("show whole page")
        !           938:             return f[0].getObject().getContentObject().view()
        !           939:         else:
        !           940:             return f[0].getObject().getLastVersionFormattedData()
        !           941:     
        !           942: 
        !           943:     def showWordInFile(self,fileId,word,indexName='graphemes',regExp=False,):
        !           944:         """get lines with word from FileId"""
        !           945:         logging.debug("showwordinfile word='%s' index=%s file=%s"%(word,indexName,fileId)) 
        !           946:         
        !           947:         file = formatAtfFullLineNum(self.getFile(fileId))
        !           948:         ret=[]
        !           949:         
        !           950:         # add whitespace before and whitespace and line-end to splitter bounds expressions
        !           951:         bounds = self.splitter[indexName].bounds
        !           952:         splitexp = "(%s|\s)(%%s)(%s|\s|\Z)"%(bounds,bounds)
        !           953:         # clean word expression 
        !           954:         # TODO: this should use QueryParser itself
        !           955:         # take out double quotes
        !           956:         word = word.replace('"','')
        !           957:         # take out ignorable signs
        !           958:         ignorable = self.splitter[indexName].ignorex
        !           959:         word = ignorable.sub('', word)
        !           960:         # compile into regexp objects and escape parens
        !           961:         wordlist = [re.compile(splitexp%re.escape(w)) for w in word.split(' ')]
        !           962:             
        !           963:         for line in file.splitlines():
        !           964:             for word in wordlist:
        !           965:                 #logging.debug("showwordinfile: searching for %s in %s"%(word.pattern,ignoreable.sub('',line)))
        !           966:                 if word.search(ignorable.sub('',line)):
        !           967:                     line = formatAtfLineHtml(line)
        !           968:                     ret.append(line)
        !           969:                     break
        !           970:                     
        !           971:         return ret
        !           972: 
        !           973:     
        !           974:     def showWordInFiles(self,fileIds,word,indexName='graphemes',regExp=False):
        !           975:         """
        !           976:         get lines with word from all ids in list FileIds.
        !           977:         returns dict with id:lines pairs.
        !           978:         """
        !           979:         logging.debug("showwordinfiles word='%s' index=%s file=%s"%(word,indexName,fileIds))
        !           980:         
        !           981:         return dict([(id,self.showWordInFile(id, word, indexName, regExp)) for id in fileIds])
        !           982:     
        !           983: 
        !           984:     def tagWordInFile(self,fileId,word,indexName='graphemes',regExp=False):
        !           985:         """get text with word highlighted from FileId"""
        !           986:         logging.debug("tagwordinfile word='%s' index=%s file=%s"%(word,indexName,fileId)) 
        !           987:         
        !           988:         file=self.getFile(fileId)
        !           989:         tagStart=u'<span class="found">'
        !           990:         tagEnd=u'</span>'
        !           991:         tagStr=tagStart + u'%%s' + tagEnd
        !           992:         ret=[]
        !           993:         
        !           994:         # add whitespace to splitter bounds expressions and compile into regexp object
        !           995:         bounds = self.splitter[indexName].bounds
        !           996:         wordsplit = re.compile("(%s|\s)"%bounds)
        !           997:         # clean word expression 
        !           998:         # TODO: this should use QueryParser itself
        !           999:         word = word.replace('"','') # take out double quotes
        !          1000:         # take out ignoreable signs
        !          1001:         ignorable = self.splitter[indexName].ignorex
        !          1002:         word = ignorable.sub('', word)
        !          1003:         # split search terms by blanks
        !          1004:         words = word.split(' ')
        !          1005:         # split search terms again (for grapheme search with words)
        !          1006:         splitwords = dict(((w,self.splitter[indexName].process([w])) for w in words))
        !          1007:             
        !          1008:         for line in file.splitlines():
        !          1009:             line = unicodify(line)
        !          1010:             # ignore lemma and other lines
        !          1011:             if line.lstrip().startswith('#lem:'):
        !          1012:                 continue
        !          1013:             # ignore p-num line
        !          1014:             if line.startswith('&P') or line.startswith('&S'):
        !          1015:                 continue
        !          1016:             # ignore version lines
        !          1017:             if line.startswith('#version'):
        !          1018:                 continue
        !          1019:             # ignore atf type lines
        !          1020:             if line.startswith('#atf:'):
        !          1021:                 continue
        !          1022: 
        !          1023:             # first scan
        !          1024:             hitwords = []
        !          1025:             for w in words:
        !          1026:                 if ignorable.sub('',line).find(w) > -1:
        !          1027:                     # word is in line
        !          1028:                     # append split word for grapheme search with words
        !          1029:                     hitwords.extend(splitwords[w])
        !          1030:                     #hitwords.extend(wordsplit.split(w))
        !          1031:                    
        !          1032:             # examine hits closer
        !          1033:             if hitwords:
        !          1034:                 # split line into words
        !          1035:                 parts = wordsplit.split(line)
        !          1036:                 line = ""
        !          1037:                 for p in parts:
        !          1038:                     #logging.debug("tagwordinfile: searching for %s in %s"%(p,hitwords))
        !          1039:                     # reassemble line
        !          1040:                     if ignorable.sub('', p) in hitwords:
        !          1041:                         #logging.debug("tagwordinfile: found %s in %s"%(p,hitwords))
        !          1042:                         # this part was found
        !          1043:                         line += tagStart + formatAtfHtml(p) + tagEnd
        !          1044:                     else:
        !          1045:                         line += formatAtfHtml(p)
        !          1046:                 
        !          1047:             else:
        !          1048:                 # no hits
        !          1049:                 line = formatAtfHtml(line)
        !          1050:             
        !          1051:             ret.append(line)
        !          1052:                         
        !          1053:         return u'<br>\n'.join(ret)
        !          1054: 
        !          1055: 
        !          1056: 
        !          1057:     def tagWordInFiles(self,fileIds,word,indexName='graphemes',regExp=False):
        !          1058:         """
        !          1059:         get texts with highlighted word from all ids in list FileIds.
        !          1060:         returns dict with id:text pairs.
        !          1061:         """
        !          1062:         logging.debug("tagwordinfiles word='%s' index=%s file=%s"%(word,indexName,fileIds)) 
        !          1063:         return dict([(id,self.tagWordInFile(id, word, indexName, regExp)) for id in fileIds])
        !          1064:     
        !          1065: 
        !          1066:     def getFileVersionList(self, pnum):
        !          1067:         """get the version history as a list for the translit file with the given pnum"""
        !          1068:         f = getattr(self, self.file_catalog).search({'textid':pnum})
        !          1069:         if not f:
        !          1070:             return []
        !          1071:         
        !          1072:         return f[0].getObject().getVersionList()
        !          1073:          
        !          1074: 
        !          1075:     def URLquote(self,str):
        !          1076:         """quote url"""
        !          1077:         return urllib.quote(str)
        !          1078:     
        !          1079:     def URLunquote(self,str):
        !          1080:         """unquote url"""
        !          1081:         return urllib.unquote(str)
        !          1082:     
        !          1083:     def URLquote_plus(self,str):
        !          1084:         """quote url"""
        !          1085:         return urllib.quote_plus(str)
        !          1086:     
        !          1087:     def URLunquote_plus(self,str):
        !          1088:         """unquote url"""
        !          1089:         return urllib.unquote_plus(str)
        !          1090:     
        !          1091:     def changeUserForPObjectFromFile(self,user,fname):
        !          1092:         """aendere user fuer alle p in fiele"""
        !          1093:         pns = file(os.path.join(package_home(globals()),'inputs',fname),"r")
        !          1094:         for p in pns.readlines():
        !          1095:             p=p.lstrip().rstrip()
        !          1096:             logging.info(str(p+".atf"))
        !          1097:             pObj=self.getFileObject(p+".atf")
        !          1098:             if pObj is not None:  
        !          1099:                 logging.info(pObj)
        !          1100:                 lv=pObj.getContentObject()
        !          1101:                 logging.info("author:"+lv.author)
        !          1102:                 lv.author=user
        !          1103:                 lv.versionComment="XXXXXXX"
        !          1104:     
        !          1105:     def forceunlock(self,REQUEST=None,user=None,fid=None):
        !          1106:         "break all locks"
        !          1107:         if fid is not None:
        !          1108:             self.getFileObject(fid).forceunlock()
        !          1109:             return fid
        !          1110:         ret=[]
        !          1111:         
        !          1112:         for f in self.ZopeFind(self,obj_metatypes="CDLI file",search_sub=1):
        !          1113:            un=f[1].forceunlock(user=user)
        !          1114:            logging.info("check:"+f[0])
        !          1115:            if un and un !="":
        !          1116:                ret.append((f[0],un))
        !          1117: 
        !          1118:         if REQUEST is not None:
        !          1119:             pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','breakLockResponse.zpt')).__of__(self)
        !          1120:            
        !          1121:             return pt(ret=ret)
        !          1122:         
        !          1123:         return ret
        !          1124:                                         
        !          1125: 
        !          1126:     def getLockedFiles(self,REQUEST=None,user=None):
        !          1127:         """hole alle gesperrten files"""
        !          1128:         ret={}
        !          1129:     
        !          1130:         for nm,f in self.v_files.items():
        !          1131:             lb = str(f.lockedBy)
        !          1132:             add=False
        !          1133:             if (lb is not None) and (lb!=""):
        !          1134:                
        !          1135:                 if user is None:
        !          1136:                     add=True
        !          1137:                 else:
        !          1138:                     if str(lb)==user:
        !          1139:                         add=True
        !          1140:                 if add==True:
        !          1141:                     if not ret.has_key(lb):
        !          1142:                         ret[lb]=[]
        !          1143:                     ret[lb].append(nm)
        !          1144:         
        !          1145:         
        !          1146:         if REQUEST is not None:
        !          1147:             pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','showlockResponse.zpt')).__of__(self)
        !          1148:            
        !          1149:             return pt(ret=ret)
        !          1150:         
        !          1151:         return ret
        !          1152:         
        !          1153:     def getChangesByAuthor(self,author,n=100):
        !          1154:         """getChangesByAuthor"""
        !          1155:         zcat=self.CDLIObjectsCatalog
        !          1156:         res=zcat({'lastEditor':author,
        !          1157:                      'sort_on':'getTime',
        !          1158:                      'sort_order':'descending',
        !          1159:                      'sort_limit':n})[:n ]
        !          1160:                        
        !          1161:         return res
        !          1162:     
        !          1163:     def getChangesByAuthor_html(self,author,n=100):
        !          1164:         """html output for changes by author"""
        !          1165:         tmp={}
        !          1166:         list=[]                         
        !          1167:         for x in self.getChangesByAuthor(author):
        !          1168:            nr=x.getObject().getVersionNumber()
        !          1169:            id=x.getObject().aq_parent.getId()
        !          1170:            #hinzufuegen, wenn Version neuer als die 
        !          1171:            if tmp.get(id,(0,0))[1] < nr:
        !          1172:                 tmp[id]=(x.getObject().aq_parent,nr)
        !          1173: 
        !          1174:      
        !          1175:         return self.cdli_main.findObjectsFromListWithVersion(list=tmp.values(),author=author)           
        !          1176:         
        !          1177:     def getLastChanges(self,n=100):
        !          1178:         """get the last n changes""" 
        !          1179:         n=int(n)                   
        !          1180:         zcat=self.CDLICatalog
        !          1181:         return zcat({'sort_on':'getLastChangeDate',
        !          1182:                      'sort_order':'descending',
        !          1183:                      'sort_limit':n})[:n ]
        !          1184:      
        !          1185:     
        !          1186:     def getLastChanges_html(self,n=100):
        !          1187:         """get the last n changes"""
        !          1188:         list = [x.getId for x in self.getLastChanges(n)]
        !          1189:         return self.cdli_main.findObjectsFromList(list=list,display=True)
        !          1190:                                        
        !          1191:     def refreshTxt(self,txt="",threadName=None):
        !          1192:         """txt fuer refresh"""
        !          1193:   
        !          1194:         return """ 2;url=%s?repeat=%s """%(self.absolute_url()+txt,threadName)
        !          1195: 
        !          1196:     def refreshTxtBasket(self,txt="",threadName=None):
        !          1197:         """txt fuer refresh"""
        !          1198:   
        !          1199:         return """ 2;url=%s?repeat=%s """%(txt,threadName)
        !          1200: 
        !          1201:     
        !          1202:     def getResult(self,threadName=None):
        !          1203:        """result of thread"""
        !          1204:        try:
        !          1205:         return self._v_uploadATF[threadName].getResult()
        !          1206:        except:
        !          1207:         return "One moment, please"
        !          1208:     
        !          1209:         
        !          1210:     def checkThreads(self):
        !          1211:         """check threads"""
        !          1212:         ret="<html><body>"
        !          1213:         for thread in threading.enumerate():
        !          1214:            ret+="<p>%s (%s): %s</p>"%(repr(thread),thread.getName(),thread.isAlive())
        !          1215:        
        !          1216:         return ret
        !          1217:                                        
        !          1218:                                            
        !          1219:     def uploadATFRPC(self,data,username):
        !          1220:         """upload an atffile via xml-rpc"""
        !          1221:         uploader=uploadATFThread()
        !          1222:         
        !          1223:         #generate an random id for the upload object
        !          1224:         from random import randint
        !          1225:         if (not self.REQUEST.SESSION.get('idTmp',None)):
        !          1226: 
        !          1227:             idTmp=str(randint(0,1000000000))
        !          1228:             self.REQUEST.SESSION['idTmp']=idTmp
        !          1229:         else:
        !          1230:             idTmp=self.REQUEST.SESSION.get('idTmp',None)
        !          1231:             
        !          1232:         
        !          1233:         uploader.set(data,0,username,idTmp)
        !          1234:         
        !          1235:         stObj=uploader.run()
        !          1236:         
        !          1237:         processor=uploadATFfinallyThread()
        !          1238:         
        !          1239:         basketname=stObj.returnValue['basketNameFromFile']
        !          1240:         
        !          1241:         processor.set("uploadchanged",basketname=basketname,SESSION=stObj.returnValue,username=username,serverport=self.REQUEST['SERVER_PORT'])
        !          1242:         
        !          1243:         processor.run()
        !          1244:         
        !          1245:         
        !          1246:         return generateXMLReturn(stObj.returnValue)
        !          1247:     
        !          1248:     def uploadATFAsync(self,repeat=None,upload=None,basketId=0,RESPONSE=None):
        !          1249:         """upload an atf file / basket file"""
        !          1250:         #self._v_uploadATF.returnValue=None
        !          1251:         
        !          1252:        
        !          1253:         idTmp=str(randint(0,1000000000))
        !          1254:         
        !          1255:         if upload is None:
        !          1256:             return "ERROR NO FILE!"
        !          1257:         
        !          1258:         tmpFile = File("/tmp/idTmp","w")
        !          1259:         # sicher basket in file
        !          1260:         for x in upload.read():
        !          1261:             tmpFile.write(x)
        !          1262:         
        !          1263:         tmpFile.close();
        !          1264:         uploadATFAsync(baskerId,idTmp,basketId,self.REQUEST['AUTHENTICATED_USER'],idTmp,serverport=self.REQUEST['SERVER_PORT'])
        !          1265:         
        !          1266:         return idTMP   
        !          1267:         
        !          1268:     def viewTicketAsync(self,ticketNr=1):
        !          1269:         """viewticket"""
        !          1270:         
        !          1271:         tmp = pickle.load(file("/tmp/"+str(ticketNr)+".result"))
        !          1272:         
        !          1273:         pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','uploadCheckAsync.zpt')).__of__(self)
        !          1274:         
        !          1275:         return pt(changed=tmp['changed'],lockerrors=tmp['lockerrors'],errors=tmp['errors'],dir=tmp['dir'],newPs=tmp['newPs'],basketLen=tmp['basketLen'],numberOfFiles=tmp['numberOfFiles'],
        !          1276:                   basketNameFromId=tmp['basketNameFromId'],basketNameFromFile=tmp['basketNameFromFile'],basketId=tmp['basketId'])
        !          1277:                
        !          1278:         
        !          1279:      
        !          1280:     def uploadATF(self,repeat=None,upload=None,basketId=0,RESPONSE=None):
        !          1281:         """upload an atf file / basket file"""
        !          1282:         #self._v_uploadATF.returnValue=None
        !          1283:         
        !          1284:         #generate an random id for the upload thread
        !          1285:         from random import randint
        !          1286:         if (not self.REQUEST.SESSION.get('idTmp',None)):
        !          1287: 
        !          1288:             idTmp=str(randint(0,1000000000))
        !          1289:             self.REQUEST.SESSION['idTmp']=idTmp
        !          1290:         else:
        !          1291:             idTmp=self.REQUEST.SESSION.get('idTmp',None)
        !          1292:             
        !          1293:     
        !          1294:         threadName=repeat
        !          1295:         if not threadName or threadName=="":
        !          1296:             #new thread not called from the waiting page
        !          1297:             tmpVar=False
        !          1298:        
        !          1299:             thread=uploadATFThread()
        !          1300:             threadName=thread.getName()[0:]                                
        !          1301:             if (not hasattr(self,'_v_uploadATF')):
        !          1302:                    self._v_uploadATF={}
        !          1303:                                        
        !          1304:             self._v_uploadATF[threadName]=thread
        !          1305:             #self._xmltrans.start()
        !          1306:             #thread=Thread(target=self._v_uploadATF)
        !          1307:             logging.info("set thread. extern")
        !          1308:             self._v_uploadATF[threadName].set(upload,basketId,self.REQUEST['AUTHENTICATED_USER'],idTmp,serverport=self.REQUEST['SERVER_PORT'])
        !          1309:             #thread.start()
        !          1310:             logging.info("start thread. extern")
        !          1311:             self._v_uploadATF[threadName].start()
        !          1312: 
        !          1313:             
        !          1314:             self.threadName=self._v_uploadATF[threadName].getName()[0:]
        !          1315:             wait_template=self.aq_parent.ZopeFind(self.aq_parent,obj_ids=['wait_template'])
        !          1316: 
        !          1317:             if wait_template:
        !          1318:                 return wait_template[0][1]()
        !          1319:             pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','uploadATFWait.zpt')).__of__(self)
        !          1320:             return pt(txt='/uploadATF',threadName=threadName)
        !          1321:             #_v_xmltrans.run()
        !          1322:             
        !          1323:         else:
        !          1324:             #recover thread, if lost
        !          1325:             if (not hasattr(self,'_v_uploadATF')):
        !          1326:                self._v_uploadATF={}
        !          1327:             if not self._v_uploadATF.get(threadName,None):
        !          1328:                  for thread in threading.enumerate():
        !          1329:                          if threadName == thread.getName():
        !          1330:                                        self._v_uploadATF[threadName]=thread
        !          1331:                                        
        !          1332:             if self._v_uploadATF.get(threadName,None) and (not self._v_uploadATF[threadName].returnValue):
        !          1333:         
        !          1334: 
        !          1335:                 wait_template=self.aq_parent.ZopeFind(self.aq_parent,obj_ids=['wait_template'])
        !          1336:                 if wait_template:
        !          1337:                         return wait_template[0][1]()
        !          1338:                 
        !          1339:                 pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','uploadATFWait.zpt')).__of__(self)
        !          1340: 
        !          1341:                 return pt(txt='/uploadATF',threadName=threadName)
        !          1342:                 
        !          1343:             else:
        !          1344:                 tmp=getattr(self.temp_folder,idTmp).returnValue
        !          1345:  
        !          1346:                 pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','uploadCheck.zpt')).__of__(self)
        !          1347: 
        !          1348:                 return pt(changed=tmp['changed'],lockerrors=tmp['lockerrors'],errors=tmp['errors'],dir=tmp['dir'],newPs=tmp['newPs'],basketLen=tmp['basketLen'],numberOfFiles=tmp['numberOfFiles'],
        !          1349:                   basketNameFromId=tmp['basketNameFromId'],basketNameFromFile=tmp['basketNameFromFile'],basketId=tmp['basketId'])
        !          1350:                      
        !          1351:           
        !          1352:     def redoUpload(self,threadName):
        !          1353:        """redo the upload"""
        !          1354:        tmp=self.cdli_main.tmpStore2[threadName]
        !          1355:        pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','uploadCheck.zpt')).__of__(self)
        !          1356:        return pt(changed=tmp['changed'],lockerrors=tmp['lockerrors'],errors=tmp['errors'],dir=tmp['dir'],newPs=tmp['newPs'],basketLen=tmp['basketLen'],numberOfFiles=tmp['numberOfFiles'],
        !          1357:                   basketNameFromId=tmp['basketNameFromId'],basketNameFromFile=tmp['basketNameFromFile'],basketId=tmp['basketId'])
        !          1358:                  
        !          1359:     def uploadATFfinally(self,procedure='',comment="",basketname='',unlock=None,repeat=None,RESPONSE=None):
        !          1360:         """nowupload the files"""
        !          1361:         threadName=repeat
        !          1362:         if not threadName or threadName=="":
        !          1363:             thread=uploadATFfinallyThread()
        !          1364:             threadName=thread.getName()[0:]
        !          1365: 
        !          1366:             if (not hasattr(self,'_v_uploadATF')):
        !          1367:                                 self._v_uploadATF={}
        !          1368: 
        !          1369: 
        !          1370:             self._v_uploadATF[threadName]=thread
        !          1371: 
        !          1372:             idTmp=self.REQUEST.SESSION['idTmp']
        !          1373:             stObj=getattr(self.temp_folder,idTmp)
        !          1374:             self._v_uploadATF[threadName].set(procedure,comment=comment,basketname=basketname,unlock=unlock,SESSION=stObj.returnValue,username=self.REQUEST['AUTHENTICATED_USER'],serverport=self.REQUEST['SERVER_PORT'])
        !          1375: 
        !          1376:             self._v_uploadATF[threadName].start()
        !          1377: 
        !          1378:             
        !          1379:             
        !          1380:             wait_template=self.aq_parent.ZopeFind(self.aq_parent,obj_ids=['wait_template'])
        !          1381: 
        !          1382:             if wait_template:
        !          1383:                 return wait_template[0][1]()
        !          1384:             pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','uploadATFWait.zpt')).__of__(self)
        !          1385: 
        !          1386:             return pt(txt='/uploadATFfinally',threadName=threadName)
        !          1387:             #_v_xmltrans.run()
        !          1388:         
        !          1389:         else:
        !          1390:             #recover thread, if lost
        !          1391:             if not hasattr(self,'_v_uploadATF'):
        !          1392:                self._v_uploadATF={}
        !          1393:             if not self._v_uploadATF.get(threadName,None):
        !          1394:                  for thread in threading.enumerate():
        !          1395:                          if threadName == thread.getName():
        !          1396:                                        self._v_uploadATF[threadName]=thread
        !          1397:                                        
        !          1398:             if self._v_uploadATF.get(threadName,None) and (self._v_uploadATF[threadName] is not None) and (not self._v_uploadATF[threadName].end) :
        !          1399: 
        !          1400:                 wait_template=self.aq_parent.ZopeFind(self.aq_parent,obj_ids=['wait_template'])
        !          1401:                 if wait_template:
        !          1402:                         return wait_template[0][1]()
        !          1403:                 
        !          1404:                 pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','uploadATFWait.zpt')).__of__(self)
        !          1405:                 return pt(txt='/uploadATFfinally',threadName=threadName)
        !          1406:             else:
        !          1407:               
        !          1408:              
        !          1409:               idTmp=self.REQUEST.SESSION['idTmp']
        !          1410:               stObj=getattr(self.temp_folder,idTmp) 
        !          1411:               self.REQUEST.SESSION['idTmp']=None
        !          1412:              
        !          1413:               #update changed
        !          1414:               logging.debug("dir:"+repr(stObj.returnValue['changed']))
        !          1415:               for x in stObj.returnValue['changed']:
        !          1416:                     ob=self.CDLICatalog.search({'title':x[0]})
        !          1417:                     try:
        !          1418:                         self.cdliRoot.updateOrAddToFileBTree(ob[0].getObject())
        !          1419:                     except:
        !          1420:                
        !          1421:                         logging.error("uploadATFfinally - cannot update Object %s Error: %s %s"%(ob[1],sys.exc_info()[0],sys.exc_info()[1]))
        !          1422:               for x in stObj.returnValue['newPs']:
        !          1423:                   obj=self.getFileObject(x) #updates the object in the cache
        !          1424:                   logging.debug("Got:"+repr(obj))
        !          1425:               if RESPONSE is not None:
        !          1426:                   RESPONSE.redirect(self.absolute_url())
        !          1427: 
        !          1428:     def importFiles(self,comment="",author="" ,folderName="/Users/dwinter/atf", files=None,ext=None):
        !          1429:         """import files"""
        !          1430:         logging.debug("importFiles folderName=%s files=%s ext=%s"%(folderName,files,ext))
        !          1431:         root=self.cdli_main
        !          1432:         count=0
        !          1433:         if not files:
        !          1434:             files=os.listdir(folderName)
        !          1435:             
        !          1436:         for f in files:
        !          1437:             folder=f[0:3]
        !          1438:             f2=f[0:5]
        !          1439:             
        !          1440:             #check if main folder PXX already exists
        !          1441:             obj=self.ZopeFind(root,obj_ids=[folder])
        !          1442:             logging.debug("importFiles: folder=%s f2=%s obj=%s"%(folder,f2,obj)) 
        !          1443:             if ext:
        !          1444:                 
        !          1445:                 if type(ext.result) is types.FileType:
        !          1446:                     ext.result.write("<p>adding: %s </p>\n"%f)
        !          1447:                 else:
        !          1448:                     ext.result="<p>adding: %s </p>"%f+ext.result
        !          1449: 
        !          1450:             
        !          1451:             if not obj: # if not create it
        !          1452:                 manage_addCDLIFileFolder(root,folder,folder)
        !          1453:                 fobj=getattr(root,folder)
        !          1454:                 #transaction.get().commit()                           
        !          1455: 
        !          1456:             else:
        !          1457:                 fobj=obj[0][1]
        !          1458:             
        !          1459:             #check IF PYYYYY already exist
        !          1460:             obj2=fobj.ZopeFind(fobj,obj_ids=[f2])
        !          1461:             logging.debug("importFiles: fobj=%s obj2=%s"%(fobj,obj2)) 
        !          1462:         
        !          1463:             if not obj2:# if not create it
        !          1464:                 manage_addCDLIFileFolder(fobj,f2,f2)
        !          1465:                 fobj2=getattr(fobj,f2)
        !          1466:         
        !          1467:             else:
        !          1468:                 fobj2=obj2[0][1]
        !          1469:               
        !          1470:             # not add the file
        !          1471:             file2=os.path.join(folderName,f)  
        !          1472:             id=f
        !          1473:             logging.debug("importFiles: addCDLIFile fobj2=%s, f=%s file2=%s"%(fobj2,repr(f),repr(file2)))
        !          1474:             fobj2.addFile(vC='',file=file(file2),author=author,newName=f)
        !          1475:             logging.debug("importfiles: fobj2.add")
        !          1476:             count+=1
        !          1477:             
        !          1478:             #now add the file to the storage
        !          1479:             ob = getattr(fobj2,f)
        !          1480:             logging.debug("importfiles: btree_start")
        !          1481:             #self.cdliRoot.updateOrAddToFileBTree(ob)
        !          1482:             logging.debug("importfiles: btree_end")
        !          1483:             if count%100==0:
        !          1484:                 logging.debug("importfiles: committing")
        !          1485:                 transaction.get().commit()
        !          1486: 
        !          1487:         logging.debug("importfiles: committing")
        !          1488:         transaction.get().commit()
        !          1489:         logging.debug("importfiles: committing done")
        !          1490:         return "ok"
        !          1491:          
        !          1492: 
        !          1493: manage_addCDLIRootForm=DTMLFile('dtml/rootAdd', globals())
        !          1494: 
        !          1495:     
        !          1496: def manage_addCDLIRoot(self, id, title='',
        !          1497:                      createPublic=0,
        !          1498:                      createUserF=0,
        !          1499:                      REQUEST=None):
        !          1500:     """Add a new Folder object with id *id*.
        !          1501: 
        !          1502:     If the 'createPublic' and 'createUserF' parameters are set to any true
        !          1503:     value, an 'index_html' and a 'UserFolder' objects are created respectively
        !          1504:     in the new folder.
        !          1505:     """
        !          1506:     ob=CDLIRoot()
        !          1507:     ob.id=str(id)
        !          1508:     ob.title=title
        !          1509:     try:
        !          1510:         self._setObject(id, ob)
        !          1511:     except:
        !          1512:         pass
        !          1513:     ob=self._getOb(id)
        !          1514: 
        !          1515:     checkPermission=getSecurityManager().checkPermission
        !          1516: 
        !          1517:     if createUserF:
        !          1518:         if not checkPermission('Add User Folders', ob):
        !          1519:             raise Unauthorized, (
        !          1520:                   'You are not authorized to add User Folders.'
        !          1521:                   )
        !          1522:         ob.manage_addUserFolder()
        !          1523: 
        !          1524:   
        !          1525:     if REQUEST is not None:
        !          1526:         return self.manage_main(self, REQUEST, update_menu=1)    
        !          1527:  
        !          1528:  
        !          1529: import cdli_basket
        !          1530: 
        !          1531: 
        !          1532: # Die folgenden Klassen sollte nicht mehr aus diesem Paket benutzt werden, sondern direkt aus
        !          1533: # cdli_basket importiert werden.
        !          1534: class uploadATFfinallyThread(cdli_basket.uploadATFfinallyThread):
        !          1535:     """depricates"""
        !          1536:     pass
        !          1537: 
        !          1538: class tmpStore(cdli_basket.tmpStore):
        !          1539:     """depricated"""
        !          1540:     pass
        !          1541: 
        !          1542: class uploadATFThread(cdli_basket.uploadATFThread):
        !          1543:     """depricated"""
        !          1544:     pass
        !          1545: 
        !          1546: class CDLIBasketContainer(cdli_basket.CDLIBasketContainer):
        !          1547:     """depricated"""
        !          1548:     pass
        !          1549: 
        !          1550: class CDLIBasket(cdli_basket.CDLIBasket):
        !          1551:     """depricated"""
        !          1552:     pass
        !          1553: 
        !          1554: class CDLIBasketVersion(cdli_basket.CDLIBasketVersion):
        !          1555:     """depricated"""
        !          1556:     pass
        !          1557: 
        !          1558: class BasketContent(cdli_basket.BasketContent):
        !          1559:     """depricated"""
        !          1560:     pass
        !          1561:     

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