1: """CDLI extensions of the filearchive"""
2: from Products.versionedFile.extVersionedFile import *
3: from Products.ZCatalog.CatalogPathAwareness import CatalogAware
4: from tempfile import mkstemp,mkdtemp
5: import os.path
6: import os
7: from types import *
8: import urlparse
9: import urllib
10: import cgi
11: from OFS.OrderedFolder import OrderedFolder
12: from OFS.SimpleItem import SimpleItem
13: import time
14: from OFS.Folder import manage_addFolder
15: import re
16: from AccessControl import ClassSecurityInfo
17: from Acquisition import Implicit
18: from Globals import Persistent
19: from threading import Thread
20: from ZPublisher.HTTPRequest import HTTPRequest
21: from ZPublisher.HTTPResponse import HTTPResponse
22: from ZPublisher.BaseRequest import RequestContainer
23: import threading
24: from BTrees.OOBTree import OOBTree, OOTreeSet
25: import logging
26: import transaction
27: import copy
28: import codecs
29: import sys
30: from BTrees.IOBTree import IOBTree
31: import cdliSplitter
32: from sets import Set
33: import md5
34: from DownloadBasket import DownloadBasketFinallyThread
35:
36: def makelist(mySet):
37: x = list(mySet)
38: x.sort()
39: return x
40:
41: def unicodify(s):
42: """decode str (utf-8 or latin-1 representation) into unicode object"""
43: if not s:
44: return u""
45: if isinstance(s, str):
46: try:
47: return s.decode('utf-8')
48: except:
49: return s.decode('latin-1')
50: else:
51: return s
52:
53: def utf8ify(s):
54: """encode unicode object or string into byte string in utf-8 representation.
55: assumes string objects to be utf-8"""
56: if not s:
57: return ""
58: if isinstance(s, str):
59: return s
60: else:
61: return s.encode('utf-8')
62:
63: def formatAtfHtml(l):
64: """escape special ATF characters for HTML"""
65: if not l:
66: return ""
67:
68: # replace &
69: l = l.replace('&','&')
70: # replace angular brackets
71: l = l.replace('<','<')
72: l = l.replace('>','>')
73: return l
74:
75: def formatAtfLineHtml(l, nolemma=True):
76: """format ATF line for HTML"""
77: if not l:
78: return ""
79:
80: if nolemma:
81: # ignore lemma lines
82: if l.lstrip().startswith('#lem:'):
83: return ""
84:
85: return formatAtfHtml(l)
86:
87:
88:
89: def formatAtfFullLineNum(txt, nolemma=True):
90: """format full line numbers in ATF text"""
91: # surface codes
92: surfaces = {'@obverse':'obv',
93: '@reverse':'rev',
94: '@surface':'surface',
95: '@edge':'edge',
96: '@left':'left',
97: '@right':'right',
98: '@top':'top',
99: '@bottom':'bottom',
100: '@face':'face',
101: '@seal':'seal'}
102:
103: if not txt:
104: return ""
105:
106: ret = []
107: surf = ""
108: col = ""
109: for line in txt.splitlines():
110: line = unicodify(line)
111: if line and line[0] == '@':
112: # surface or column
113: words = line.split(' ')
114: if words[0] in surfaces:
115: surf = line.replace(words[0],surfaces[words[0]]).strip()
116:
117: elif words[0] == '@column':
118: col = ' '.join(words[1:])
119:
120: elif line and line[0] in '123456789':
121: # ordinary line -> add line number
122: line = "%s:%s:%s"%(surf,col,line)
123:
124: ret.append(line)
125:
126: return '\n'.join(ret)
127:
128:
129: def generateXMLReturn(hash):
130: """erzeugt das xml file als returnwert fuer uploadATFRPC"""
131:
132: ret="<return>"
133:
134: ret+="<errors>"
135: for error in hash['errors']:
136: ret+="""<error atf="%s">%s</error>"""%error
137:
138: ret+="</errors>"
139:
140: ret+="<changes>"
141: for changed in hash['changed']:
142: ret+="""<change atf="%s">%s</change>"""%changed
143: ret+="</changes>"
144:
145: ret+="<newPs>"
146: for new in hash['newPs']:
147: ret+="""<new atf="%s"/>"""%new
148: ret+="</newPs>"
149:
150: ret+="</return>"
151: return ret
152:
153:
154: def unique(s):
155: """Return a list of the elements in s, but without duplicates.
156:
157: For example, unique([1,2,3,1,2,3]) is some permutation of [1,2,3],
158: unique("abcabc") some permutation of ["a", "b", "c"], and
159: unique(([1, 2], [2, 3], [1, 2])) some permutation of
160: [[2, 3], [1, 2]].
161:
162: For best speed, all sequence elements should be hashable. Then
163: unique() will usually work in linear time.
164:
165: If not possible, the sequence elements should enjoy a total
166: ordering, and if list(s).sort() doesn't raise TypeError it's
167: assumed that they do enjoy a total ordering. Then unique() will
168: usually work in O(N*log2(N)) time.
169:
170: If that's not possible either, the sequence elements must support
171: equality-testing. Then unique() will usually work in quadratic
172: time.
173: (from the python cookbook)
174: """
175:
176: n = len(s)
177: if n == 0:
178: return []
179:
180: # Try using a dict first, as that's the fastest and will usually
181: # work. If it doesn't work, it will usually fail quickly, so it
182: # usually doesn't cost much to *try* it. It requires that all the
183: # sequence elements be hashable, and support equality comparison.
184: u = {}
185: try:
186: for x in s:
187: u[x] = 1
188: except TypeError:
189: del u # move on to the next method
190: else:
191: return u.keys()
192:
193: # We can't hash all the elements. Second fastest is to sort,
194: # which brings the equal elements together; then duplicates are
195: # easy to weed out in a single pass.
196: # NOTE: Python's list.sort() was designed to be efficient in the
197: # presence of many duplicate elements. This isn't true of all
198: # sort functions in all languages or libraries, so this approach
199: # is more effective in Python than it may be elsewhere.
200: try:
201: t = list(s)
202: t.sort()
203: except TypeError:
204: del t # move on to the next method
205: else:
206: assert n > 0
207: last = t[0]
208: lasti = i = 1
209: while i < n:
210: if t[i] != last:
211: t[lasti] = last = t[i]
212: lasti += 1
213: i += 1
214: return t[:lasti]
215:
216: # Brute force is all that's left.
217: u = []
218: for x in s:
219: if x not in u:
220: u.append(x)
221: return u
222:
223:
224: class BasketContent(SimpleItem):
225: """classe fuer den Inhalt eines Baskets"""
226:
227: def __init__(self,content=[]):
228: """content"""
229: self.contentList=content[0:]
230:
231: def getContent(self):
232: """get content"""
233:
234: return self.contentList
235:
236: def setContent(self,content):
237: self.contentList=content[0:]
238:
239: def numberOfItems(self):
240: """number"""
241:
242: return len(self.getContent())
243:
244:
245: class uploadATFfinallyThread(Thread):
246: """class for adding uploaded filed (temporarily stored in the staging area at /tmp"""
247:
248: def __init__(self):
249: """init for uploadATFfinallyThread"""
250: self.continueVar=True
251: self.returnValue=None
252: self.end=False
253: Thread.__init__(self)
254:
255: def set(self,procedure,comment="",basketname='',unlock=None,SESSION=None,username=None,serverport="8080"):
256: """set start values for the thread"""
257: self.procedure=procedure
258: self.comment=comment
259: self.basketname=basketname
260: self.unlock=unlock
261: self.SESSION=SESSION
262: self.username=username
263: self.serverport=serverport
264:
265:
266: def __call__(self):
267: """call of the thread (equals run)"""
268: self.run()
269: return True
270:
271: def getContext(self, app,serverport="8080"):
272: """get the context within the ZODB"""
273:
274: resp = HTTPResponse(stdout=None)
275: env = {
276: 'SERVER_NAME':'localhost',
277: 'SERVER_PORT':serverport,
278: 'REQUEST_METHOD':'GET'
279: }
280: req = HTTPRequest(None, env, resp)
281: return app.__of__(RequestContainer(REQUEST = req))
282:
283:
284: def run(self):
285: """run"""
286:
287: self.result=""
288: #find context within ZODB
289: from Zope import DB
290: conn = DB.open()
291: root = conn.root()
292: app = root['Application']
293: ctx = self.getContext(app,serverport=self.serverport)
294:
295: #add the files
296: self.uploadATFfinallyThread(ctx,self.procedure,comment=self.comment,basketname=self.basketname,unlock=self.unlock,SESSION=self.SESSION,username=self.username)
297: #commit the transactions
298: transaction.get().commit()
299: conn.close()
300: #set flag for end of this method
301: self.end=True
302: logging.info("ended")
303: return True
304:
305: def __del__(self):
306: """delete"""
307:
308:
309:
310: def getResult(self):
311: """method for accessing result"""
312:
313: return self.result
314:
315: def uploadATFfinallyThread(self,ctx,procedure,comment="",basketname='',unlock=None,RESPONSE=None,SESSION=None,username=None):
316: """upload the files"""
317: #TODO: make this configurable, at the moment, rootFolder for cdli has to be cdliRoot
318: ctx2=ctx.cdliRoot
319:
320: self.result+="<h2>Start processing</h2>"
321:
322: #shall I only upload the changed files?
323: logging.debug("uploadATFfinally procedure: %s"%procedure)
324: if procedure=="uploadchanged":
325: changed=[x[0] for x in SESSION.get('changed',[])]
326: uploadFns=changed+SESSION.get('newPs',[])
327:
328: #or all
329: elif procedure=="uploadAll":
330: uploadFns=[]
331: for x in os.listdir(SESSION['tmpdir']):
332: if not x in SESSION['lockerrors']:
333: uploadFns.append(x)
334:
335: #or maybe nothing
336: elif procedure=="noupload":
337: return True
338: else:
339: uploadFns=[]
340:
341: #do first the changed files
342: i=0
343: for fn in uploadFns:
344: logging.debug("uploadATFfinally uploadFn=%s"%fn)
345: i+=1
346: founds=ctx2.CDLICatalog.search({'title':fn})
347: if len(founds)>0:
348: SESSION['author']=str(username)
349: self.result="<p>Changing : %s"%fn+self.result
350: logging.debug("uploadatffinallythread changing:%s"%fn+self.result)
351: founds[0].getObject().manage_addCDLIFileObject('',comment,SESSION['author'],file=os.path.join(SESSION['tmpdir'],fn),from_tmp=True)
352: if i%200==0:
353: transaction.get().commit()
354: logging.debug("uploadatffinallythread changing: do commit")
355:
356: transaction.get().commit()
357: logging.debug("uploadatffinallythread changing: last commit")
358:
359: #now add the new files
360: newPs=SESSION['newPs']
361: if len(newPs)>0:
362: tmpDir=SESSION['tmpdir']
363: logging.debug("uploadatffinallythread adding start")
364: self.result="<p>Adding files</p>"+self.result
365: #TODO: make this configurable, at the moment base folder for the files has to be cdli_main
366: ctx2.importFiles(comment=comment,author=str(username) ,folderName=tmpDir, files=newPs,ext=self)
367: logging.debug("uploadatffinallythread adding finished")
368:
369: #unlock locked files?
370: if unlock:
371: logging.debug("uploadatffinallythread unlocking start")
372: self.result="<p>Unlock files</p>"+self.result
373: unlockFns=[]
374: for x in os.listdir(SESSION['tmpdir']):
375: if not x in SESSION['errors']:
376: unlockFns.append(x)
377:
378: logging.debug("unlocking have now what to unlock")
379:
380: for fn in unlockFns:
381: #logging.info("will unlock: %s"%fn)
382: founds=ctx2.CDLICatalog.search({'title':fn})
383: #logging.info("found it: %s"%repr(founds))
384: if len(founds)>0:
385: #logging.info("unlock: %s"%founds[0].getObject().getId())
386: SESSION['author']=str(username)
387: founds[0].getObject().lockedBy=""
388:
389: logging.debug("uploadatffinallythread unlocking done")
390:
391: #if a basketname is given, add files to the basket
392: if not (basketname ==''):
393: logging.debug("uploadatffinallythread add to basket %s"%basketname)
394: self.result="<p>Add to basket</p>"+self.result
395: basketId=ctx2.basketContainer.getBasketIdfromName(basketname)
396:
397: if not basketId: # create new basket
398: logging.debug("uploadatffinallythread create basket %s"%basketname)
399: self.result="<p>Create a new basket</p>"+self.result
400: ob=ctx2.basketContainer.addBasket(basketname)
401: basketId=ob.getId()
402: basket=getattr(ctx2.basketContainer,str(basketId))
403: ids=os.listdir(SESSION['tmpdir'])
404: logging.debug("should add:"+repr(ids))
405: basket.addObjects(ids,deleteOld=True,username=str(username))
406:
407: logging.debug("uploadatffinallythread uploadfinally done")
408:
409: if RESPONSE is not None:
410: RESPONSE.redirect(self.aq_parent.absolute_url())
411:
412: return True
413:
414: class tmpStore(SimpleItem):
415: """simple item"""
416: meta_type="cdli_upload"
417:
418: def __init__(self,id):
419: """init tmp"""
420: self.id=id
421:
422: class uploadATFThread(Thread):
423: """class for checking the files befor uploading"""
424:
425: def __init__(self):
426: """initialise"""
427:
428: self.continueVar=True
429: self.returnValue=None
430:
431: Thread.__init__(self)
432:
433:
434: def set(self,upload,basketId,username,idTmp,serverport="8080"):
435: """set start values for the thread"""
436: self.result=""
437: self.upload=upload
438: self.basketId=basketId
439: self.username=username
440: self.serverport=serverport
441: self.idTmp=idTmp
442:
443: def __call__(self):
444: """call method """
445: self.run()
446: return True
447:
448: def getContext(self, app,serverport="8080"):
449: """get the context within the ZODB"""
450: resp = HTTPResponse(stdout=None)
451: env = {
452: 'SERVER_NAME':'localhost',
453: 'SERVER_PORT':serverport,
454: 'REQUEST_METHOD':'GET'
455: }
456: req = HTTPRequest(None, env, resp)
457: return app.__of__(RequestContainer(REQUEST = req))
458:
459: def run(self):
460: idTmp=self.idTmp
461: self.result=""
462: #find context within ZODB
463: from Zope import DB
464: conn = DB.open()
465: root = conn.root()
466: app = root['Application']
467: ctx = self.getContext(app,serverport=self.serverport)
468: logging.info("run intern")
469: try:
470: logging.info("created: %s"%idTmp)
471: ctx.temp_folder._setObject(idTmp,tmpStore(idTmp))
472: except:
473: logging.error("thread upload: %s %s"%sys.exc_info()[0:2])
474:
475: logging.info("call thread intern")
476: self.uploadATFThread(ctx,self.upload,idTmp,self.basketId)
477:
478: #ctx.cdliRoot.cdli_main.tmpStore2[self.getName()[0:]]=self.returnValue
479:
480:
481: transaction.get().commit()
482:
483: conn.close()
484:
485: return getattr(ctx.temp_folder,idTmp)
486:
487: def getResult(self):
488: """method for accessing result"""
489: return self.result
490:
491: def uploadATFThread(self,ctx,upload,idTmp,basketId=0):
492: """upload an atf file"""
493: #TODO: add comments
494: #TODO: finish uploadATF
495:
496: stObj=getattr(ctx.temp_folder,idTmp)
497: logging.info("start, upload thread")
498: self.result="<html><body><h2>I got your file, start now to split it into single atf-files!</h2><p>"
499:
500: #make sure that id is a string and not an integer
501: basketId=str(basketId)
502:
503: #TODO: make this configurable, at the moment, rootFolder for cdli has to be cdliRoot
504: ctx2=ctx.cdliRoot
505:
506: #get temporary file for staging the downloaded and splitted files
507: dir=mkdtemp()
508:
509:
510: changed=[] # changed files
511: errors=[] # files with errors
512: lockerrors=[] # files with errors
513:
514: newPs=[] # new p filed
515: psNotInCatalog=[] # files not in the catalog
516:
517: #split the uploadedd atf file
518: basketNameFromFile, numberOfFiles=splitatf(upload,dir,ext=self)
519:
520: #find basketId if not set
521:
522: #get active abaket
523: if basketId == '0':
524: basketObj=ctx2.basketContainer.getActiveBasket()
525: if basketObj:
526: basketId=basketObj.getId()
527:
528: #if there is no active basket and no basketid given, id is empty, else get besketname and length
529: if basketId == '0':
530: basketNameFromId=""
531: basketLen=0
532: else:
533: basketNameFromId=getattr(ctx2.basketContainer,basketId).title
534: basketLen=getattr(ctx2.basketContainer,basketId).getLastVersion().numberOfItems()
535:
536: logging.info("got the file, upload thread")
537: self.result+="""<html><body><h2>I got the files</h2><
538: p>I am computing the differences to the exisiting files</p>"""
539:
540: #start to check the files
541: for fn in os.listdir(dir):
542:
543: self.result="<p>process:%s</p>"%fn+self.result
544:
545: # check if file is in the catalog
546: #TODO: checkCatalog is not implemented yet
547: if ctx2.cdli_main.checkCatalog(fn):
548: psNotInCatalog.append(fn)
549:
550: #check if p-file already at the server
551: founds=ctx2.CDLICatalog.search({'title':fn})
552:
553: #if not than add filename to the list of newfiles
554:
555: data=file(os.path.join(dir,fn)).read()
556: status,msg=checkFile(fn,data,dir)
557: #status=True
558:
559:
560: if not status: # error
561: errors.append((fn,msg))
562:
563: else:
564: if len(founds)==0:
565: newPs.append(fn)
566:
567: #if p file alread at the server
568: for found in founds:
569: #analyse the differences to the actual file
570: obj=found.getObject()
571:
572: if (not (str(obj.lockedBy))=='') and (not (str(obj.lockedBy)==str(self.username))):
573: lockerrors.append((fn,str(obj.lockedBy)))
574: else:
575:
576: diffs=obj.diff(data)
577: if diffs[0]>0:
578: changed.append((obj,diffs)) #hochladen
579:
580: #ready, set the returnValues
581: self.result+="<h3>Done</h3></body></html>"
582:
583: stObj.returnValue={}
584:
585: stObj.returnValue['errors']=errors
586:
587: stObj.returnValue['newPs']=newPs
588: stObj.returnValue['tmpdir']=dir
589: stObj.returnValue['basketLen']=basketLen
590: stObj.returnValue['numberOfFiles']=numberOfFiles
591: stObj.returnValue['basketNameFromId']=basketNameFromId
592: stObj.returnValue['basketNameFromFile']=basketNameFromFile
593: stObj.returnValue['basketId']=basketId
594: stObj.returnValue['dir']=dir
595: #stObj.returnValue['changed']=copy.copy(changed)
596: stObj.returnValue['changed']=[(x[0].getId(),x[1][0]) for x in changed]
597: #stObj.returnValue['lockerrors']=[x[0].getId() for x in lockerrors]
598: stObj.returnValue['lockerrors']=[x for x in lockerrors]
599: self.returnValue=True
600: #ctx2.cdli_main.setTemp('v_uploadATF_returnValue',True)
601:
602:
603: class CDLIBasketContainer(OrderedFolder):
604: """contains the baskets"""
605:
606:
607: security=ClassSecurityInfo()
608: meta_type="CDLIBasketContainer"
609:
610: def getPNumbersOfBasket(self,basketName):
611: """get all pnumbers of a basket as a list, returns an empty list if basket not found
612: @param basketName: name of the basket
613: """
614: ret=[]
615: basketId=self.getBasketIdfromName(basketName)
616: if not basketId:
617: return []
618:
619: ob=getattr(self,basketId).getContent()
620:
621: ret=[x[0].split(".")[0] for x in ob]
622:
623: return ret
624:
625: security.declareProtected('manage','getBasketAsOneFile')
626: def getBasketAsOneFile(self,basketName,current="no"):
627: """returns all files of the basket combined in one file
628: @param basketName: Name of the basket
629: @param current: (optional) if current is set to "yes" then the most current version of
630: all files are downloaded and not the versions of the files as stored in the basket
631: """
632: ret=""
633: basketId=self.getBasketIdfromName(basketName)
634: if not basketId:
635: return ""
636:
637: ob=getattr(self,basketId).getLastVersion()
638: for object in ob.getContent():
639: if current=="no": #version as they are in the basket
640: ret+=str(object[0].getData())+"\n"
641: elif current=="yes":
642: #search current object
643: logging.debug("current: %s"%object[1].getId().split(".")[0])
644: founds=self.CDLICatalog.search({'title':object[1].getId().split(".")[0]})
645: if len(founds)>0:
646: ret+=str(founds[0].getObject().getLastVersion().getData())+"\n"
647: return ret
648:
649: security.declareProtected('manage','upDateBaskets')
650: def upDateBaskets(self):
651: """update content in to objects"""
652:
653: founds=self.ZopeFind(self,obj_metatypes=['CDLIBasketVersion'],search_sub=1)
654:
655: for found in founds:
656: found[1].updateBasket()
657:
658: security.declareProtected('manage','deleteBaskets')
659: def deleteBaskets(self,ids=None):
660: """delete baskets, i.e. move them into trash folder"""
661:
662:
663: found=self.ZopeFind(self,obj_ids=['trash'])
664:
665: if len(found)<1:
666: manage_addFolder(self, 'trash')
667: trash=self._getOb('trash')
668: else:
669: trash=found[0][1]
670:
671: if type(ids) is not ListType:
672: ids=[ids]
673: cut=self.manage_cutObjects(ids)
674: trash.manage_pasteObjects(cut)
675:
676: security.declareProtected('manage','manageBaskets')
677: def manageBaskets(self,submit,ids=None,basket1="",basket2="",joinBasket="",subtractBasket="",REQUEST=None,RESPONSE=None):
678: """manage baskets, delete or copy"""
679: if submit=="delete":
680: self.deleteBaskets(ids)
681:
682: elif submit=="join":
683: flag,msg=self.joinBasket(joinBasket, ids)
684: logging.info("joining %s %s"%(flag,msg))
685:
686: elif submit=="subtract":
687: logging.info("BBBb %s %s"%(basket1,basket2))
688: flag,msg=self.subtractBasket(subtractBasket, basket1,basket2)
689: logging.info("subtract %s %s"%(flag,msg))
690:
691: if RESPONSE:
692: RESPONSE.redirect(self.absolute_url())
693:
694: security.declareProtected('View','getBasketIdfromName')
695: def getBasketIdfromName(self,basketname):
696: """get id from name"""
697:
698: for basket in self.ZopeFind(self,obj_metatypes=["CDLIBasket"]):
699: if basket[1].title==basketname:
700: return basket[0]
701: else:
702: None
703:
704: security.declareProtected('manage','uploadBasket_html')
705:
706: def uploadBasket_html(self,basketId='0'):
707: """upload an atf file, html form"""
708:
709:
710: basketId=str(basketId)
711: if not basketId=='0':
712: basketName=getattr(self.basketContainer,basketId).title
713: else:
714: basketName=""
715:
716: pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','uploadBasket_html.zpt')).__of__(self)
717: return pt(basketId=basketId,basketName=basketName)
718:
719:
720: security.declareProtected('manage','index_html')
721: def index_html(self):
722: """stanadard ansicht"""
723:
724:
725:
726: ext=self.ZopeFind(self,obj_ids=["index.html"])
727: if ext:
728: return ext[0][1]()
729:
730: pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','BasketContainerMain')).__of__(self)
731: return pt()
732:
733: def getStorageFolderRoot(self):
734: """root des storage folders"""
735: return self.cdli_main
736:
737: def __init__(self,id,title):
738: """ init basket container"""
739: self.id=id
740: self.title=title
741:
742:
743: def getBasketsId(self):
744: """get all baskets als klartext"""
745:
746: ret=""
747: baskets=self.ZopeFind(self,obj_metatypes=['CDLIBasket'])
748: for basket in baskets:
749: com,user,time,values = basket[1].getContentIds()
750: ret+= "BASKET:"+com+"\t"+user+"\t"+time+"\n"
751: for x in values:
752: ret+= x[0]+"\t"+x[1]+"\n"
753: return ret
754:
755: def getBaskets(self,sortField='title'):
756: """get all baskets files"""
757:
758: def sortName(x,y):
759: return cmp(x[1].title.lower(),y[1].title.lower())
760:
761: def sortDate(x,y):
762: return cmp(y[1].getLastVersion().getTime(),x[1].getLastVersion().getTime())
763:
764:
765: def sortComment(x,y):
766:
767:
768:
769: try:
770: xc=getattr(x[1],'comment','ZZZZZZZZZZZZZ').lower()
771: except:
772: xc='ZZZZZZZZZZZZZ'.lower()
773: try:
774: yc=getattr(y[1],'comment','ZZZZZZZZZZZZZ').lower()
775: except:
776: yc='ZZZZZZZZZZZZZ'.lower()
777:
778:
779: if (xc=='') or (xc=='ZZZZZZZZZZZZZ'.lower()):
780:
781: try:
782: xc=x[1].getLastVersion().getComment().lower()
783: except:
784: xc='ZZZZZZZZZZZZZ'.lower()
785:
786: if (yc=='') or (yc=='ZZZZZZZZZZZZZ'.lower()):
787: try:
788: yc=y[1].getLastVersion().getComment().lower()
789: except:
790: yc='ZZZZZZZZZZZZZ'.lower()
791:
792:
793: return cmp(xc,yc)
794:
795: def sortAuthor(x,y):
796:
797: return cmp(x[1].getLastVersion().getUser().lower(),y[1].getLastVersion().getUser().lower())
798:
799: baskets=self.ZopeFind(self,obj_metatypes=['CDLIBasket'])
800:
801:
802: if sortField=='title':
803: baskets.sort(sortName)
804: elif sortField=='date':
805: baskets.sort(sortDate)
806: elif sortField=='author':
807: baskets.sort(sortAuthor)
808: elif sortField=='comment':
809: baskets.sort(sortComment)
810:
811: return baskets
812:
813:
814: def subtractBasket(self,newBasket,basket1,basket2):
815: """subtract basket2 from basket1
816: (i.e. newbasket will contain alle elements of basket1 which are not in basket2),
817: if basket2 contains files which are not in basket1, then theses files fill be ignored
818:
819: @param newbasket: name of the new basket
820: @param basket1: basket where basket2 will be subtracted from
821: @param basket2: see above
822:
823: """
824: logging.info("CCCCC %s %s"%(basket1,basket2))
825:
826: try:
827: newB=self.addBasket(newBasket)
828: except:
829: return False, "cannot create the new basket"
830:
831:
832:
833:
834:
835: bas2= getattr(self,basket2)
836: bas2content=bas2.getContent()
837: bas2ids=[x[0] for x in bas2content]
838:
839:
840:
841: bas1= getattr(self,basket1)
842: bas1content=bas1.getContent()
843:
844:
845: newBasketContent={}
846:
847: for id,version in bas1content:
848: if not (id in bas2ids):
849: newBasketContent[id]=version
850:
851: username=self.getActualUserName()
852:
853: logging.info("sbc %s"%newBasketContent)
854: newB.addObjectsWithVersion(newBasketContent,username=username,catalog=self.CDLICatalog)
855:
856: return True, ""
857:
858:
859: def joinBasket(self,newBasket,oldBaskets):
860: """join two baskets
861: @param newbasket: name of the new basket
862: @param oldbaskets: list of baskets to be joined
863: """
864: try:
865: newB=self.addBasket(newBasket)
866: except:
867: return False, "cannot create the new basket"
868:
869: newBasketContent={}
870: for ob in oldBaskets:
871: x= getattr(self,ob,None)
872: if x is None:
873: return False, "cannot find basket: %s"%ob
874:
875: ids=x.getContent() # hole den Inhalt
876:
877: for id,version in ids:
878: if newBasketContent.has_key(id): # p number gibt's schon
879: newBasketContent[id]=max(newBasketContent[id],version) # speichere die groessere Versionsnumber
880: else:
881: newBasketContent[id]=version
882: username=self.getActualUserName()
883:
884: logging.info("nbc %s"%newBasketContent)
885: newB.addObjectsWithVersion(newBasketContent,username=username,catalog=self.CDLICatalog)
886:
887: return True, ""
888:
889: def getNewId(self):
890: """createIds"""
891: last=getattr(self,'last',0)
892: last +=1
893: while len(self.ZopeFind(self,obj_ids=[str(last)]))>0:
894: last+=1
895:
896: self.last=last
897: return last
898:
899: def setActiveBasket(self,basketId,REQUEST=None):
900: """store active basketId in a cookie"""
901: self.REQUEST.RESPONSE.setCookie("CDLIActiveBasket",basketId,path="/")
902: try:
903: qs=cgi.parse_qs(REQUEST['QUERY_STRING'])
904: del(qs['basketId'])
905: except:
906: qs={}
907: if REQUEST:
908: REQUEST.RESPONSE.redirect(REQUEST['URL1']+'?'+urllib.urlencode(qs))
909:
910: def getActiveBasket(self):
911: """get active basket from cookie"""
912:
913: id= self.REQUEST.cookies.get('CDLIActiveBasket',None)
914: if id:
915: obj=getattr(self,str(id),None)
916: else:
917: obj=None
918: return obj
919:
920: def getActualUserName(self):
921: """get name of the actualuser"""
922: return str(self.REQUEST['AUTHENTICATED_USER'])
923:
924: security.declareProtected('manage','addBasket')
925: def addBasket(self,newBasketName):
926: """add a new basket"""
927:
928: ob=manage_addCDLIBasket(self,newBasketName)
929: return ob
930:
931: def storeInBasket(self,submit,ids=None,newBasketName=None,fromFileList=None,RESPONSE=None,REQUEST=None):
932: """store it"""
933: if not ids:
934: ids=self.REQUEST.SESSION['fileIds']
935:
936: if (type(ids) is not ListType) and (not isinstance(ids,Set)):
937: ids=[ids]
938:
939: if isinstance(ids,Set):
940: ids=list(ids)
941:
942: if (submit.lower()=="store in new basket") or (submit.lower()=="new basket"):
943: basketRet=self.addBasket(newBasketName)
944: self.setActiveBasket(basketRet.getId())
945: basket=getattr(self,basketRet.getId())
946: elif (submit.lower()=="store in active basket") or (submit.lower()=="active basket"):
947: basket=self.getActiveBasket()
948:
949: added=basket.addObjects(ids)
950: back=self.REQUEST['HTTP_REFERER'].split("?")[0]+"?basketName="+basket.title+"&numberOfObjects="+str(added)
951:
952:
953: if fromFileList:
954:
955: return self.cdli_main.findObjectsFromList(list=ids,basketName=basket.title,numberOfObjects=added)
956:
957: if RESPONSE:
958:
959: RESPONSE.redirect(back)
960:
961: return True
962:
963: def manage_addCDLIBasketContainerForm(self):
964: """add the CDLIBasketContainer form"""
965: pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','addCDLIBasketContainer.zpt')).__of__(self)
966: return pt()
967:
968: def manage_addCDLIBasketContainer(self,id,title,RESPONSE=None):
969: """add the basket"""
970: ob=CDLIBasketContainer(id,title)
971:
972: self._setObject(id, ob)
973:
974: if RESPONSE is not None:
975: RESPONSE.redirect('manage_main')
976:
977: class CDLIBasket(Folder,CatalogAware):
978: """basket"""
979:
980: meta_type="CDLIBasket"
981: default_catalog="CDLIBasketCatalog"
982:
983: def searchInBasket(self,indexName,searchStr,regExp=False):
984: """searchInBasket"""
985:
986: lst=self.searchInLineIndexDocs(indexName,searchStr,uniq=True,regExp=regExp) #TODO: fix this
987: ret={}
988:
989: lv=self.getLastVersion()
990:
991:
992: for obj in lv.content.getContent():
993: id=obj[1].getId().split(".")[0]
994: if id in lst:
995:
996: ret[id]=self.showWordInFile(id,searchStr,lineList=self.getLinesFromIndex(indexName,searchStr,id,regExp=regExp),regExp=regExp,indexName=indexName)
997:
998:
999: pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','searchResultsInBasket')).__of__(self)
1000: return pt(result=ret,indexName=indexName,regExp=regExp,word=searchStr)
1001:
1002:
1003:
1004:
1005: def searchInBasket_v1(self,searchStr):
1006: """search occurences of searchStr in files im basket"""
1007: ret=[]
1008: lv=self.getLastVersion()
1009: logging.info("searching")
1010: for obj in lv.content.getContent():
1011: txt=obj[0].getData()
1012: for x in txt.split("\n"):
1013: logging.info("search %s"%x)
1014: if re.match(searchStr,x):
1015: ret.append(x)
1016:
1017: return "\n".join(ret)
1018:
1019:
1020: def getFile(self,obj):
1021: return obj[1]
1022:
1023: def getFileLastVersion(self,obj):
1024: return obj[0]
1025:
1026: def getFileNamesInLastVersion(self):
1027: """get content of the last version as list"""
1028:
1029: return [x[1].getId() for x in self.getLastVersion().getContent()]
1030:
1031:
1032: def isActual(self,obj):
1033: """teste ob im basket die aktuelle version ist"""
1034: try:
1035: logging.debug("isActual:"+repr(obj))
1036: actualNo=obj[1].getLastVersion().getVersionNumber()
1037: storedNo=obj[0].getVersionNumber()
1038:
1039:
1040: actualNo=self.getFileObjectLastVersion(obj.getId()).getVersionNumber()
1041:
1042: #if len(founds)>0 and founds[0].getObject().aq_parent.getId()==".trash":
1043: # return False, -1
1044:
1045: if actualNo==storedNo:
1046: return True , 0
1047: else:
1048: return False, actualNo
1049: except:
1050: return False, -1
1051:
1052: def history(self):
1053: """history"""
1054:
1055: ext=self.ZopeFind(self.aq_parent,obj_ids=["history_template.html"])
1056: if ext:
1057: return getattr(self,ext[0][1].getId())()
1058:
1059: pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','BasketHistory')).__of__(self)
1060: return pt()
1061:
1062: def getStorageFolderRoot(self):
1063: """root des storage folders"""
1064: return self.aq_parent.cdli_main
1065:
1066: def __init__(self,id,title,shortDescription="",comment=""):
1067: """init a CDLIBasket"""
1068:
1069: self.id=id
1070: self.title=title
1071: self.shortDescription=shortDescription
1072: self.comment=comment
1073:
1074: def getActualUserName(self):
1075: """get name of the actualuser"""
1076:
1077: return str(self.REQUEST['AUTHENTICATED_USER'])
1078:
1079:
1080: def getLastVersion(self):
1081: """hole letzte version"""
1082:
1083: ids=[]
1084: idsTmp= self.objectIds()
1085: for x in idsTmp:
1086: try:
1087: ids.append(int(x))
1088: except:
1089: pass
1090: ids.sort()
1091:
1092: if len(ids)==0:
1093: return None
1094: else:
1095: ob=getattr(self,str(ids[-1]))
1096:
1097:
1098: return ob
1099:
1100: def getVersions(self):
1101: """get versions"""
1102: versions=self.ZopeFind(self,obj_metatypes=["CDLIBasketVersion"])
1103: return versions
1104:
1105:
1106: def updateObjects(self,ids,RESPONSE=None,REQUEST=None):
1107: """update ids, ids not in the basket the add"""
1108: if type(ids) is not ListType:
1109: ids=[ids]
1110:
1111: lastVersion=self.getLastVersion()
1112: oldContent=lastVersion.content.getContent()
1113: newContent=[]
1114:
1115: #first copy the old
1116: for obj in oldContent:
1117: if obj[1].getId() not in ids:
1118: newContent.append(obj)
1119: #now add the new
1120:
1121: for id in ids:
1122: founds=self.CDLICatalog.search({'title':id})
1123:
1124: for found in founds:
1125: if found.getObject() not in oldContent:
1126: #TODO: was passiert wenn, man eine Object dazufŸgt, das schon da ist aber eine neuere version
1127: newContent.append((found.getObject().getLastVersion(),found.getObject()))
1128:
1129:
1130: content=newContent
1131: user=self.getActualUserName()
1132:
1133: ob=manage_addCDLIBasketVersion(self,user,comment="",basketContent=newContent)
1134:
1135: obj=self._getOb(ob.getId())
1136: if RESPONSE:
1137:
1138: RESPONSE.redirect(obj.absolute_url())
1139:
1140: return obj
1141:
1142: def addObjectsWithVersion(self,ids,deleteOld=None,username=None,catalog=None):
1143: """generate a new version of the basket with objects added,
1144: hier wird jedoch nicht die letzte Version jedes Files hinzugefuegt, s
1145: ondern ids is ein Tupel mit der Id (d.h. der p-number) und der Versionsnummer.
1146: """
1147: logging.info("add to basket (%s)"%(self.getId()))
1148: lastVersion=self.getLastVersion()
1149:
1150: if not catalog:
1151: catalog=self.CDLICatalog
1152:
1153: if lastVersion is None:
1154: oldContent=[]
1155: else:
1156: oldContent=lastVersion.content.getContent()
1157:
1158: if deleteOld:
1159: oldContent=[]
1160:
1161: newContent=[]
1162: added=0
1163:
1164: for id,version in ids.iteritems():
1165: logging.info("adding %s %s"%(id,version))
1166: id=id.split(".")[0] # title nur die pnumber ohne atf
1167:
1168: try:
1169: founds=catalog.search({'title':id})
1170: except:
1171: founds=[]
1172: logging.info(" found %s "%(founds))
1173: for found in founds:
1174: if found.getObject() not in oldContent:
1175:
1176: #TODO: was passiert wenn, man eine Object dazufŸgt, das schon da ist aber eine neuere version
1177: newContent.append((found.getObject().getVersions()[version-1][1],found.getObject()))
1178: added+=1
1179:
1180: content=oldContent+newContent
1181: if not username:
1182: logging.error("XXXXXXXXXXX %s"%repr(self))
1183: user=self.getActualUserName()
1184: else:
1185: user = username
1186:
1187: ob=manage_addCDLIBasketVersion(self,user,comment="",basketContent=content)
1188: logging.info("add to basket (%s) done"%(self.getId()))
1189: return added
1190:
1191:
1192: def addObjects(self,ids,deleteOld=None,username=None):
1193: """generate a new version of the basket with objects added"""
1194:
1195: def swap(x):
1196: return (x[1],x[0])
1197:
1198: logging.info("add to basket (%s)"%(self.getId()))
1199: lastVersion=self.getLastVersion()
1200:
1201: if lastVersion is None:
1202: oldContent=[]
1203: else:
1204: oldContent=lastVersion.content.getContent()
1205:
1206: if deleteOld:
1207: oldContent=[]
1208:
1209: added=0
1210: # for id in ids:
1211: # logging.debug("adding:"+id)
1212: # try:
1213: # founds=self.CDLICatalog.search({'title':id})
1214: # except:
1215: # founds=[]
1216: #
1217: # for found in founds:
1218: # if found.getObject() not in oldContent:
1219: # #TODO: was passiert wenn, man eine Object dazufŸgt, das schon da ist aber eine neuere version
1220: # newContent.append((found.getObject().getLastVersion(),found.getObject()))
1221: # added+=1
1222:
1223: hash = md5.new(repr(makelist(ids))).hexdigest() # erzeuge hash als identification
1224: #logging.debug("JJJJJJJ:"+repr(self.makelist(ids)))
1225: logging.debug("JJJJJJJ:"+repr(hash))
1226:
1227: if hasattr(self.cdliRoot,'v_tmpStore') and self.cdliRoot.v_tmpStore.has_key(hash):
1228: logging.debug("from store!")
1229: newContent=Set(map(swap,self.cdliRoot.v_tmpStore[hash]))
1230:
1231: else:
1232: logging.debug("not from store!")
1233: newContent=Set([(self.getFileObjectLastVersion(x),self.getFileObject(x)) for x in ids])
1234:
1235:
1236: content=Set(oldContent).union(newContent)
1237: added = len(content)-len(oldContent)
1238: if not username:
1239: user=self.getActualUserName()
1240: else:
1241: user = username
1242:
1243: #logging.debug("content:"+repr(list(content)))
1244: ob=manage_addCDLIBasketVersion(self,user,comment="",basketContent=list(content))
1245: logging.info("add to basket (%s) done"%(self.getId()))
1246: return added
1247:
1248:
1249:
1250: def getContent(self):
1251: """print content"""
1252: ret=[]
1253:
1254: lv=self.getLastVersion()
1255: for obj in lv.content.getContent():
1256: logging.info("XXXXXXXXXX %s"%repr(obj))
1257: ret.append((obj[1].getId(),obj[0].versionNumber))
1258:
1259: return ret
1260:
1261: def getContentIds(self):
1262: """print basket content"""
1263: ret=[]
1264: lv=self.getLastVersion()
1265: for obj in lv.content.getContent():
1266: ret.append((obj[0].getId(),obj[1].getId()))
1267:
1268:
1269: return lv.getComment(),lv.getUser(),lv.getTime(),ret
1270:
1271: def changeBasket(self,ids,submit,RESPONSE=None,REQUEST=None):
1272: """change a basket"""
1273: if submit=="update":
1274: return self.updateObjects(ids,RESPONSE=RESPONSE,REQUEST=REQUEST)
1275: elif submit=="delete":
1276: return self.deleteObjects(ids,RESPONSE=RESPONSE,REQUEST=REQUEST)
1277:
1278: def deleteObjects(self,ids,RESPONSE=None,REQUEST=None):
1279: """delete objects"""
1280:
1281: if type(ids) is not ListType:
1282: ids=[ids]
1283:
1284: lastVersion=self.getLastVersion()
1285: oldContent=lastVersion.content.getContent()
1286: newContent=[]
1287: for obj in oldContent:
1288: if obj[1].getId() not in ids:
1289: newContent.append(obj)
1290:
1291:
1292: user=self.getActualUserName()
1293:
1294: ob=manage_addCDLIBasketVersion(self,user,comment="",basketContent=newContent)
1295:
1296: if RESPONSE:
1297: obj=self._getOb(ob.getId())
1298: RESPONSE.redirect(obj.absolute_url())
1299:
1300: def manage_addCDLIBasketForm(self):
1301: """add the CDLIBasketContainer form"""
1302: pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','addCDLIBasket.zpt')).__of__(self)
1303: return pt()
1304:
1305: def manage_addCDLIBasket(self,title,shortDescription="",comment="",RESPONSE=None):
1306: """add the basket"""
1307:
1308: id=str(self.getNewId())
1309:
1310: ob=CDLIBasket(id,title,shortDescription,comment)
1311:
1312: self._setObject(id, ob)
1313:
1314: if RESPONSE is not None:
1315: RESPONSE.redirect('manage_main')
1316: else:
1317: return ob
1318:
1319: class CDLIBasketVersion(Implicit,Persistent,Folder):
1320: """version of a basket"""
1321:
1322: meta_type="CDLIBasketVersion"
1323: security=ClassSecurityInfo()
1324:
1325: def updateBasket(self):
1326: """update"""
1327: try:
1328: self._setObject('content',BasketContent(self.basketContent))
1329: except:
1330: try:
1331: if len(self.basketContent)>0:
1332: self.content.setContent(self.basketContent)
1333: except:
1334: print "error",self.getId(),self.aq_parent.getId()
1335: self.basketContent=[]
1336:
1337:
1338: def containsNonActualFiles(self):
1339: """returns True if basket contains one or more non current files"""
1340:
1341: objs=self.getContent()
1342: for obj in objs:
1343: if not self.isActual(obj)[0]:
1344: return True
1345: return False
1346:
1347: def downloadListOfPnumbers(self):
1348: """download pnumbers of the basket as list"""
1349:
1350: basket_name=self.aq_parent.title
1351:
1352: ids=self.getContent() # get the list of objects
1353: logging.error(ids)
1354: ret="\n".join([x[1].getId().split(".")[0] for x in ids])
1355:
1356: self.REQUEST.RESPONSE.setHeader("Content-Disposition","""attachement; filename="%s.txt" """%basket_name)
1357: self.REQUEST.RESPONSE.setHeader("Content-Type","application/octet-stream")
1358: length=len(ret)
1359: self.REQUEST.RESPONSE.setHeader("Content-Length",length)
1360: self.REQUEST.RESPONSE.write(ret)
1361:
1362: security.declareProtected('manage','downloadObjectsAsOneFile')
1363: def downloadObjectsAsOneFile(self,lock=None,procedure=None,REQUEST=None,check="yes",current="no"):
1364: """download all selected files in one file"""
1365: logging.debug("doasof")
1366: if self.temp_folder.downloadCounterBaskets > 10000:
1367: return """I am sorry, currently the server has to many requests for downloads, please come back later!"""
1368:
1369:
1370: if (check=="yes") and self.containsNonActualFiles():
1371: pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','downloadObjectAsOneFile_check.zpt')).__of__(self)
1372:
1373: return pt(lock=lock)
1374:
1375: else:
1376:
1377: return self.downloadObjectsAsOneFileFinally(lock=lock,procedure=procedure,REQUEST=REQUEST,current="no")
1378:
1379: def downloadObjectsAsOneFileFinally(self,lock=None,procedure=None,REQUEST=None,current="no",repeat=None):
1380: """print do the download"""
1381:
1382: logging.debug("HIHHHH")
1383: ret=""
1384: lockedObjects={}
1385:
1386:
1387: logging.debug("lock:"+repr(lock))
1388: if lock:
1389: logging.debug("------lock:"+repr(lock))
1390: if str(self.REQUEST['AUTHENTICATED_USER'])=='Anonymous User':
1391:
1392: return "please login first"
1393:
1394: #check if a locked object exist in the basket.
1395: lockedObjects={}
1396: for object in self.content.getContent():
1397:
1398: if (not str(object[1].lockedBy)=="") and (not (str(object[1].lockedBy)==str(self.REQUEST['AUTHENTICATED_USER']))):
1399: lockedObjects[object[1].title]=repr(object[1].lockedBy)
1400:
1401:
1402: keys=lockedObjects.keys()
1403:
1404:
1405: if len(keys)>0 and (not procedure):
1406: self.REQUEST.SESSION['lockedObjects']=lockedObjects
1407: pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','lockedObjects.zpt')).__of__(self)
1408:
1409:
1410: return pt()
1411:
1412: elif not procedure: #keine fails gesperrt dann alle donwloaden
1413: procedure="downloadAll"
1414:
1415:
1416:
1417:
1418: threadName=repeat
1419: if not threadName or threadName=="":
1420: thread=DownloadBasketFinallyThread()
1421: threadName=thread.getName()[0:]
1422:
1423: if (not hasattr(self,'_v_downloadBasket')):
1424: self._v_downloadBasket={}
1425:
1426:
1427: self._v_downloadBasket[threadName]=thread
1428: logging.debug("dwonloadfinally:"+repr(self))
1429: basketID=self.aq_parent.aq_parent.getId()
1430: versionNumber=self.aq_parent.getId()
1431:
1432: if lock:
1433: logging.debug("-----start locking")
1434: for object in self.content.getContent():
1435: if object[1].lockedBy =='':
1436: object[1].lockedBy=self.REQUEST['AUTHENTICATED_USER']
1437: logging.debug("-----finished locking")
1438:
1439: #obj.lockedBy=user
1440: self._v_downloadBasket[threadName].set(lock,procedure,self.REQUEST['AUTHENTICATED_USER'],current,basketID,versionNumber)
1441:
1442: self._v_downloadBasket[threadName].start()
1443:
1444:
1445:
1446: wait_template=self.aq_parent.ZopeFind(self.aq_parent,obj_ids=['wait_template'])
1447:
1448: if wait_template:
1449: return wait_template[0][1]()
1450: pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','downloadBasketWait.zpt')).__of__(self)
1451:
1452: return pt(txt=self.absolute_url()+'/downloadObjectsAsOneFileFinally',threadName=threadName,
1453: counter=self._v_downloadBasket[threadName].getCounter(),
1454: number=self._v_downloadBasket[threadName].getNumberOfFiles())
1455: #_v_xmltrans.run()
1456:
1457: else:
1458: #recover thread, if lost
1459: if not hasattr(self,'_v_downloadBasket'):
1460: self._v_downloadBasket={}
1461: if not self._v_downloadBasket.get(threadName,None):
1462: for thread in threading.enumerate():
1463: if threadName == thread.getName():
1464: self._v_downloadBasket[threadName]=thread
1465:
1466: if self._v_downloadBasket.get(threadName,None) and (self._v_downloadBasket[threadName] is not None) and (not self._v_downloadBasket[threadName].end) :
1467:
1468: wait_template=self.aq_parent.ZopeFind(self.aq_parent,obj_ids=['wait_template'])
1469: if wait_template:
1470: return wait_template[0][1]()
1471:
1472: pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','downloadBasketWait.zpt')).__of__(self)
1473: return pt(txt=self.absolute_url()+'/downloadObjectsAsOneFileFinally',threadName=threadName,
1474: counter=self._v_downloadBasket[threadName].getCounter(),
1475: number=self._v_downloadBasket[threadName].getNumberOfFiles())
1476: else:
1477:
1478:
1479: logging.debug("FINISHED")
1480: if not self._v_downloadBasket.get(threadName,None):
1481: for thread in threading.enumerate():
1482: if threadName == thread.getName():
1483: self._v_downloadBasket[threadName]=thread
1484:
1485: #files = self._v_downloadBasket[threadName].result
1486: files=self.basketContainer.resultHash[threadName]
1487: # fh=file("/var/tmp/test")
1488: #ret =fh.read()
1489:
1490: if (not isinstance(self.aq_parent,CDLIBasket)):
1491: basket_name=self.aq_parent.aq_parent.title+"_V"+self.getId()
1492: else:
1493: basket_name=self.aq_parent.title+"_V"+self.getId()
1494:
1495:
1496:
1497: #write basketname to header of atf file
1498:
1499:
1500: self.REQUEST.RESPONSE.setHeader("Content-Disposition","""attachement; filename="%s.atf" """%basket_name)
1501: self.REQUEST.RESPONSE.setHeader("Content-Type","application/octet-stream")
1502: #length=len(ret)
1503: #self.REQUEST.RESPONSE.setHeader("Content-Length",length)
1504: ret="#basket: %s\n"%basket_name
1505: self.REQUEST.RESPONSE.write(ret)
1506: for fileName in files:
1507: self.REQUEST.RESPONSE.write(file(fileName).read())
1508:
1509: del self.basketContainer.resultHash[threadName]
1510:
1511: def numberOfItems(self):
1512: """return anzahl der elemente im basket"""
1513: return self.content.numberOfItems()
1514:
1515: def getTime(self):
1516: """getTime"""
1517: #return self.bobobase_modification_time().ISO()
1518:
1519: if hasattr(self,'time'):
1520: return time.strftime("%Y-%m-%d %H:%M:%S",self.time)
1521: elif hasattr(self,'timefixed'):
1522: return self.timefixed
1523: else:
1524: setattr(self,'timefixed',self.bobobase_modification_time().ISO())
1525: return self.bobobase_modification_time().ISO()
1526:
1527: def getContent(self):
1528: """get Basket Content"""
1529: return self.content.getContent()
1530:
1531:
1532: def __init__(self,id,user,comment="",basketContent=[]):
1533: """ init a basket version"""
1534: self.id=id
1535: self.comment=comment
1536: self._setObject('content',BasketContent(basketContent))
1537: #self.basketContent=basketContent[0:]a
1538: self.user=user
1539: self.time=time.localtime()
1540:
1541: def getUser(self):
1542: """get user"""
1543: return self.user
1544:
1545: def getComment(self):
1546: """get Comment"""
1547: return self.comment
1548:
1549: security.declareProtected('manage','index_html')
1550: def index_html(self):
1551: """view the basket"""
1552:
1553: if self.REQUEST.get('change',False):
1554: ob=self.aq_parent.updateObjects(self.REQUEST['change'])
1555:
1556: self.REQUEST.RESPONSE.redirect(ob.absolute_url())#go to new basket, because changing generates a new basket
1557:
1558: pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','BasketVersionMain.zpt')).__of__(self)
1559: return pt()
1560:
1561: def getObjUrl(self,result):
1562: """getUrl of the version of the object"""
1563: objId=result[1].getTitle()
1564: founds=self.CDLICatalog.search({'title':objId})
1565: if len(founds)>0:
1566: return founds[0].getObject().getLastVersion().absolute_url()
1567:
1568: else: #assume version number
1569: splitted=objId.split("_")
1570: founds=self.CDLICatalog.search({'title':splitted[1]})
1571: return founds[0].getObject().getLastVersion().absolute_url()+'/'+objId
1572:
1573: def manage_addCDLIBasketVersion(self,user,comment="",basketContent=[],RESPONSE=None):
1574: """add a version"""
1575:
1576: #check for already existing versions
1577:
1578: lastVersion=self.getLastVersion()
1579: if lastVersion is None:
1580: newId=str(1)
1581: else:
1582: newId=str(int(lastVersion.getId())+1)
1583:
1584: ob=CDLIBasketVersion(newId,user,comment,basketContent)
1585:
1586: self._setObject(newId, ob)
1587:
1588: if RESPONSE is not None:
1589: RESPONSE.redirect('manage_main')
1590: else:
1591: return ob
1592:
1593: class CDLIFileObject(CatalogAware,extVersionedFileObject):
1594: """CDLI file object"""
1595:
1596: meta_type="CDLI File Object"
1597: default_catalog='CDLIObjectsCatalog'
1598:
1599: security=ClassSecurityInfo()
1600:
1601: security.declareProtected('manage','index_html')
1602:
1603: security.declarePublic('view')
1604: view = PageTemplateFile('zpt/viewCDLIFile.zpt', globals())
1605:
1606: security.declarePublic('editATF')
1607: editATF = PageTemplateFile('zpt/editATFFile.zpt', globals())
1608:
1609: def PrincipiaSearchSource(self):
1610: """Return cataloguable key for ourselves."""
1611: return str(self)
1612:
1613: def setAuthor(self, author):
1614: """change the author"""
1615: self.author = author
1616:
1617: def makeThisVersionCurrent_html(self):
1618: """form for mthis version current"""
1619:
1620: pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','makeThisVersionCurrent.zpt')).__of__(self)
1621: return pt()
1622:
1623: security.declarePublic('makeThisVersionCurrent')
1624: def makeThisVersionCurrent(self,comment,author,RESPONSE=None):
1625: """copy this version to current"""
1626: parent=self.aq_parent
1627: parent.manage_addVersionedFileObject(id=None,vC=comment,author=author,file=self.getData(),RESPONSE=RESPONSE)
1628: #newversion=parent.manage_addCDLIFileObject('',comment,author)
1629: #newversion.manage_upload(self.getData())
1630:
1631: #if RESPONSE is not None:
1632: # RESPONSE.redirect(self.aq_parent.absolute_url()+'/history')
1633:
1634: return True
1635:
1636: def getFormattedData(self):
1637: """fromat text"""
1638: data=self.getData()
1639: # return re.sub("\s\#lem"," #lem",data) #remove return vor #lem
1640: return re.sub("#lem"," #lem",data) #remove return vor #lem
1641:
1642:
1643: security.declarePublic('getPNumber')
1644: def getPNumber(self):
1645: """get the pnumber"""
1646: try:
1647: txt=re.match("&[Pp](\d*)\s*=([^\r\n]*)",self.getData()[0:])
1648: except:
1649: txt=self.getData()[0:]
1650:
1651: return "ERROR"
1652: try:
1653: return "P"+txt.group(1)
1654: except:
1655: return "ERROR"
1656:
1657: security.declarePublic('getDesignation')
1658: def getDesignation(self):
1659: """get the designation out of the file"""
1660: try:
1661: txt=re.match("&[Pp](\d*)\s*=([^\r\n]*)",self.getData()[0:])
1662: except:
1663: txt=self.getData()[0:]
1664:
1665: return "ERROR"
1666: try:
1667: return txt.group(2)
1668: except:
1669: return "ERROR"
1670:
1671:
1672: manage_addCDLIFileObjectForm=DTMLFile('dtml/fileAdd', globals(),Kind='CDLIFileObject',kind='CDLIFileObject', version='1')
1673:
1674: def manage_addCDLIFileObject(self,id,vC='',author='', file='',title='',versionNumber=0,
1675: precondition='', content_type='',
1676: from_tmp=False,REQUEST=None):
1677: """Add a new File object.
1678: Creates a new File object 'id' with the contents of 'file'"""
1679:
1680: id=str(id)
1681: title=str(title)
1682: content_type=str(content_type)
1683: precondition=str(precondition)
1684:
1685: id, title = cookId(id, title, file)
1686:
1687: self=self.this()
1688:
1689: # First, we create the file without data:
1690: self._setObject(id, CDLIFileObject(id,title,versionNumber=versionNumber,versionComment=vC,time=time.localtime(),author=author))
1691: fob = self._getOb(id)
1692:
1693: # Now we "upload" the data. By doing this in two steps, we
1694: # can use a database trick to make the upload more efficient.
1695:
1696: if file and not from_tmp:
1697: fob.manage_upload(file)
1698: elif file and from_tmp:
1699: fob.manage_file_upload(file) # manage_upload_from_tmp doesn't exist in ExtFile2
1700: # fob.manage_upload_from_tmp(file) # manage_upload_from_tmp doesn't exist in ExtFile2
1701: if content_type:
1702: fob.content_type=content_type
1703:
1704: #logging.debug("manage_add: lastversion=%s"%self.getData())
1705: logging.debug("reindex1: %s in %s"%(repr(self),repr(self.default_catalog)))
1706: self.reindex_object()
1707: #logging.debug("manage_add: fob_data=%s"%fob.getData())
1708: logging.debug("reindex2: %s in %s"%(repr(fob), repr(fob.default_catalog)))
1709: fob.index_object()
1710:
1711: self.CDLIRoot.updateOrAddToFileBTree(ob)
1712: if REQUEST is not None:
1713: REQUEST['RESPONSE'].redirect(self.absolute_url()+'/manage_main')
1714:
1715:
1716: class CDLIFile(extVersionedFile,CatalogAware):
1717: """CDLI file"""
1718:
1719: security=ClassSecurityInfo()
1720: meta_type="CDLI file"
1721: content_meta_type = ["CDLI File Object"]
1722:
1723: default_catalog='CDLICatalog'
1724:
1725: security.declareProtected('manage','index_html')
1726:
1727: def getLastVersionData(self):
1728: """get last version data"""
1729: return self.getData()
1730:
1731: def getLastVersionFormattedData(self):
1732: """get last version data"""
1733: return self.getContentObject().getFormattedData()
1734:
1735: def getTextId(self):
1736: """returns P-number of text"""
1737: # assuming that its the beginning of the title
1738: return self.title[:7]
1739:
1740: #security.declarePublic('history')
1741: def history(self):
1742: """history"""
1743:
1744: ext=self.ZopeFind(self.aq_parent,obj_ids=["history_template.html"])
1745: if ext:
1746: return getattr(self,ext[0][1].getId())()
1747:
1748: pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','versionHistory')).__of__(self)
1749: return pt()
1750:
1751:
1752: def getBasketFromId(self,basketid, context=None):
1753: """get basket from id"""
1754:
1755: if not context:
1756: context=self
1757:
1758: for basket in self.ZopeFind(context,obj_metatypes=["CDLIBasket"]):
1759: if basket[0]==basketid:
1760: return basket[1]
1761: else:
1762: None
1763:
1764:
1765: def isContainedInBaskets(self,context=None):
1766: """check is this file is part of any basket
1767: @param context: (optional) necessessary if CDLIBasketCatalog is not an (inherited) attribute of self, context.CDLIBasketCatalog
1768: has to exist.
1769: """
1770:
1771: if not context:
1772: context=self
1773:
1774: ret=[]
1775: for x in context.CDLIBasketCatalog.search({'getFileNamesInLastVersion':self.getId()}):
1776: #if the basket x is deleted it seemes to be that x is sometimes still in the Catalog, why?
1777: try:
1778: ret.append(x.getObject())
1779: except:
1780: pass
1781: return ret
1782: #return [x.getObject() for x in context.CDLIBasketCatalog.search({'getFileNamesInLastVersion':self.getId()})]
1783:
1784:
1785: def _newContentObject(self, id, title='', versionNumber=0, versionComment=None, time=None, author=None):
1786: """factory for content objects. to be overridden in derived classes."""
1787: logging.debug("_newContentObject(CDLI)")
1788: return CDLIFileObject(id,title,versionNumber=versionNumber,versionComment=versionComment,time=time,author=author)
1789:
1790:
1791: def addCDLIFileObjectForm(self):
1792: """add a new version"""
1793:
1794: if str(self.REQUEST['AUTHENTICATED_USER']) in ["Anonymous User"]:
1795: return "please login first"
1796: if (self.lockedBy==self.REQUEST['AUTHENTICATED_USER']) or (self.lockedBy==''):
1797: out=DTMLFile('dtml/fileAdd', globals(),Kind='CDLIFileObject',kind='CDLIFileObject',version=self.getVersion()).__of__(self)
1798: return out()
1799: else:
1800: return "Sorry file is locked by somebody else"
1801:
1802: def manage_addCDLIFileObject(self,id,vC,author,
1803: file='',title='',
1804: precondition='',
1805: content_type='',
1806: changeName='no',newName='',
1807: come_from=None,
1808: from_tmp=False,RESPONSE=None):
1809: """add"""
1810:
1811: try: #TODO: der ganze vC unsinn muss ueberarbeitet werden
1812: vC=self.REQUEST['vC']
1813: except:
1814: pass
1815:
1816: ob = self.addContentObject(id, vC, author, file, title, changeName=changeName, newName=newName, from_tmp=from_tmp,
1817: precondition=precondition, content_type=content_type)
1818:
1819: try:
1820: #FIXME: wozu ist das gut?
1821: self.REQUEST.SESSION['objID_parent']=self.getId()
1822: except:
1823: pass
1824:
1825: #self.cdliRoot.updateOrAddToFileBTree(self)# now update the object in the cache
1826:
1827:
1828: if RESPONSE:
1829: if ob.getSize()==0:
1830: self.REQUEST.SESSION['objID']=ob.getId()
1831: pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','errorUploadFile')).__of__(self)
1832: return pt()
1833: else:
1834: if come_from and (come_from!=""):
1835: RESPONSE.redirect(come_from+"?change="+self.getId())
1836: else:
1837: RESPONSE.redirect(self.REQUEST['URL2']+'?uploaded=%s'%self.title)
1838: else:
1839: return ob
1840:
1841:
1842: def manage_addCDLIFileForm(self):
1843: """interface for adding the OSAS_root"""
1844: pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','addCDLIFile.zpt')).__of__(self)
1845: return pt()
1846:
1847: def manage_addCDLIFile(self,id,title,lockedBy, author=None, RESPONSE=None):
1848: """add the OSAS_root"""
1849: newObj=CDLIFile(id,title,lockedBy,author)
1850:
1851: tryToggle=True
1852: tryCount=0
1853:
1854: self._setObject(id,newObj)
1855: getattr(self,id).reindex_object()
1856:
1857: if RESPONSE is not None:
1858: RESPONSE.redirect('manage_main')
1859:
1860:
1861: def checkUTF8(data):
1862: """check utf 8"""
1863: try:
1864: data.encode('utf-8')
1865: return True
1866: except:
1867: return False
1868:
1869:
1870: def checkFile(filename,data,folder):
1871: """check the files"""
1872: # first check the file name
1873: fn=filename.split(".") # no extension
1874:
1875: if not fn[0][0]=="P":
1876: return False,"P missing in the filename"
1877: elif len(fn[0])!=7:
1878: return False,"P number has not the right length 6"
1879: elif not checkUTF8(data):
1880: return False,"not utf-8"
1881: else:
1882: return True,""
1883:
1884:
1885: def splitatf(fh,dir=None,ext=None):
1886: """split it"""
1887: ret=None
1888: nf=None
1889: i=0
1890:
1891: #ROC: why split \n first and then \r???
1892: if (type(fh) is StringType) or (type(fh) is UnicodeType):
1893: iter=fh.split("\n")
1894: else:
1895: iter=fh.readlines()
1896:
1897: for lineTmp in iter:
1898: lineTmp=lineTmp.replace(codecs.BOM_UTF8,'') # make sure that all BOM are removed..
1899: for line in lineTmp.split("\r"):
1900: #logging.log("Deal with: %s"%line)
1901: if ext:
1902: i+=1
1903: if (i%100)==0:
1904: ext.result+="."
1905: if i==10000:
1906: i=0
1907: ext.result+="<br>"
1908: #check if basket name is in the first line
1909: if line.find("#atf basket")>=0: #old convention
1910: ret=line.replace('#atf basket ','')
1911: ret=ret.split('_')[0]
1912: elif line.find("#basket:")>=0: #new convention
1913: ret=line.replace('#basket: ','')
1914: ret=ret.split('_')[0]
1915:
1916: else:
1917: if (len(line.lstrip())>0) and (line.lstrip()[0]=="&"): #newfile
1918: if nf:
1919: nf.close() #close last file
1920:
1921:
1922: filename=line[1:].split("=")[0].rstrip()+".atf"
1923: if dir:
1924: filename=os.path.join(dir,filename)
1925: nf=file(filename,"w")
1926: logging.info("open %s"%filename)
1927: if nf:
1928: nf.write(line.replace("\n","")+"\n")
1929:
1930: try:
1931: nf.close()
1932: except:
1933: pass
1934:
1935: if not((type(fh) is StringType) or (type(fh) is UnicodeType)):
1936: fh.close()
1937: return ret,len(os.listdir(dir))
1938:
1939:
1940: class CDLIFileFolder(extVersionedFileFolder):
1941: """CDLI File Folder"""
1942:
1943: security=ClassSecurityInfo()
1944: meta_type="CDLI Folder"
1945: file_meta_type=['CDLI file']
1946: folder_meta_type=['CDLI Folder']
1947:
1948: file_catalog='CDLICatalog'
1949:
1950: #downloadCounter=0 # counts how many download for all files currently run, be mehr als 5 wird verweigert.
1951: tmpStore2={}
1952:
1953: def _newVersionedFile(self, id, title='', lockedBy=None, author=None):
1954: """factory for versioned files. to be overridden in derived classes."""
1955: logging.debug("_newVersionedFile(CDLI)")
1956: return CDLIFile(id, title, lockedBy=lockedBy, author=author)
1957:
1958: def setTemp(self,name,value):
1959: """set tmp"""
1960:
1961: setattr(self,name,value)
1962:
1963: deleteFileForm = PageTemplateFile("zpt/doDeleteFile", globals())
1964:
1965: def delete(self,ids,REQUEST=None):
1966: """delete these files"""
1967: if type(ids) is not ListType:
1968: ids=[ids]
1969:
1970: self.manage_delObjects(ids)
1971:
1972: if REQUEST is not None:
1973: return self.index_html()
1974:
1975:
1976: def getVersionNumbersFromIds(self,ids):
1977: """get the numbers of the current versions of documents described by their ids"""
1978:
1979: ret=[]
1980: searchStr=" OR ".join(ids)
1981:
1982: founds=self.CDLICatalog.search({'title':searchStr})
1983:
1984: for found in founds:
1985: lastVersion=found.getObject().getContentObject()
1986: ret.append((found.getId,lastVersion))
1987:
1988: return ret
1989:
1990: def getFile(self,fn):
1991: """get the content of the file fn"""
1992: logging.debug("getFile: %s"%repr(fn))
1993: if not self.hasObject(fn):
1994: # search deeper
1995: founds=getattr(self, self.file_catalog).search({'textid':fn})
1996: if founds:
1997: obj=founds[0].getObject().getContentObject()
1998: else:
1999: return ""
2000: else:
2001: obj = self[fn].getContentObject()
2002:
2003: return obj.getData()[0:]
2004:
2005:
2006: def checkCatalog(self,fn):
2007: """check if fn is in the catalog"""
2008: #TODO add checkCatalog
2009:
2010:
2011: def findObjectsFromListWithVersion(self,list,author=None):
2012: """find objects from a list with versions
2013: @param list: list of tuples (cdliFile,version)
2014: """
2015: #self.REQUEST.SESSION['fileIds']=list#store fieldIds in session for further usage
2016: #self.REQUEST.SESSION['searchList']=self.REQUEST.SESSION['fileIds']
2017:
2018: pt=getattr(self,'filelistVersioned.html')
2019:
2020: return pt(search=list,author=author)
2021:
2022:
2023: def getAllPNumbers(self):
2024: """get a list of all files (resp their p-numbers) stored"""
2025:
2026: ret=[x.getId for x in self.CDLICatalog()]
2027:
2028: return ret
2029:
2030: def expandFile(self,fileId,fileTree):
2031: """wildcard in fileID suche alle Treffer"""
2032: founds=self.CDLICatalog({'title':fileId})
2033: for found in founds:
2034: fileTree.add(found.getId)
2035: logging.debug("ADDD:"+found.getId)
2036:
2037: 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):
2038: """findObjectsFromList (, TAB oder LINE separated)"""
2039:
2040: logging.debug("start: findObjectsFromList")
2041: #logging.debug("start: findObjectsFromList"+repr(list))
2042:
2043:
2044: if upload: # list from file upload
2045: txt=upload.read()
2046:
2047: if enterList:
2048: txt=enterList
2049:
2050: if upload or enterList:
2051: txt=txt.replace(",","\n")
2052: txt=txt.replace("\t","\n")
2053: txt=txt.replace("\r","\n")
2054: idsTmp=txt.split("\n")
2055: ids=[]
2056: for id in idsTmp: # make sure that no empty lines
2057: idTmp=id.lstrip().rstrip()
2058: if len(idTmp)>0:
2059:
2060: ids.append(idTmp)
2061:
2062: #self.REQUEST.SESSION['ids']=" OR ".join(ids)
2063:
2064: pt=getattr(self,'filelist.html')
2065: self.REQUEST.SESSION['searchList']=ids
2066: return pt(search=ids)
2067:
2068: if basketName:
2069: #TODO: get rid of one of these..
2070:
2071: pt=getattr(self,'filelist.html')
2072: return pt(basketName=basketName,numberOfObjects=numberOfObjects)
2073:
2074: if hash is not None and hasattr(self.cdliRoot,'v_tmpStore') and self.cdliRoot.v_tmpStore.has_key(hash):
2075:
2076: logging.debug("asking for storage2")
2077: result =self.cdliRoot.v_tmpStore[hash]
2078: if result:
2079: logging.debug("give result from storage2")
2080: return hash,self.cdliRoot.v_tmpStore[hash]
2081:
2082: if list is not None: # got already a list
2083:
2084: logging.debug(" ----List version")
2085: ret=[]
2086: fileTree=Set()
2087:
2088: for fileId in list:
2089:
2090: if fileId.find("*")>-1: #check for wildcards
2091: self.expandFile(fileId,fileTree)
2092:
2093: elif len(fileId.split("."))==1:
2094: fileId=fileId+".atf"
2095: fileTree.add(fileId)
2096: #logging.debug(" -----:"+fileId)
2097: #ret+=self.CDLICatalog({'title':fileId})
2098: #x =self.getFileObject(fileId)
2099: #if x is not None:
2100: # ret.append(x)
2101:
2102:
2103:
2104: ids = fileTree & self.v_file_ids
2105: #self.REQUEST.SESSION['fileIds']=ids#store fieldIds in session for further usage
2106: l=makelist(fileTree)[0:]
2107: logging.debug("l-list:"+repr(l))
2108: self.REQUEST.SESSION['fileIds']=l#store fieldIds in session for further usage
2109: self.REQUEST.SESSION['searchList']=l
2110: #self.REQUEST.SESSION['searchList']=['P000001.atf']
2111:
2112:
2113: hash = md5.new(repr(makelist(fileTree))).hexdigest() # erzeuge hash als identification
2114: self.REQUEST.SESSION['hash']=hash
2115: #TODO: do I need garbage collection for v_tmpStore ?
2116:
2117: #logging.debug("Hash:"+repr(hash))
2118: #
2119: # if hasattr(self.cdliRoot,'v_tmpStore') and self.cdliRoot.v_tmpStore.has_key(hash):
2120: # logging.debug("asking for storage")
2121: # res=self.cdliRoot.v_tmpStore[hash]
2122: # if res:
2123: # if returnHash == True:
2124: # return hash,res
2125: # return res
2126:
2127: #TODO: get rid of one of these..
2128: #ids=[x.getObject().getId() for x in ret]
2129: ret=[(self.getFileObject(x),self.getFileObjectLastVersion(x)) for x in ids]
2130:
2131: #self.REQUEST.SESSION['fileIds']=ids#store fieldIds in session for further usage
2132: #self.REQUEST.SESSION['searchList']=self.REQUEST.SESSION['fileIds']
2133:
2134: if display:
2135: pt=getattr(self,'filelist.html')
2136:
2137: return pt(search=ids)
2138: else:
2139: #self.REQUEST.SESSION['hash'] = ret # store in session
2140: if not hasattr(self,'v_tmpStore'):
2141: self.cdliRoot.v_tmpStore={}
2142: #logging.debug("HHHHHHNEU:"+repr(self.makelist(ids)))
2143: #logging.debug("HHHHHHNEU:"+repr(hash))
2144: self.cdliRoot.v_tmpStore[hash] = ret # store in session
2145: if returnHash == True:
2146: return hash,ret
2147: return ret
2148:
2149:
2150:
2151: if start:
2152: RESPONSE.redirect("filelist.html?start:int="+str(start))
2153:
2154: security.declareProtected('Manage','createAllFilesAsSingleFile')
2155: def createAllFilesAsSingleFile(self,RESPONSE=None):
2156: """download all files"""
2157:
2158: def sortF(x,y):
2159: return cmp(x[0],y[0])
2160:
2161: catalog=getattr(self,self.file_catalog)
2162: #tf,tfilename=mkstemp()
2163: if not hasattr(self.temp_folder,'downloadCounter'):
2164: self.temp_folder.downloadCounter=0
2165:
2166: if getattr(self.temp_folder,'downloadCounter',0) > 5:
2167: return """I am sorry, currently the server has to many requests for downloads, please come back later!"""
2168:
2169: self.temp_folder.downloadCounter+=1
2170: self._p_changed=1
2171: transaction.get().commit()
2172:
2173: list=[(x.getId,x) for x in catalog()]
2174: list.sort(sortF)
2175:
2176:
2177:
2178: RESPONSE.setHeader("Content-Disposition","""attachement; filename=%s"""%"all.atf")
2179: RESPONSE.setHeader("Content-Type","application/octet-stream")
2180: tmp=""
2181: for l in list:
2182: obj=l[1].getObject()
2183:
2184: if obj.meta_type=="CDLI file":
2185:
2186: #os.write(tf,obj.getLastVersion().data)
2187: if RESPONSE:
2188: RESPONSE.write(obj.getData()[0:])
2189: RESPONSE.write("\n")
2190: self.temp_folder.downloadCounter-=1
2191: self._p_changed=1
2192: transaction.get().commit()
2193: #os.close(tf)
2194: #RESPONSE.redirect(self.absolute_url()+"/downloadFile?fn="%tfilename)
2195: return True
2196:
2197: def downloadFile(self,fn):
2198: """download fn - not used yet"""
2199: self.REQUEST.RESPONSE.setHeader("Content-Disposition","""attachement; filename=%s"""%self.getLastVersion().getId())
2200: self.REQUEST.RESPONSE.setHeader("Content-Type","application/octet-stream")
2201: self.REQUEST.RESPONSE.write(file(fn).read())
2202:
2203:
2204:
2205: def hasParent(self):
2206: """returns true falls subfolder"""
2207:
2208: if self.aq_parent.meta_type in self.folder_meta_type:
2209: return True
2210: else:
2211: return False
2212:
2213: def getFolders(self):
2214: """get all subfolders"""
2215: ret=[]
2216: folders=self.ZopeFind(self,obj_metatypes=self.folder_meta_type)
2217: for folder in folders:
2218: ret.append((folder[1],
2219: len(self.ZopeFind(folder[1],obj_metatypes=self.folder_meta_type)),
2220: len(self.ZopeFind(folder[1],obj_metatypes=self.file_meta_type))
2221: ))
2222: return ret
2223:
2224:
2225: security.declareProtected('manage','index_html')
2226: def index_html(self):
2227: """main"""
2228: ext=self.ZopeFind(self,obj_ids=["index.html"])
2229: if ext:
2230: return ext[0][1]()
2231:
2232: pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','CDLIFileFolderMain')).__of__(self)
2233: return pt()
2234:
2235:
2236: manage_addCDLIFileFolderForm=DTMLFile('dtml/folderAdd', globals())
2237:
2238:
2239: def manage_addCDLIFileFolder(self, id, title='',
2240: createPublic=0,
2241: createUserF=0,
2242: REQUEST=None):
2243: """Add a new Folder object with id *id*.
2244:
2245: If the 'createPublic' and 'createUserF' parameters are set to any true
2246: value, an 'index_html' and a 'UserFolder' objects are created respectively
2247: in the new folder.
2248: """
2249: ob=CDLIFileFolder()
2250: ob.id=str(id)
2251: ob.title=title
2252: self._setObject(id, ob)
2253: ob=self._getOb(id)
2254:
2255: checkPermission=getSecurityManager().checkPermission
2256:
2257: if createUserF:
2258: if not checkPermission('Add User Folders', ob):
2259: raise Unauthorized, (
2260: 'You are not authorized to add User Folders.'
2261: )
2262: ob.manage_addUserFolder()
2263:
2264:
2265: if REQUEST is not None:
2266: return self.manage_main(self, REQUEST, update_menu=1)
2267:
2268: class CDLIRoot(Folder):
2269: """main folder for cdli"""
2270:
2271: meta_type="CDLIRoot"
2272: downloadCounterBaskets=0 # counts the current basket downloads if counter > 10 no downloads are possible
2273:
2274: file_catalog = 'CDLICatalog'
2275:
2276: # word splitter for search
2277: splitter = {'words':cdliSplitter.wordSplitter(),
2278: 'graphemes':cdliSplitter.graphemeSplitter()}
2279:
2280:
2281: def viewATF(self,id,RESPONSE):
2282: """view an Object"""
2283: ob = self.CDLICatalog({'title':id})
2284: logging.debug(ob[0].getObject().getLastVersion().absolute_url()+"/view")
2285: if len(ob)>0:
2286: RESPONSE.redirect(ob[0].getObject().getLastVersion().absolute_url()+"/view")
2287: return "not found"
2288:
2289: def history(self,id,RESPONSE):
2290: """view an Object"""
2291: ob = self.CDLICatalog({'title':id})
2292: if len(ob)>0:
2293: RESPONSE.redirect(ob[0].absolute_url+"/history")
2294: return "not found"
2295:
2296:
2297: def downloadLocked(self,id,RESPONSE):
2298: """view an Object"""
2299: ob = self.CDLICatalog({'title':id})
2300: if len(ob)>0:
2301: RESPONSE.redirect(ob[0].absolute_url+"/downloadLocked")
2302: return "not found"
2303:
2304: def download(self,id,RESPONSE):
2305: """view an Object"""
2306: ob = self.CDLICatalog({'title':id})
2307: if len(ob)>0:
2308: RESPONSE.redirect(ob[0].getLastVersion().absolute_url())
2309: return "not found"
2310: def addCDLIFileObjectForm(self,id,RESPONSE):
2311: """view an Object"""
2312: ob = self.CDLICatalog({'title':id})
2313: if len(ob)>0:
2314: RESPONSE.redirect(ob[0].absolute_url+"/addCDLIFileObjectForm")
2315: return "not found"
2316:
2317: def addVersionedFileObjectForm(self,id,RESPONSE):
2318: """view an Object"""
2319: ob = self.CDLICatalog({'title':id})
2320: if len(ob)>0:
2321: RESPONSE.redirect(ob[0].absolute_url+"/addVersionedFileObjectForm")
2322: return "not found"
2323:
2324: def unlock(self,id,RESPONSE):
2325: """view an Object"""
2326: ob = self.CDLICatalog({'title':id})
2327: if len(ob)>0:
2328: RESPONSE.redirect(ob[0].absolute_url+"/unlock")
2329: return "not found"
2330:
2331: def getFileObject(self,fileId):
2332: """get an object"""
2333: x=self.v_files.get(fileId)
2334: #logging.debug(x)
2335: return x
2336:
2337: def getFileObjectLastVersion(self,fileId):
2338: """get an object"""
2339: x=self.v_files_lastVersion.get(fileId)
2340: #logging.debug(x)
2341: return x
2342:
2343: def showFileIds(self):
2344: """showIds"""
2345: return self.v_file_ids
2346:
2347: def generateFileBTree(self):
2348: """erzeuge einen Btree aus allen Files"""
2349: self.v_files = OOBTree()
2350: self.v_files_lastVersion = OOBTree()
2351: self.v_file_ids = Set()
2352:
2353: for x in self.CDLICatalog.searchResults():
2354:
2355: self.v_files.update({x.getId:x.getObject()})
2356: self.v_files_lastVersion.update({x.getId:x.getObject().getLastVersion()})
2357: self.v_file_ids.add(x.getId)
2358: logging.debug("add:"+x.getId+"XXX"+repr(x.getObject()))
2359:
2360: return True
2361:
2362:
2363: def updateOrAddToFileBTree(self,obj):
2364: """update a BTree"""
2365: self.v_files.update({obj.getId():obj})
2366: self.v_files_lastVersion.update({obj.getId():obj.getLastVersion()})
2367:
2368: self.v_file_ids.add(obj.getId())
2369: logging.debug("update:"+obj.getId()+"XXX"+repr(obj))
2370:
2371: def deleteFromBTree(self,objId):
2372: """delete an obj"""
2373: self.v_files.pop(objId)
2374: self.v_files_lastVersion.pop(objId)
2375: self.v_file_ids.remove(objId)
2376:
2377:
2378:
2379: def deleteFiles(self,ids):
2380: """delete files"""
2381: for id in ids:
2382: founds=self.CDLICatalog.search({'title':id.split(".")[0]})
2383: if founds:
2384: logging.debug("deleting %s"%founds)
2385: folder=founds[0].getObject().aq_parent #get the parent folder of the object
2386: logging.debug("deleting from %s"%folder)
2387: cut=folder.delete([founds[0].getId]) #cut it out
2388:
2389:
2390:
2391: def searchText(self, query, index='graphemes'):
2392: """searches query in the fulltext index and returns a list of file ids/P-numbers"""
2393: # see also: http://www.plope.com/Books/2_7Edition/SearchingZCatalog.stx#2-13
2394: logging.debug("searchtext for '%s' in index %s"%(query,index))
2395: #import Products.ZCTextIndex.QueryParser
2396: #qp = QueryParser.QueryParser()
2397: #logging.debug()
2398: idxQuery = {index:{'query':query}}
2399: idx = getattr(self, self.file_catalog)
2400: # do search
2401: resultset = idx.search(query_request=idxQuery,sort_index='textid')
2402: # put only the P-Number in the result
2403: results = [res.getId[:7] for res in resultset]
2404: logging.debug("searchtext: found %d texts"%len(results))
2405: return results
2406:
2407:
2408: def getFile(self, pnum):
2409: """get the translit file with the given pnum"""
2410: f = getattr(self, self.file_catalog).search({'textid':pnum})
2411: if not f:
2412: return ""
2413:
2414: return f[0].getObject().getData()
2415:
2416:
2417: def showFile(self,fileId,wholePage=False):
2418: """show a file
2419: @param fileId: P-Number of the document to be displayed
2420: """
2421: f=getattr(self, self.file_catalog).search({'textid':fileId})
2422: if not f:
2423: return ""
2424:
2425: if wholePage:
2426: logging.debug("show whole page")
2427: return f[0].getObject().getContentObject().view()
2428: else:
2429: return f[0].getObject().getLastVersionFormattedData()
2430:
2431:
2432: def showWordInFile(self,fileId,word,indexName='graphemes',regExp=False,):
2433: """get lines with word from FileId"""
2434: logging.debug("showwordinfile word='%s' index=%s file=%s"%(word,indexName,fileId))
2435:
2436: file = formatAtfFullLineNum(self.getFile(fileId))
2437: ret=[]
2438:
2439: # add whitespace before and whitespace and line-end to splitter bounds expressions
2440: bounds = self.splitter[indexName].bounds
2441: splitexp = "(%s|\s)(%%s)(%s|\s|\Z)"%(bounds,bounds)
2442: # clean word expression
2443: # TODO: this should use QueryParser itself
2444: # take out double quotes
2445: word = word.replace('"','')
2446: # take out ignorable signs
2447: ignorable = self.splitter[indexName].ignorex
2448: word = ignorable.sub('', word)
2449: # compile into regexp objects and escape parens
2450: wordlist = [re.compile(splitexp%re.escape(w)) for w in word.split(' ')]
2451:
2452: for line in file.splitlines():
2453: for word in wordlist:
2454: #logging.debug("showwordinfile: searching for %s in %s"%(word.pattern,ignoreable.sub('',line)))
2455: if word.search(ignorable.sub('',line)):
2456: line = formatAtfLineHtml(line)
2457: ret.append(line)
2458: break
2459:
2460: return ret
2461:
2462:
2463: def showWordInFiles(self,fileIds,word,indexName='graphemes',regExp=False):
2464: """
2465: get lines with word from all ids in list FileIds.
2466: returns dict with id:lines pairs.
2467: """
2468: logging.debug("showwordinfiles word='%s' index=%s file=%s"%(word,indexName,fileIds))
2469:
2470: return dict([(id,self.showWordInFile(id, word, indexName, regExp)) for id in fileIds])
2471:
2472:
2473: def tagWordInFile(self,fileId,word,indexName='graphemes',regExp=False):
2474: """get text with word highlighted from FileId"""
2475: logging.debug("tagwordinfile word='%s' index=%s file=%s"%(word,indexName,fileId))
2476:
2477: file=self.getFile(fileId)
2478: tagStart=u'<span class="found">'
2479: tagEnd=u'</span>'
2480: tagStr=tagStart + u'%%s' + tagEnd
2481: ret=[]
2482:
2483: # add whitespace to splitter bounds expressions and compile into regexp object
2484: bounds = self.splitter[indexName].bounds
2485: wordsplit = re.compile("(%s|\s)"%bounds)
2486: # clean word expression
2487: # TODO: this should use QueryParser itself
2488: word = word.replace('"','') # take out double quotes
2489: # take out ignoreable signs
2490: ignorable = self.splitter[indexName].ignorex
2491: word = ignorable.sub('', word)
2492: # split search terms by blanks
2493: words = word.split(' ')
2494: # split search terms again (for grapheme search with words)
2495: splitwords = dict(((w,self.splitter[indexName].process([w])) for w in words))
2496:
2497: for line in file.splitlines():
2498: line = unicodify(line)
2499: # ignore lemma and other lines
2500: if line.lstrip().startswith('#lem:'):
2501: continue
2502: # ignore p-num line
2503: if line.startswith('&P'):
2504: continue
2505: # ignore version lines
2506: if line.startswith('#version'):
2507: continue
2508: # ignore atf type lines
2509: if line.startswith('#atf:'):
2510: continue
2511:
2512: # first scan
2513: hitwords = []
2514: for w in words:
2515: if ignorable.sub('',line).find(w) > -1:
2516: # word is in line
2517: # append split word for grapheme search with words
2518: hitwords.extend(splitwords[w])
2519: #hitwords.extend(wordsplit.split(w))
2520:
2521: # examine hits closer
2522: if hitwords:
2523: # split line into words
2524: parts = wordsplit.split(line)
2525: line = ""
2526: for p in parts:
2527: #logging.debug("tagwordinfile: searching for %s in %s"%(p,hitwords))
2528: # reassemble line
2529: if ignorable.sub('', p) in hitwords:
2530: #logging.debug("tagwordinfile: found %s in %s"%(p,hitwords))
2531: # this part was found
2532: line += tagStart + formatAtfHtml(p) + tagEnd
2533: else:
2534: line += formatAtfHtml(p)
2535:
2536: else:
2537: # no hits
2538: line = formatAtfHtml(line)
2539:
2540: ret.append(line)
2541:
2542: return u'<br>\n'.join(ret)
2543:
2544:
2545:
2546: def tagWordInFiles(self,fileIds,word,indexName='graphemes',regExp=False):
2547: """
2548: get texts with highlighted word from all ids in list FileIds.
2549: returns dict with id:text pairs.
2550: """
2551: logging.debug("tagwordinfiles word='%s' index=%s file=%s"%(word,indexName,fileIds))
2552: return dict([(id,self.tagWordInFile(id, word, indexName, regExp)) for id in fileIds])
2553:
2554:
2555: def getFileVersionList(self, pnum):
2556: """get the version history as a list for the translit file with the given pnum"""
2557: f = getattr(self, self.file_catalog).search({'textid':pnum})
2558: if not f:
2559: return []
2560:
2561: return f[0].getObject().getVersionList()
2562:
2563:
2564: def URLquote(self,str):
2565: """quote url"""
2566: return urllib.quote(str)
2567:
2568: def URLunquote(self,str):
2569: """unquote url"""
2570: return urllib.unquote(str)
2571:
2572: def URLquote_plus(self,str):
2573: """quote url"""
2574: return urllib.quote_plus(str)
2575:
2576: def URLunquote_plus(self,str):
2577: """unquote url"""
2578: return urllib.unquote_plus(str)
2579:
2580:
2581: def forceunlock(self):
2582: "break all locks"
2583: ret=[]
2584: for f in self.ZopeFind(self,obj_metatypes="CDLI file",search_sub=1):
2585: un=f[1].forceunlock()
2586:
2587: if un and un !="":
2588: ret.append((f[0],un))
2589:
2590: return ret
2591:
2592:
2593: def getChangesByAuthor(self,author,n=100):
2594: """getChangesByAuthor"""
2595: zcat=self.CDLIObjectsCatalog
2596: res=zcat({'lastEditor':author,
2597: 'sort_on':'getTime',
2598: 'sort_order':'descending',
2599: 'sort_limit':n})[:n ]
2600:
2601: return res
2602:
2603: def getChangesByAuthor_html(self,author,n=100):
2604: """html output for changes by author"""
2605: tmp={}
2606: list=[]
2607: for x in self.getChangesByAuthor(author):
2608: nr=x.getObject().getVersionNumber()
2609: id=x.getObject().aq_parent.getId()
2610: #hinzufuegen, wenn Version neuer als die
2611: if tmp.get(id,(0,0))[1] < nr:
2612: tmp[id]=(x.getObject().aq_parent,nr)
2613:
2614:
2615: return self.cdli_main.findObjectsFromListWithVersion(list=tmp.values(),author=author)
2616:
2617: def getLastChanges(self,n=100):
2618: """get the last n changes"""
2619: n=int(n)
2620: zcat=self.CDLICatalog
2621: return zcat({'sort_on':'getLastChangeDate',
2622: 'sort_order':'descending',
2623: 'sort_limit':n})[:n ]
2624:
2625:
2626: def getLastChanges_html(self,n=100):
2627: """get the last n changes"""
2628: list = [x.getId for x in self.getLastChanges(n)]
2629: return self.cdli_main.findObjectsFromList(list=list,display=True)
2630:
2631: def refreshTxt(self,txt="",threadName=None):
2632: """txt fuer refresh"""
2633:
2634: return """ 2;url=%s?repeat=%s """%(self.absolute_url()+txt,threadName)
2635:
2636: def refreshTxtBasket(self,txt="",threadName=None):
2637: """txt fuer refresh"""
2638:
2639: return """ 2;url=%s?repeat=%s """%(txt,threadName)
2640:
2641:
2642: def getResult(self,threadName=None):
2643: """result of thread"""
2644: try:
2645: return self._v_uploadATF[threadName].getResult()
2646: except:
2647: return "One moment, please"
2648:
2649:
2650: def checkThreads(self):
2651: """check threads"""
2652: ret="<html><body>"
2653: for thread in threading.enumerate():
2654: ret+="<p>%s (%s): %s</p>"%(repr(thread),thread.getName(),thread.isAlive())
2655:
2656: return ret
2657:
2658:
2659: def uploadATFRPC(self,data,username):
2660: """upload an atffile via xml-rpc"""
2661: uploader=uploadATFThread()
2662:
2663: #generate an random id for the upload object
2664: from random import randint
2665: if (not self.REQUEST.SESSION.get('idTmp',None)):
2666:
2667: idTmp=str(randint(0,1000000000))
2668: self.REQUEST.SESSION['idTmp']=idTmp
2669: else:
2670: idTmp=self.REQUEST.SESSION.get('idTmp',None)
2671:
2672:
2673: uploader.set(data,0,username,idTmp)
2674:
2675: stObj=uploader.run()
2676:
2677: processor=uploadATFfinallyThread()
2678:
2679: basketname=stObj.returnValue['basketNameFromFile']
2680:
2681: processor.set("uploadchanged",basketname=basketname,SESSION=stObj.returnValue,username=username,serverport=self.REQUEST['SERVER_PORT'])
2682:
2683: processor.run()
2684:
2685:
2686: return generateXMLReturn(stObj.returnValue)
2687:
2688: def uploadATF(self,repeat=None,upload=None,basketId=0,RESPONSE=None):
2689: """upload an atf file / basket file"""
2690: #self._v_uploadATF.returnValue=None
2691:
2692: #generate an random id for the upload thread
2693: from random import randint
2694: if (not self.REQUEST.SESSION.get('idTmp',None)):
2695:
2696: idTmp=str(randint(0,1000000000))
2697: self.REQUEST.SESSION['idTmp']=idTmp
2698: else:
2699: idTmp=self.REQUEST.SESSION.get('idTmp',None)
2700:
2701:
2702: threadName=repeat
2703: if not threadName or threadName=="":
2704: #new thread not called from the waiting page
2705: tmpVar=False
2706:
2707: thread=uploadATFThread()
2708: threadName=thread.getName()[0:]
2709: if (not hasattr(self,'_v_uploadATF')):
2710: self._v_uploadATF={}
2711:
2712: self._v_uploadATF[threadName]=thread
2713: #self._xmltrans.start()
2714: #thread=Thread(target=self._v_uploadATF)
2715: logging.info("set thread. extern")
2716: self._v_uploadATF[threadName].set(upload,basketId,self.REQUEST['AUTHENTICATED_USER'],idTmp,serverport=self.REQUEST['SERVER_PORT'])
2717: #thread.start()
2718: logging.info("start thread. extern")
2719: self._v_uploadATF[threadName].start()
2720:
2721:
2722: self.threadName=self._v_uploadATF[threadName].getName()[0:]
2723: wait_template=self.aq_parent.ZopeFind(self.aq_parent,obj_ids=['wait_template'])
2724:
2725: if wait_template:
2726: return wait_template[0][1]()
2727: pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','uploadATFWait.zpt')).__of__(self)
2728: return pt(txt='/uploadATF',threadName=threadName)
2729: #_v_xmltrans.run()
2730:
2731: else:
2732: #recover thread, if lost
2733: if (not hasattr(self,'_v_uploadATF')):
2734: self._v_uploadATF={}
2735: if not self._v_uploadATF.get(threadName,None):
2736: for thread in threading.enumerate():
2737: if threadName == thread.getName():
2738: self._v_uploadATF[threadName]=thread
2739:
2740: if self._v_uploadATF.get(threadName,None) and (not self._v_uploadATF[threadName].returnValue):
2741:
2742:
2743: wait_template=self.aq_parent.ZopeFind(self.aq_parent,obj_ids=['wait_template'])
2744: if wait_template:
2745: return wait_template[0][1]()
2746:
2747: pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','uploadATFWait.zpt')).__of__(self)
2748:
2749: return pt(txt='/uploadATF',threadName=threadName)
2750:
2751: else:
2752: tmp=getattr(self.temp_folder,idTmp).returnValue
2753:
2754: pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','uploadCheck.zpt')).__of__(self)
2755:
2756: return pt(changed=tmp['changed'],lockerrors=tmp['lockerrors'],errors=tmp['errors'],dir=tmp['dir'],newPs=tmp['newPs'],basketLen=tmp['basketLen'],numberOfFiles=tmp['numberOfFiles'],
2757: basketNameFromId=tmp['basketNameFromId'],basketNameFromFile=tmp['basketNameFromFile'],basketId=tmp['basketId'])
2758:
2759: def redoUpload(self,threadName):
2760: """redo the upload"""
2761: tmp=self.cdli_main.tmpStore2[threadName]
2762: pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','uploadCheck.zpt')).__of__(self)
2763: return pt(changed=tmp['changed'],lockerrors=tmp['lockerrors'],errors=tmp['errors'],dir=tmp['dir'],newPs=tmp['newPs'],basketLen=tmp['basketLen'],numberOfFiles=tmp['numberOfFiles'],
2764: basketNameFromId=tmp['basketNameFromId'],basketNameFromFile=tmp['basketNameFromFile'],basketId=tmp['basketId'])
2765:
2766: def uploadATFfinally(self,procedure='',comment="",basketname='',unlock=None,repeat=None,RESPONSE=None):
2767: """nowupload the files"""
2768:
2769:
2770:
2771: threadName=repeat
2772: if not threadName or threadName=="":
2773: thread=uploadATFfinallyThread()
2774: threadName=thread.getName()[0:]
2775:
2776: if (not hasattr(self,'_v_uploadATF')):
2777: self._v_uploadATF={}
2778:
2779:
2780: self._v_uploadATF[threadName]=thread
2781:
2782: idTmp=self.REQUEST.SESSION['idTmp']
2783: stObj=getattr(self.temp_folder,idTmp)
2784: 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'])
2785:
2786: self._v_uploadATF[threadName].start()
2787:
2788:
2789:
2790: wait_template=self.aq_parent.ZopeFind(self.aq_parent,obj_ids=['wait_template'])
2791:
2792: if wait_template:
2793: return wait_template[0][1]()
2794: pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','uploadATFWait.zpt')).__of__(self)
2795:
2796: return pt(txt='/uploadATFfinally',threadName=threadName)
2797: #_v_xmltrans.run()
2798:
2799: else:
2800: #recover thread, if lost
2801: if not hasattr(self,'_v_uploadATF'):
2802: self._v_uploadATF={}
2803: if not self._v_uploadATF.get(threadName,None):
2804: for thread in threading.enumerate():
2805: if threadName == thread.getName():
2806: self._v_uploadATF[threadName]=thread
2807:
2808: if self._v_uploadATF.get(threadName,None) and (self._v_uploadATF[threadName] is not None) and (not self._v_uploadATF[threadName].end) :
2809:
2810: wait_template=self.aq_parent.ZopeFind(self.aq_parent,obj_ids=['wait_template'])
2811: if wait_template:
2812: return wait_template[0][1]()
2813:
2814: pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','uploadATFWait.zpt')).__of__(self)
2815: return pt(txt='/uploadATFfinally',threadName=threadName)
2816: else:
2817:
2818:
2819: idTmp=self.REQUEST.SESSION['idTmp']
2820: stObj=getattr(self.temp_folder,idTmp)
2821: self.REQUEST.SESSION['idTmp']=None
2822:
2823: #update changed
2824: logging.debug("dir:"+repr(stObj.returnValue['changed']))
2825: for x in stObj.returnValue['changed']:
2826: ob=self.CDLICatalog.search({'title':x[0]})
2827:
2828: self.cdliRoot.updateOrAddToFileBTree(ob[0].getObject())
2829: if RESPONSE is not None:
2830: RESPONSE.redirect(self.absolute_url())
2831:
2832: def importFiles(self,comment="",author="" ,folderName="/Users/dwinter/atf", files=None,ext=None):
2833: """import files"""
2834: logging.debug("importFiles folderName=%s files=%s ext=%s"%(folderName,files,ext))
2835: root=self.cdli_main
2836: count=0
2837: if not files:
2838: files=os.listdir(folderName)
2839:
2840: for f in files:
2841: folder=f[0:3]
2842: f2=f[0:5]
2843:
2844: #check if main folder PXX already exists
2845: obj=self.ZopeFind(root,obj_ids=[folder])
2846: logging.debug("importFiles: folder=%s f2=%s obj=%s"%(folder,f2,obj))
2847: if ext:
2848: ext.result="<p>adding: %s </p>"%f+ext.result
2849:
2850:
2851: if not obj: # if not create it
2852: manage_addCDLIFileFolder(root,folder,folder)
2853: fobj=getattr(root,folder)
2854: #transaction.get().commit()
2855:
2856: else:
2857: fobj=obj[0][1]
2858:
2859: #check IF PYYYYY already exist
2860: obj2=fobj.ZopeFind(fobj,obj_ids=[f2])
2861: logging.debug("importFiles: fobj=%s obj2=%s"%(fobj,obj2))
2862:
2863: if not obj2:# if not create it
2864: manage_addCDLIFileFolder(fobj,f2,f2)
2865: fobj2=getattr(fobj,f2)
2866:
2867: else:
2868: fobj2=obj2[0][1]
2869:
2870: # not add the file
2871: file2=os.path.join(folderName,f)
2872: id=f
2873: logging.debug("importFiles: addCDLIFile fobj2=%s, f=%s file2=%s"%(fobj2,repr(f),repr(file2)))
2874: fobj2.addFile(vC='',file=file(file2),author=author,newName=f)
2875: count+=1
2876:
2877: #now add the file to the storage
2878: ob = getattr(fobj2,f)
2879: self.cdliRoot.updateOrAddToFileBTree(ob)
2880:
2881: if count%100==0:
2882: logging.debug("importfiles: committing")
2883: transaction.get().commit()
2884:
2885: transaction.get().commit()
2886: return "ok"
2887:
2888:
2889: manage_addCDLIRootForm=DTMLFile('dtml/rootAdd', globals())
2890:
2891:
2892: def manage_addCDLIRoot(self, id, title='',
2893: createPublic=0,
2894: createUserF=0,
2895: REQUEST=None):
2896: """Add a new Folder object with id *id*.
2897:
2898: If the 'createPublic' and 'createUserF' parameters are set to any true
2899: value, an 'index_html' and a 'UserFolder' objects are created respectively
2900: in the new folder.
2901: """
2902: ob=CDLIRoot()
2903: ob.id=str(id)
2904: ob.title=title
2905: try:
2906: self._setObject(id, ob)
2907: except:
2908: pass
2909: ob=self._getOb(id)
2910:
2911: checkPermission=getSecurityManager().checkPermission
2912:
2913: if createUserF:
2914: if not checkPermission('Add User Folders', ob):
2915: raise Unauthorized, (
2916: 'You are not authorized to add User Folders.'
2917: )
2918: ob.manage_addUserFolder()
2919:
2920:
2921: if REQUEST is not None:
2922: return self.manage_main(self, REQUEST, update_menu=1)
2923:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>