File:  [Repository] / MPIWGWeb / Attic / MPIWGRoot.py
Revision 1.1.2.35: download - view: text, annotated - select for diffs - revision graph
Mon Jan 9 10:36:48 2012 UTC (12 years, 5 months ago) by dwinter
Branches: r2
changed cache for harvest to zeo

    1: from Products.PageTemplates.PageTemplateFile import PageTemplateFile
    2: from Products.PageTemplates.PageTemplate import PageTemplate
    3: from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
    4: from Products.ZSQLExtend.ZSQLExtend import ZSQLExtendFolder
    5: from Products.ZCatalog.CatalogPathAwareness import CatalogAware
    6: from OFS.Image import Image
    7: from Globals import package_home
    8: import urllib
    9: import MPIWGStaff
   10: import string
   11: import re
   12: import os
   13: from types import *
   14: import logging
   15: import xmlhelper # Methoden zur Verwaltung der projekt xml
   16: from OFS.SimpleItem import SimpleItem
   17: from OFS.Folder import Folder
   18: from Products.ZSQLMethods.SQL import SQLConnectionIDs
   19: from AccessControl import ClassSecurityInfo
   20: from bibliography import *
   21: import time
   22: import xml.dom.minidom
   23: import sys
   24: #from Ft.Xml.XPath import Evaluate
   25: #from Ft.Xml.XPath.Context import Context
   26: #from Ft.Xml.Domlette import NonvalidatingReader,PrettyPrint, Print
   27: #from Ft.Xml import EMPTY_NAMESPACE
   28: import copy
   29: import updatePersonalWWW
   30: import MPIWGStaff
   31: from MPIWGHelper import *
   32: from BeautifulSoup import BeautifulSoup, Comment
   33: from ZODB import FileStorage, DB
   34: from ZEO import ClientStorage
   35: 
   36: def sortWeight(x,y):
   37:     x1=int(getattr(x[1],'weight','0'))
   38:     y1=int(getattr(y[1],'weight','0'))
   39:     return cmp(x1,y1)
   40: 
   41: 
   42: class MPIWGRoot(ZSQLExtendFolder):
   43:     """Stammordner fuer den Web-Server"""
   44: 
   45:     _v_harvestCache=None
   46:     meta_type='MPIWGRoot'
   47: 
   48:     fieldLabels={'WEB_title':'WEB_Title',
   49:                  'xdata_01':'Responsible Scientists',
   50:                  'xdata_02':'Department',
   51:                  'xdata_03':'Historical Persons',
   52:                  'xdata_04':'Time period',
   53:                  'xdata_05':'Sorting number',
   54:                  'xdata_06':'Keywords',
   55:                  'xdata_07':'Short title',
   56:                  'xdata_08':'Other involved scholars' ,
   57:                  'xdata_09':'Disciplines',
   58:                  'xdata_10':'Themes',
   59:                  'xdata_11':'Object Digitallibrary',
   60:                  'xdata_12':'Cooperation partners',
   61:                  'xdata_13':'Funding institutions',
   62:                  'WEB_project_header':'WEB_project_header',
   63:                  'WEB_project_description':'WEB_project_description',
   64:                  'WEB_related_pub':'WEB_related_pub'}
   65:     
   66:     # (is this used?)
   67:     folders=['MPIWGProject','Folder','ECHO_Navigation']
   68:     # language of this instance
   69:     lang = 'en'
   70:     # types of objects that show up in navigation
   71:     nav_meta_types = ['MPIWGTemplate','MPIWGLink','MPIWGFolder']
   72: 
   73:     manage_options = Folder.manage_options+(
   74:         {'label':'Update personal homepages','action':'updatePersonalwww_html'},
   75:         {'label':'Reindex catalogs','action':'reindexCatalogs'},
   76:         {'label':'Main config','action':'changeMPIWGRootForm'},
   77:         {'label':'add e-mails','action':'showNewDBEntries'},
   78:         #{'label':'update the institutsbibliography','action':'updateInstitutsbiliography'},
   79:         #{'label':'Edit Historical Persons','action':'editHistoricalPersonsForm'},
   80:         #{'label':'Store Historical Persons','action':'storeHistoricalPersons'},
   81:         )
   82:     
   83: 
   84:     def getHarvestCachePort(self):
   85:         return getattr(self,"harvestPort",29999)
   86:     
   87:     def getHarvestCacheServer(self):
   88:         return getattr(self,"harvestServer","localhost")
   89:    
   90:     
   91:     def getHarvestCache(self):
   92:         logging.debug("CACHE:"+repr(self._v_harvestCache))
   93:         if self._v_harvestCache==None:
   94:              #storage = FileStorage.FileStorage('/tmp/'+self.getId()+'test-filestorage.fs')
   95:              addr = self.getHarvestCacheServer(), self.getHarvestCachePort()
   96:              storage = ClientStorage.ClientStorage(addr)
   97:              db = DB(storage)
   98:              self._v_harvestDV=db
   99:              conn = db.open()
  100:              dbroot = conn.root()
  101:              if not dbroot.has_key('templates'):
  102:                  from BTrees.OOBTree import OOBTree
  103:                  dbroot['templates'] = OOBTree()
  104:                  
  105:              self._v_harvestCache = dbroot['templates']
  106:         logging.debug("CACHE2:"+repr(self._v_harvestCache))            
  107:         return self._v_harvestCache
  108:     
  109:     def __del__(self):
  110:          if self._v_harvestCache!=None:
  111:              self._v_harvestDV.close();
  112:              
  113:     def getGetNeighbourhood(self,obj, wordStr, length=100,tagging=True):
  114:         """finde umgebung um die worte in wordStr, zurueckgegeben wird eine Array mit den Umgebungen von Fundstellen der Worte
  115:         alle Tags werden entfernt, die Fundstellen werden mit <span class="found">XX</span> getaggt, die Umgebungen werden 
  116:         case insensitive gesucht
  117:         @param wordStr: string mit Worten getrennt durch Leerzeichen, Phrasen sind mit " gekennzeichnet
  118:                         "eine phrase", "*"  bezeichnet wildcards und wird ignoriert"
  119:         @param length: optional, default wert 100, 2*length ist die groesse der Umgebung
  120:         @param tagging: optional default wert true, kein span tag wird erzweugt falls tag=false
  121:         """
  122:         
  123:         ret=[] # nimmt das Array auf, dass spaeter zurueckgegeben wird
  124:         ranges=[] #Array mit tupeln x,y wobei x die Position des Anfang und y des Endes der i-ten Umgebung angiebt
  125:         
  126:         wordStr=wordStr.lstrip().rstrip()
  127:         
  128:         def isInRanges(nr,length):
  129:             """test ob eine gegeben Position nr schon irgendwo in einer Umgebung ist, gibt den Index des ersten Wertes aus ranges zurueck, 
  130:             -1, wenn kein Treffer
  131:             
  132:             @param nr: Position die geprueft werden soll
  133:             @param length: Laenge des Wortes das geprueft werden soll
  134:             """
  135:             for x in ranges:
  136:                 if (x[0]<=nr) and (nr < (x[1]-length)):
  137:                     return ranges.index(x)
  138:             return -1
  139:                 
  140:         # deal with phrases, in Phrasen werden die Leerzeichen durch "_" ersetzt.
  141:         def rep_empty(str):
  142:             x= re.sub(" ","_",str.group(0))
  143:             return re.sub("\"","",x)
  144:             
  145:         wordStr=re.sub("\".*?\"", rep_empty,wordStr)#ersetze leerzeichen in " " durch "_" und loesche "
  146:         
  147:         #deal with wildcards, for our purposes it is enough to delete the wildcard 
  148:         wordStr=wordStr.replace("*","")
  149:         
  150:         words=wordStr.split(" ")
  151:         #if not words is ListType:
  152:         #   words=[words]
  153:         
  154:         
  155:         txtCache = self.en.getHarvestCache();
  156:         txt=  txtCache.get(obj.absolute_url(),None)
  157:        
  158:         if txt==None:
  159:             
  160:             logging.debug("NO CACHE for: "+obj.absolute_url())
  161:             txt=obj.harvest_page(mode="slim")
  162:         
  163:         
  164:         if not txt:
  165:             return ret
  166:         
  167:         soup = BeautifulSoup(txt)
  168:         
  169:         comments = soup.findAll(text=lambda text:isinstance(text, Comment))
  170:         [comment.extract() for comment in comments]
  171:         
  172:         txt = ''.join(soup.findAll(text=True))
  173:         
  174:         
  175:         #txt=re.sub("<.*?>", "", txt) # loesche alle Tags
  176:         for word in words:
  177:             word=re.sub("_"," ",word) # ersetze zurueck "_" durch " "
  178:             pos=0
  179:             
  180:             n=txt.lower().count(word.lower()) # wie oft tritt das Wort auf
  181: 
  182:             for i in range(n):
  183:                 pos=txt.lower().find(word.lower(),pos)
  184: 
  185:                 if pos > 0:
  186:                     x=max(0,pos-length)
  187:                     y=min(len(txt),pos+length)
  188:                   
  189:                     
  190:                     #is word already in one of the results
  191:                     nr=isInRanges(pos,len(word))
  192:                     if nr >=0:# word ist in einer schon gefunden Umgebung, dann vergroessere diese
  193:                         x=min(ranges[nr][0],x)
  194:                         y=max(ranges[nr][1],y)
  195:               
  196:                     str=txt[x:y]
  197:                     if x!=0: #add dots if in the middle of text
  198:                         str="..."+str
  199:                     
  200:                     if y!=len(txt): #add dots if in the middle of text
  201:                         str=str+"..."
  202:                         
  203:                         
  204:                 
  205:                     if nr >=0: # word ist in einer schon gefunden Umgebung
  206:                         ranges[nr]=(x,y) # neue Position der Umgebung
  207: 
  208:                         ret[nr]=str # neue Umgebung
  209:                     else: # andernfalls neue Umgebung hinzufuegen
  210:                         ranges.append((x,y))
  211: 
  212:                         ret.append(str)
  213:                     
  214:                     pos=pos+len(word)
  215:                 else:
  216:                     break;
  217:                 
  218:         # now highlight everything        
  219:         if tagging:
  220:             for x in range(len(ret)):
  221:                 for word in words:
  222:                     repl=re.compile(word,re.IGNORECASE)
  223:                     ret[x]=repl.sub(""" <span class="found">%s</span>"""%word.upper(),ret[x])
  224: 
  225:         return ret
  226:     def copyAllImagesToMargin(self):
  227:         """tranformiere alle Bilder in die Margins"""
  228:         projects=self.getTree()
  229:         ret=""
  230:         for project in projects:
  231:             proj=project[3]
  232:             try:
  233:                 persons=proj.copyImageToMargin();
  234:             except:
  235:                 logging.error("Cannnot do: %s"%repr(project))
  236:                 
  237:     def transformProjectsToId(self):
  238:         """trnasformiere zu ID, Hilfsfunktion die die alten Templates analysiert und mit der neuen Liste
  239:         verantwortlicher Personen versieht"""
  240:         projects=self.getTree()
  241:         ret=""
  242:         for project in projects:
  243:             
  244:             proj=project[3]
  245:             persons=proj.identifyNames(proj.getContent('xdata_01'))
  246:             if not hasattr(proj,'responsibleScientistsList'):
  247:                         proj.responsibleScientistsList=[]
  248:                         
  249:             for person in persons.items():
  250:               
  251:                 if len(person[1]) >1: #nicht eindeutig
  252:                     ret+="nicht eindeutig ---  %s:  %s\n"%(proj.getId(),person[0])
  253:                     
  254:                 elif len(person[1]) ==0: #kein eintrage
  255:                     ret+="kein eintrag---  %s:  %s\n"%(proj.getId(),person[0])
  256:                     proj.responsibleScientistsList.append((person[0],""))
  257:                 else:           
  258:                     proj.responsibleScientistsList.append((person[0],person[1][0].getObject().getKey()))
  259:         
  260:         return ret
  261:           
  262:                 
  263:     def harvestProjects(self):
  264:         """harvest"""
  265:         folder="/tmp"
  266:         try:
  267:             os.mkdir("/tmp/harvest_MPIWG")
  268:         except:
  269:             pass
  270:         founds=self.ZopeFind(self.aq_parent.projects,obj_metatypes=['MPIWGProject'],search_sub=1)
  271:         for found in founds:
  272:             txt=found[1].harvest_page()
  273:         
  274:             if txt and (txt != ""):
  275:                 name=found[0].replace("/","_")
  276:                 fh=file("/tmp/harvest_MPIWG/"+name,"w")
  277:                 fh.write(txt)
  278:                 fh.close()
  279:                 
  280:     def decode(self,str):
  281:         """decoder"""
  282: 
  283:         if not str:
  284:             return ""
  285:         if type(str) is StringType:
  286:             try:            
  287:                 return str.decode('utf-8')
  288:             except:
  289:                 return str.decode('latin-1')
  290:         else:
  291:             return str
  292: 
  293: 
  294:     def getat(self,array,idx=0,default=None):
  295:         """return array element idx or default (but no exception)"""
  296:         if len(array) <= idx:
  297:             return default
  298:         else:
  299:             return array[idx]
  300:         
  301:     def getLang(self):
  302:         """returns the default language"""
  303:         return self.lang
  304: 
  305:     def browserCheck(self):
  306:         """check the browsers request to find out the browser type"""
  307:         bt = {}
  308:         ua = self.REQUEST.get_header("HTTP_USER_AGENT")
  309:         bt['ua'] = ua
  310:         bt['isIE'] = False
  311:         bt['isN4'] = False
  312:         if string.find(ua, 'MSIE') > -1:
  313:             bt['isIE'] = True
  314:         else:
  315:             bt['isN4'] = (string.find(ua, 'Mozilla/4.') > -1)
  316: 
  317:         try:
  318:             nav = ua[string.find(ua, '('):]
  319:             ie = string.split(nav, "; ")[1]
  320:             if string.find(ie, "MSIE") > -1:
  321:                 bt['versIE'] = string.split(ie, " ")[1]
  322:         except: pass
  323: 
  324:         bt['isMac'] = string.find(ua, 'Macintosh') > -1
  325:         bt['isWin'] = string.find(ua, 'Windows') > -1
  326:         bt['isIEWin'] = bt['isIE'] and bt['isWin']
  327:         bt['isIEMac'] = bt['isIE'] and bt['isMac']
  328:         bt['staticHTML'] = False
  329: 
  330:         return bt
  331: 
  332: 
  333:     def versionHeaderEN(self):
  334:         """version header text"""
  335:         
  336:         date= self.REQUEST.get('date',None)
  337:         if date:
  338:             txt="""<h2>This pages shows the project which existed at %s</h2>"""%str(date)
  339:             return txt
  340:         return ""
  341: 
  342:     def versionHeaderDE(self):
  343:         """version header text"""
  344:         date= self.REQUEST.get('date',None)
  345:         if date:
  346:             txt="""<h2>Auf dieser Seite finden Sie die Projekte mit Stand vom %s</h2>"""%str(date)
  347:         return ""
  348:     
  349:         
  350:     def createOrUpdateId_raw(self):
  351:         """create sequence to create ids for bibliography"""
  352:         debug=None
  353:         #suche groesste existierende id
  354:         founds=self.ZSQLQuery("select id from bibliography")
  355:         
  356:         if founds:
  357:             ids=[int(x.id[1:]) for x in founds]
  358:             maximum=max(ids)
  359:             
  360:             id_raw=self.ZSQLQuery("select nextval('id_raw')",debug=debug)
  361:             
  362:             if id_raw:
  363:                 self.ZSQLQuery("drop sequence id_raw",debug=debug)
  364:             
  365:             self.ZSQLQuery("create sequence id_raw start %i"%(maximum+1),debug=debug)
  366:         
  367:     
  368:     def queryLink(self,link):
  369:         """append querystring to the link"""
  370:         return "%s?%s"%(link,self.REQUEST.get('QUERY_STRING',''))
  371: 
  372:     def getKategory(self,url):
  373:         """kategorie"""
  374:         splitted=url.split("/")
  375:         return splitted[4]
  376: 
  377:     def generateUrlProject(self,url,project=None):
  378:         """erzeuge aus absoluter url, relative des Projektes"""
  379:         if project:
  380:             splitted=url.split("/")
  381:             length=len(splitted)
  382:             short=splitted[length-2:length]
  383:             
  384:             base=self.REQUEST['URL3']+"/"+"/".join(short)
  385: 
  386:         else:
  387:             findPart=url.find("/projects/")
  388:             base=self.REQUEST['URL1']+"/"+url[findPart:]
  389: 
  390:                 
  391:         return base
  392:     
  393:     def isNewCapital(self,text=None,reset=None):
  394:         if reset:
  395:             self.REQUEST['capital']="A"
  396:             return True
  397:         else:
  398:             if len(text)>0 and not (text[0]==self.REQUEST['capital']):
  399:                 self.REQUEST['capital']=text[0]
  400:                 return True
  401:             else:
  402:                 return False
  403:     
  404:     def subNavStatic(self,obj):
  405:         """subnav" von self"""
  406:         subs=self.ZopeFind(obj,obj_metatypes=['MPIWGTemplate','MPIWGLink'])
  407:         subret=[]
  408: 
  409:         for x in subs:
  410:             if not(x[1].title==""):
  411:                 subret.append(x)
  412:         subret.sort(sortWeight)
  413:         return subret
  414:     
  415:     def subNav(self,obj):
  416:         """return sub-navigation elements i.e. below sections"""
  417:         # get section -> parent should be MPIWGRoot
  418:         p = obj
  419:         sec = None
  420:         # descend parents to the root (and remember the last id)
  421:         while p is not None and p.meta_type != 'MPIWGRoot':
  422:             sec = p
  423:             p = p.aq_parent
  424:     
  425:         subsecs = sec.objectItems(self.nav_meta_types)
  426:         subsecs = [s for s in subsecs if s[1].title != ""]
  427:         subsecs.sort(sortWeight)
  428:         return subsecs
  429:                 
  430:     def isType(self,object,meta_type):
  431:         """teste ob ein object vom meta_type ist."""
  432:         return (object.meta_type==meta_type)
  433:     
  434:     def isActive(self,name):
  435:         """teste ob subnavigation aktiv"""
  436:         for part in self.REQUEST['URL'].split("/"):
  437:             if part==name:
  438:                 return True
  439:         return False
  440:         
  441:     
  442:     def getSections(self):
  443:         """returns a list of all sections i.e. top-level MPIWGFolders"""
  444:         secs = self.objectItems(['MPIWGFolder'])
  445:         secs.sort(sortWeight)
  446:         #logging.debug("root: %s secs: %s"%(repr(self.absolute_url()), repr(secs)))
  447:         # return pure list of objects
  448:         return [s[1] for s in secs]
  449: 
  450:     def getSectionStyle(self, name, style=""):
  451:         """returns a string with the given style + '-sel' if the current section == name"""
  452:         if self.getSection() == name:
  453:             return style + '-sel'
  454:         else:
  455:             return style    
  456: 
  457:     def getFeatures(self, num=None):
  458:         """returns a list of the last num Features"""
  459:         dir = getattr(self, 'features')
  460:         features = dir.objectItems(['MPIWGFeature'])
  461:         features.sort(sortWeight)
  462:         if num is not None:
  463:             # take only the last num elements
  464:             features = features[-num:]
  465:         # return pure list of objects
  466:         return [f[1] for f in features]
  467: 
  468: 
  469:     def getMPIWGRoot(self):
  470:         """returns the MPIWG root"""
  471:         return self
  472: 
  473:     def MPIWGrootURL(self):
  474:         """returns the URL to the root"""
  475:         return self.absolute_url()
  476:         
  477:     def upDateSQL(self,fileName):
  478:         """updates SQL databases using fm.jar"""
  479:         fmJarPath=os.path.join(package_home(globals()), 'updateSQL/fm.jar')
  480:         xmlPath=os.path.join(package_home(globals()), "updateSQL/%s"%fileName)
  481:         logger("MPIWG Web",logging.INFO,"java -classpath %s -Djava.awt.headless=true Convert %s"%(fmJarPath,xmlPath))
  482:         ret=os.popen("java -classpath %s -Djava.awt.headless=true Convert %s"%(fmJarPath,xmlPath),"r").read()
  483:         logger("MPIWG Web",logging.INFO,"result convert: %s"%ret)
  484:         return 1
  485:     
  486:     def patchProjects(self,RESPONSE):
  487:         """patch"""
  488:         projects=self.ZopeFind(self.projects,obj_metatypes=['MPIWGProject'])
  489:         for project in projects:
  490:                 tmp=project[1].WEB_project_description[0].replace("/CD/projects/","")[0:]
  491:                 setattr(project[1],'WEB_project_description',[tmp[0:]])
  492:                 RESPONSE.write("<p>%s</p>\n"%project[0])
  493:             
  494:     def replaceNotEmpty(self,format,field):
  495:         """replace not empty"""
  496:         if field and (not field.lstrip()==''):
  497:             return self.decode(format%field)
  498:         else:
  499:             return ""
  500:         
  501: 
  502:     def isActiveMember(self,key):
  503:         """tested ob Mitarbeiter key ist aktiv"""
  504:         key=utf8ify(key)
  505:         ret=self.getat(self.ZSQLInlineSearch(_table='personal_www',
  506:                                             _op_key='eq',key=key,
  507:                                             _op_publish_the_data='eq',
  508:                                             publish_the_data='yes'))
  509:         
  510:         logging.info("ACTIVE_MEMBER  %s"%ret)
  511:         if ret:
  512:             return True
  513:         else:
  514:             return False
  515:         
  516:     def isActual(self,project):
  517:         """checke if project is actual"""
  518:         actualTime=time.localtime()
  519:         
  520:         if hasattr(project,'getObject'): #obj ist aus einer catalogTrefferList
  521:             obj=project.getObject()
  522:         else:
  523:             obj=project
  524:             
  525:         if getattr(obj,'archiveTime',actualTime)< actualTime:
  526:             return False
  527:         else:
  528:             return True
  529:         
  530:     def redirectIndex_html(self,request):
  531:         #return request['URL1']+'/index_html'
  532:         
  533:         return urllib.urlopen(request['URL1']+'/index_html').read()
  534: 
  535:     
  536:     def formatBibliography(self,here,found):
  537:         """format"""
  538:         return formatBibliography(here,found)
  539:     
  540:     def getValue(self,fieldStr):
  541:         """Inhalt des Feldes"""
  542:         
  543:         if type(fieldStr)==StringType:
  544:             field=fieldStr
  545:         else:
  546:             field=fieldStr[0]
  547:         try:
  548:             if field[len(field)-1]==";":
  549:                 field=field[0:len(field)-1]
  550:         except:
  551: 
  552:             """nothing"""
  553:         field=re.sub(r';([^\s])','; \g<1>',field)
  554:         return field.encode('utf-8')
  555: 
  556: 
  557:     
  558:     def sortedNames(self,list):
  559:         """sort names"""
  560: 
  561:         def sortLastName(x_c,y_c):
  562:             try:
  563:                 x=urllib.unquote(x_c).encode('utf-8','ignore')
  564:             except:
  565:                 x=urllib.unquote(x_c)
  566: 
  567:             try:
  568:                 y=urllib.unquote(y_c).encode('utf-8','ignore')
  569:             except:
  570:                 x=urllib.unquote(y_c)
  571:                 
  572: 
  573:             
  574:             try:
  575:                 last_x=x.split()[len(x.split())-1]
  576:                 last_y=y.split()[len(y.split())-1]
  577: 
  578:             except:
  579: 
  580:                 last_x=""
  581:                 last_y=""
  582:             
  583:             
  584:             
  585:             if last_x<last_y:
  586:                 return 1
  587:             elif last_x>last_y:
  588:                 return -1
  589:             else:
  590:                 return 0
  591:             
  592:         list.sort(sortLastName)
  593:         list.reverse()
  594:         
  595:         return list
  596:     
  597:     def __init__(self, id, title):
  598:         """init"""
  599:         self.id=id
  600:         self.title=title
  601: 
  602:     def removeStopWords(self,xo):
  603:         """remove stop words from xo"""
  604:         if not hasattr(self,'_v_stopWords'):
  605:             self._v_stopWords=self.stopwords_en.data.split("\n")
  606:     
  607:         x=str(xo)
  608:     
  609:         strx=x.split(" ")
  610:   
  611:         for tmp in strx:
  612:      
  613:             if tmp.lower() in self._v_stopWords:
  614:                 del strx[strx.index(tmp)]
  615: 
  616:         return " ".join(strx)
  617:     
  618:     def urlQuote(self,str):
  619:         """quote"""
  620:         return urllib.quote(str)
  621: 
  622:     def urlUnQuote(self,str):
  623:         """quote"""
  624:         return urllib.unquote(str)
  625:     
  626:         
  627: 
  628:     def getProjectsByFieldContent(self,fieldName,fieldContentsEntry, date=None):
  629:         """gib alle Projekte aus mit Value von field mit fieldName enthaelt ein Element der Liste fieldContents"""
  630:         def sort(x,y):
  631:                 return cmp(x.WEB_title[0],y.WEB_title[0])
  632: 
  633:         if type(fieldContentsEntry) is StringType:
  634:             fieldContentsTmp=[fieldContentsEntry]
  635:         else:
  636:             fieldContentsTmp=fieldContentsEntry
  637: 
  638:         fieldContents=[]
  639:         for x in fieldContentsTmp:
  640:             fieldContents.append(" AND ".join(x.split()))
  641:         projects=self.ProjectCatalog({fieldName:string.join(fieldContents,' AND')})
  642:         #print projects
  643:         #ret=[x for x in projects]
  644:         ret=[]
  645:         for x in projects:
  646:             obj=x.getObject()
  647:             obj=obj.getActualVersion(date)
  648:             if obj and (not getattr(obj,'invisible',None)):
  649:                 #if not (x in ret):
  650:                     ret.append(x)
  651: 
  652:         ret.sort(sort)
  653:         return ret
  654: 
  655:     def changeMPIWGRootForm(self):
  656:         """edit"""
  657:         pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','changeMPIWGRootForm')).__of__(self)
  658:         return pt()
  659: 
  660:     def changeMPIWGRoot(self,title,connection_id,coneServiceURL,harvestPort,harvestServer,lang=None,autocommit=None,RESPONSE=None):
  661:         """change"""
  662:         self.title=title
  663:         self.connection_id=connection_id
  664:         #self.disciplineList=disciplineList
  665:         #self.themesList=themesList
  666:         self.coneServiceURL=coneServiceURL
  667:         self.harvestServer=harvestServer
  668:         try:
  669:             self.harvestPort=int(harvestPort)
  670:         except:
  671:             logging.error("couldn't change port!: no number:"+harvestPort)
  672:         
  673:         if lang is not None:
  674:             self.lang = lang
  675: 
  676:         self.autocommit = (autocommit == "on")
  677:         if RESPONSE is not None:
  678:             RESPONSE.redirect('manage_main')
  679: 
  680: 
  681:     def getContexts(self,childs=None,parents=None,depth=None,date=None,onlyActive=True):
  682:         """childs alle childs, alle parents"""
  683:         ret=[]
  684:     
  685:         if parents:
  686:             pnums=parents.split(".")
  687:             while len(pnums) > 1:
  688:                 pnums.pop()
  689:                 parentId=string.join(pnums,".")
  690:         
  691:                 for project in self.getProjectFields('xdata_05',sort='int',date=date):
  692:                     if project[1]==parentId:
  693:                         ret.append(project)
  694:                 
  695:                 if (depth is not None) and (len(ret) >= depth):
  696:                     break
  697: 
  698:         if childs:
  699:             for project in self.getProjectFields('xdata_05',sort='int',date=date):
  700:                 searchStr=childs+"(\..*)"
  701:                
  702:                 if (onlyActive and project[0].isActiveProject()) or (not onlyActive):
  703:                     if re.match(searchStr,project[1]):
  704:                         
  705:                         if depth:
  706:     
  707:                             if int(depth)>=len(project[1].split("."))-len(childs.split(".")):
  708:                         
  709:                                 ret.append(project)
  710:                         else:
  711:                             ret.append(project)
  712:         
  713:         #logging.debug("getContexts: childs=%s parents=%s depth=%s => %s"%(childs,parents,depth,repr(ret)))
  714:      
  715:         return ret
  716: 
  717:     
  718:     def getProjectFields(self,fieldName,date=None,folder=None,sort=None):
  719:         """getListofFieldNames"""
  720:         ret=[]
  721:     
  722:         objects=self.ZopeFind(self.projects,obj_metatypes=['MPIWGProject'],search_sub=0)
  723: 
  724:                 
  725:         for object in objects:
  726:             obj=object[1]
  727:             obj=obj.getActualVersion(date)
  728:             if obj and (not getattr(obj,'invisible',None)):
  729:                 if fieldName=="WEB_title_or_short":
  730: 
  731:                     if len(obj.getContent('xdata_07'))<3: # hack weil z.Z. manchmal noch ein Trennzeichen ; oder , im Feld statt leer
  732:                         fieldNameTmp="WEB_title"
  733:                     else:
  734:                         fieldNameTmp="xdata_07"
  735:                 else:
  736:                     fieldNameTmp=fieldName
  737: 
  738:                 ret.append((obj,obj.getContent(fieldNameTmp)))
  739: 
  740:         
  741:         if sort=="int":
  742:             ret.sort(sortI)
  743:         elif sort=="stopWords":
  744:  
  745:             ret.sort(sortStopWords(self))
  746:             
  747:         else:
  748:             ret.sort(sortF)
  749:         
  750:         return ret
  751: 
  752:     def showNewProjects(self):
  753:         projects=[]
  754:         for objs in self.getProjectFields('WEB_title_or_short'): # Get all Projets
  755:             if objs[0].xdata_05 and (objs[0].xdata_05[0] == ""):
  756:                 
  757:                 projects.append(objs)
  758:                 
  759:         return projects
  760:     
  761:         
  762:     def updatePublicationDB(self,personId=None):
  763:         """updates the publication db, i.e. copy year and type into the main table"""
  764:         
  765:         if personId:
  766:             founds = self.ZSQLInlineSearch(_table="publications",key_main=personId)
  767:         else:
  768:             founds = self.ZSQLInlineSearch(_table="publications")
  769:             
  770:         for found in founds:
  771:                         
  772:             if found.id_institutsbibliographie and (not found.id_institutsbibliographie =="") and (not found.id_institutsbibliographie =="0"):
  773:                 
  774:                 entries = self.ZSQLInlineSearch(_table="institutsbiblio",id=found.id_institutsbibliographie)
  775:                 for entry in entries:
  776:                     self.ZSQLChange(_table='publications',_identify='oid=%s' % found.oid,year=entry.year,referencetype=entry.reference_type)
  777:                     
  778:             if found.id_gen_bib and (not found.id_gen_bib ==""):
  779:                 entries = self.ZSQLInlineSearch(_table="bibliography",id=found.id_gen_bib)
  780:                 for entry in entries:
  781:                     self.ZSQLChange(_table='publications',_identify='oid=%s' % found.oid,year=entry.year,referencetype=entry.reference_type)
  782:                     
  783:         return True        
  784:     
  785:     def showNewDBEntries(self):
  786:         """zeige neue Eintraege in der Datenbank ohne e-mail adressen bzw. fuer die noch kein Object angelegt wurde"""
  787:         
  788:         qstr="select * from personal_www where web_object_created='no' and not key=''"
  789:         res=self.ZSQLQuery(qstr)
  790:         
  791:         pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','showNewDBEntries.zpt')).__of__(self)
  792:         return pt(newEntries=res)
  793:    
  794:     def createNewStaffObjects(self,RESPONSE):
  795:         """create new staff object"""
  796:         
  797:         memberFolder=getattr(self,'members')
  798:         args=self.REQUEST.form
  799:         arg_k=args.keys()
  800:         arg_k.remove("submit")
  801:         ret=""
  802:         for key in arg_k:
  803:             k=self.urlUnQuote(key)
  804:           
  805:             qstr="select * from personal_www where key=%s"%self.ZSQLQuote(k)
  806:             res=self.ZSQLQuery(qstr)[0]
  807:             if args[key]!="": #email-adresse wurde eingetragen
  808:                 #create the object
  809:                 e_mail=args[key]
  810:                 try:
  811:                     newObj=MPIWGStaff.MPIWGStaff(e_mail,res.last_name,res.first_name,k)
  812:                     memberFolder._setObject(e_mail,newObj)
  813:                     obj=getattr(memberFolder,e_mail)
  814:                     obj.reindex_object()
  815:                     ret+="Created %s \n"%e_mail
  816:                     created=True
  817:                 except:
  818:                     msg="Cannot create new user %s (%s %s)"%(e_mail,sys.exc_info()[0],sys.exc_info()[1])
  819:                     logging.error(msg)
  820:                     ret+=msg+"\n"
  821:                     created=False
  822:                 
  823:                 if created:
  824:                     qstr="update personal_www set web_object_created='yes',e_mail='%s@mpiwg-berlin.mpg.de' where key=%s"%(e_mail,self.ZSQLQuote(k))
  825:                     self.ZSQLQuery(qstr)
  826:         
  827:         return ret
  828:                    
  829:         
  830:     def generateNewPersonEntry(self,data):
  831:         """generate a new person entry for data, neue personen werden zunaechst nur in der datenbank angelegt """
  832:         
  833:         #memberFolder=getattr(self,'members')
  834:         #create the object
  835:         
  836: #        try:
  837: #            newObj=MPIWGStaff.MPIWGStaff(urllib.quote(data['key']),data['last_name'].encode('utf-8'),data['first_name'].encode('utf-8')) 
  838: #            memberFolder._setObject(urllib.quote(data['key']),newObj)
  839: #        except:
  840: #            return False, "Cannot create new user %s (%s %s)"%(data['key'],sys.exc_info()[0],sys.exc_info()[1])
  841: #        
  842:         
  843:         #create the new entry in the database
  844:         
  845:         
  846:         result,msg=MPIWGStaff.createNewDBEntry(self,data['publish_the_data'],data['key'],data['last_name'],
  847:                                   data['first_name'],data['titles_new'],data['status'],"",
  848:                                   "",data['date_from'],data['date_to'],
  849:                                   data['department'],'',data['funded_by'],
  850:                                   data['e_mail2'],data['current_work'],"yes",data['date_stay_at_mpiwg'],data['group'],"no",data['current_work'])
  851:         
  852:         return result,msg
  853:  
  854:     def updatePersonEntry(self,data,ignoreEntries=None):
  855:         """update an person entry from data. but ignore all fields in ignore Entries"""
  856:         if ignoreEntries is None:
  857:             ignoreEntries = []
  858:             
  859:         #ignoreEntries.append('current_work') # TODO:updatecurrent work
  860:         logging.debug("updatePersonEntry: data=%s ignoreEntries=%s"%(repr(data),repr(ignoreEntries)))
  861:         
  862:         if data['date_to']=="": # wenn date_to leer
  863:              data['date_to']="date_none"
  864:         
  865:         if data['date_from']=="": # wenn date_fromleer
  866:              data['date_from']="date_none"
  867:         msg=""
  868:    
  869:         
  870:         #eintragen
  871:          
  872:         columns=data.keys()
  873:         for x in ignoreEntries:
  874:             logging.debug("updatePersonEntry: ignoring %s"%x)
  875:             try: #falls in ignore entries felder sind, die nicht in columns sind, fange den fehler ab
  876:                 columns.remove(x)
  877:             except:
  878:                 pass
  879: 
  880:         
  881:         insert=[]
  882:         for key in columns:
  883:             if data[key]=="date_none": # date_none eintrag wird zu null uebersetzt
  884:                 insert.append('%s=null'%key)
  885:             else:
  886:                 insert.append(""" "%s"=%s"""%(key,self.ZSQLQuote(data[key])))
  887:             
  888:         insertStr=",".join(insert)
  889:         queryStr="update personal_www SET %s where key='%s'"%(insertStr,data['key'])
  890:         self.ZSQLQuery("SET DATESTYLE TO 'German'")
  891:         self.ZSQLQuery(queryStr)
  892:        
  893:         #currentwork
  894:         #if not (txt==""):
  895:         #    queryStr="INSERT INTO current_work (id_main,current,publish) VALUES ('%s','%s','%s')"%(id,txt,txt_p)
  896:         #
  897:         #    self.ZSQLQuery(queryStr)
  898:         
  899:         return True,msg
  900: 
  901: 
  902:     def updatePersonalwww_doIt(self):
  903:         """do the update"""
  904:         args=self.REQUEST.form
  905:         resultSet=self.REQUEST.SESSION['personal_www']['resultSet']
  906:         news=self.REQUEST.SESSION['personal_www']['news']
  907:         conflicts=self.REQUEST.SESSION['personal_www']['conflicts']
  908:         logging.debug("updatePersonalwww_doIt: args=%s\n  resultSet=%s\n  news=%s\n  conflicts=%s"%(args,resultSet,news,conflicts))
  909:         
  910:         ret="<html><body>"
  911:         # generate the new entry
  912:       
  913:         if news and (len(news)>0):
  914:             ret+="<h2>Hinzugef&uuml;gt</h2>"
  915:             ret+="<p>Neueintr&auml;ge erscheinen erst auf der Homepage, wenn ihnen eine e-mail Adresse zugeordnet wurde.</p>"
  916:             ret+="<ul>"
  917:             
  918:         for new in news:
  919:             if args.has_key(self.urlQuote(new.encode('utf-8'))): # entry was selected
  920:                 result,msg=self.generateNewPersonEntry(resultSet[new])
  921:                 if not result:
  922:                     logging.error("Error (generateNewPersonEntry) %s"%msg)
  923:                     ret+="<li>ERROR: %s %s"%(new.encode('utf-8'),msg)
  924:                 else:
  925:                     ret+="<li>OK: %s"%(new.encode('utf-8'))
  926:                     
  927:         if news and (len(news)>0):
  928:             ret+="<p>Neueintr&auml;ge erscheinen erst auf der Homepage, wenn ihnen eine e-mail Adresse zugeordnet wurde.</p>"
  929:             ret+="</ul>"     
  930:         
  931:         # update
  932: 
  933:         if len(conflicts.keys())>0:
  934:             ret+="<h2>&Auml;nderung des Benutzers &uuml;bernehmen</h2>"
  935:             ret+="<p>Wenn n&ouml;tig in Filemaker-db &auml;ndern:</p>"
  936:             
  937:         # konflicte   
  938:         for conflict in conflicts.keys():
  939:             ignoreEntries=[]
  940:             displayIgnored=[]
  941:             for cf in conflicts[conflict]:
  942:                 if args[conflict.encode('utf-8')+'_'+cf[0]]=="stored": #use the stored one
  943:                     ignoreEntries.append(cf[0])  #so ignore field cf[0]       
  944:                     displayIgnored.append(cf)
  945:                     
  946:             if len(displayIgnored)>0:
  947:                 ret+="<h3>%s</h3>"%conflict.encode('utf-8')
  948:                 ret+="<table border='1'>"
  949:                 for iE in displayIgnored:
  950:                     ret+="<tr><td>%s</td><td>%s</td><td>%s</td>"%(iE[0].encode('utf-8'),iE[1].encode('utf-8'),iE[2].encode('utf-8'))
  951:                 ret+="</table>"
  952:                 
  953:             self.updatePersonEntry(resultSet[conflict],ignoreEntries=ignoreEntries)
  954:          
  955:          # rest
  956:         cl=list(conflicts.keys())
  957:         
  958:         for key in resultSet.keys():
  959:              if key not in cl:
  960:                  self.updatePersonEntry(resultSet[key])
  961:         return ret+"</body></html>"
  962:                      
  963: 
  964:     def updateInstitutsbiliography(self):
  965:         """update the Institutsbibliogrpahy"""
  966:         self.upDateSQL('personalwww.xml')
  967:         return "<html><body>DONE</body></html>"
  968: 
  969: 
  970:     
  971: 
  972:     def updatePersonalwww_html(self):
  973:         """update form for the homepages web form"""
  974:         pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','updatePersonalwww.zpt')).__of__(self)
  975:         return pt()
  976: 
  977:     
  978:     def updatePersonalwww(self,uploadfile):
  979:         """update personalwww
  980:         @param uploadfile: file handle auf das file
  981:         """
  982:         dsn=self.getConnectionObj().connection_string
  983:         #dsn="dbname=personalwww"
  984:         resultSet=updatePersonalWWW.importFMPXML(uploadfile)
  985:         news,conflicts=updatePersonalWWW.checkImport(dsn, resultSet)
  986: 
  987:         self.REQUEST.SESSION['personal_www']={}
  988:         self.REQUEST.SESSION['personal_www']['resultSet']=resultSet
  989:         self.REQUEST.SESSION['personal_www']['news']=news
  990:         self.REQUEST.SESSION['personal_www']['conflicts']=conflicts
  991:         
  992:         pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','updatePersonalwww_check.zpt')).__of__(self)
  993:         return pt()
  994:     
  995: 
  996:      
  997:     def reindexCatalogs(self,RESPONSE=None):
  998:         """reindex members and project catalog"""
  999:         
 1000:         
 1001:         try:
 1002:             
 1003:             self.ProjectCatalog.manage_catalogReindex(self.REQUEST,RESPONSE,self.REQUEST['URL1'])
 1004:             logger("MPIWG Root (reindexCatalog: projects)",logging.INFO,"DONE")
 1005:         except:
 1006:             logger("MPIWG Root (reindexCatalog: projects)",logging.WARNING," %s %s"%sys.exc_info()[:2])
 1007:         
 1008:         try:
 1009:             
 1010:             self.MembersCatalog.manage_catalogReindex(self.REQUEST,RESPONSE,self.REQUEST['URL1'])
 1011:             logger("MPIWG Root (reindexCatalog: members)",logging.INFO,"DONE")
 1012:         except:
 1013:             logger("MPIWG Root (reindexCatalog: members)",logging.WARNING," %s %s"%sys.exc_info()[:2])
 1014:         
 1015:         try:
 1016:             
 1017:             self.fulltextProjectsMembers.manage_catalogReindex(self.REQUEST,RESPONSE,self.REQUEST['URL1'])
 1018:             logger("MPIWG Root (reindexCatalog: fulltextProjectsMembers)",logging.INFO,"DONE")
 1019:         except:
 1020:             logger("MPIWG Root (reindexCatalog: fulltextProjectsMembers)",logging.WARNING," %s %s"%sys.exc_info()[:2])
 1021:             
 1022:         
 1023:         
 1024:         
 1025:         
 1026:         
 1027:         if RESPONSE:
 1028:             RESPONSE.redirect('manage_main')
 1029: 
 1030:         
 1031:         
 1032: 
 1033:     def getAllMembers(self):
 1034:         #ret=[]
 1035:         
 1036:         def sorter(x,y):
 1037:             return cmp(x[0],y[0])
 1038:                     
 1039:         results=self.MembersCatalog({'isPublished':True})
 1040:        
 1041:         ret=[(unicodify(", ".join([proj.lastName, proj.firstName])), proj.getKey) for proj in results]
 1042:         
 1043:         ret.sort(sorter)
 1044:         return ret
 1045:     
 1046:     
 1047:     def printAllMembers(self):
 1048:         """print"""
 1049:         members=self.getAllMembers()
 1050:         ret=""
 1051:         for x in members:
 1052:             ret+="<p>%s</p>"%x
 1053:         return ret
 1054:     
 1055:         
 1056:     def makeList(self,entry):
 1057:         """makes a list out of one entry or repeat a list"""
 1058:         if type(entry) is StringType:
 1059:             return [entry]
 1060:         else:
 1061:             return entry
 1062: 
 1063:     def getTreeRSS(self,dep=None,date=None,onlyActive=1,onlyArchived=0):
 1064:         """generateTree"""
 1065:         rss="""<?xml version="1.0" encoding="utf-8"?>
 1066:                  <rss version="2.0">
 1067:                    <channel>"""
 1068:         
 1069:         for obj in self.getTree(dep, date, onlyActive, onlyArchived):
 1070:             linkStr="""<link>http://www.mpiwg-berlin.mpg.de/en/research/projects/%s</link>"""
 1071:             rss+="""<item>"""
 1072:             rss+=linkStr%obj[3].getId()
 1073:             rss+="""</item>"""
 1074:             if hasattr(obj[3],'publicationList'):
 1075: 	        rss+="""<item>"""
 1076:                 rss+=linkStr%(obj[3].getId()+"/publicationList");
 1077:                 rss+="""</item>"""
 1078:         rss+="""</channel>
 1079:         </rss>"""
 1080: 
 1081:         
 1082:         return rss
 1083: 
 1084:     def getTree(self,dep=None,date=None,onlyActive=0,onlyArchived=0):
 1085:         """generate Tree from project list
 1086:         als Liste, jeder Eintrag ist ein Tupel ,(Tiefe, ProjektNummer,ProjektObject
 1087:         onlyActive = 0  : alle Projekte
 1088:         onlyActive = 1 : nur active Projekte
 1089:         onlyActive = 2: nur inactive Projekte
 1090:         
 1091:         onlyArchived=0: alle Projekte
 1092:         onlyArchived= 1 : nur aktuelle Projekte
 1093:         onlyArchived = 2: nur archivierte Projekte
 1094:         
 1095:         department fuer das Tree geholt werden soll
 1096:         """
 1097: 
 1098:         returnListTmp=[]
 1099:         returnList=[]
 1100:         
 1101:         for project in self.getProjectFields('xdata_05',sort="int",date=date): # get Projects sorted by xdata_05
 1102: 
 1103:             for idNr in project[1].split(";"): # more than one number
 1104:                 if not idNr=="":
 1105:                     splittedId=idNr.split(".")
 1106:                     depth=len(splittedId)
 1107:                     nr=idNr
 1108:                     #title=project[0].WEB_title
 1109:                     title=[project[0].getContent('WEB_title')]
 1110:                     #print title
 1111:                     
 1112:                     if idNr[0]=="x": # kompatibilitaet mit alter Konvention, x vor der Nummer macht project inactive
 1113:                         project[0].setActiveFlag(False)
 1114:                    
 1115:                     if (not dep) or (splittedId[0]==dep): #falls dep gesetzt ist nur dieses hinzufuegen.
 1116:                         
 1117:                         if (onlyActive==0):
 1118:                             returnListTmp.append((depth,nr,title,project[0]))
 1119:                         elif (onlyActive==1) and project[0].isActiveProject(): #nur active projekte
 1120:                             returnListTmp.append((depth,nr,title,project[0]))
 1121:                         elif (onlyActive==2) and (not project[0].isActiveProject()): #nur active projekte
 1122:                             returnListTmp.append((depth,nr,title,project[0]))
 1123:                    
 1124:                    
 1125:         #filter jetzt die Liste nach Archived oder nicht
 1126:         for entry in returnListTmp:
 1127:                     if (onlyArchived==0):
 1128:                             returnList.append(entry)
 1129:                     elif (onlyArchived==1) and (not entry[3].isArchivedProject()): #nur active projekte
 1130:                             returnList.append(entry)
 1131:                     elif (onlyArchived==2) and (entry[3].isArchivedProject()): #nur active projekte
 1132:                             returnList.append(entry)
 1133:                    
 1134:         
 1135:         return returnList
 1136: 
 1137: 
 1138:         
 1139:     def changePosition(self,treeId,select,RESPONSE=None):
 1140:         """Change Postion Entry"""
 1141:         numbers=[]
 1142: 
 1143:         # Suche hoechste bisherige nummer
 1144:         projects=self.getProjectFields('xdata_05') # get Projects sorted by xdata_05
 1145:         #print "pj",projects
 1146:         for project in projects: #suche alle subtrees der treeId
 1147:             #print treeId
 1148:             
 1149:             founds=re.match(treeId+"\.(.*)",project[1].split(";")[0])
 1150:             if founds:
 1151:                 #print "x",founds.group(0),len(founds.group(0).split("."))
 1152:                 if len(founds.group(0).split("."))==len(treeId.split("."))+1: # nur ein punkt mehr, d.h. untere ebene
 1153:                     try:
 1154:                         numbers.append(int(founds.group(0).split(".")[len(founds.group(0).split("."))-1]))
 1155:                     except:
 1156:                         numbers.append(int(0))
 1157: 
 1158:         try:
 1159:             highest=max(numbers)
 1160:         except:
 1161:             highest=0
 1162:         projects=self.showNewProjects()
 1163:         for i in self.makeList(select):
 1164:             highest+=10
 1165:             projects[int(i)][0].xdata_05=treeId+"."+str(highest)
 1166: 
 1167: 
 1168:         if RESPONSE is not None:
 1169:             RESPONSE.redirect('showTree')
 1170:         
 1171:     def changeTree(self,RESPONSE=None):
 1172:         """change the complete tree"""
 1173:         form=self.REQUEST.form
 1174:         hashList={}
 1175:         onlyArchived=int(form.get("onlyArchived",0))
 1176:         onlyActive=int(form.get("onlyActive",0))
 1177:         dep=form.get("dep",None)
 1178:         
 1179:         fields=self.getTree(dep=dep,onlyArchived=onlyArchived,onlyActive=onlyActive)
 1180:         
 1181:         logging.info("GOT TREE!----------------------------------------------------")
 1182:         for field in form.keys():
 1183:             
 1184:             splitted=field.split('_')
 1185:             if (len(splitted)>1) and (splitted[1]=="runningNumber"): #feld hat die Form Nummer_name und runnignNumber
 1186:             
 1187:                 
 1188:                 nr=int(splitted[0]) # nummer des Datensatzes
 1189:                 currentEntry = fields[nr]
 1190:             
 1191:                 if form.has_key(str(nr)+'_active'): # active flag is set
 1192:                     fields[nr][3].setActiveFlag(True)
 1193:                 else:
 1194:                     fields[nr][3].setActiveFlag(False)
 1195:                     
 1196:                 #nummer hat sich geaendert
 1197:                 
 1198:                 entryChanged = False;
 1199:                 
 1200:                 if isinstance(fields[nr][3].xdata_05,list): #for some reasons somtimes the content of the field is a list with one entry.
 1201:                     fields[nr][3].xdata_05=fields[nr][3].xdata_05[0]
 1202:                     
 1203:                 if not (fields[nr][3].xdata_05==form[str(nr)+'_number']):
 1204:                     logging.info("Changed!Number+++++++++++++++++++++++++++++++++")
 1205:                     logging.info(repr(fields[nr][3].xdata_05)+" ---> "+ repr(form[str(nr)+'_number']))
 1206:                     fields[nr][3].xdata_05=form[str(nr)+'_number']
 1207:                     entryChanged = True
 1208:                     
 1209:                 #completed har sich geaendert
 1210:                           
 1211:                 td = fields[nr][3].transformDate # hole die funktion zum transformieren des datums
 1212:                   
 1213:                 if not (td(fields[nr][3].getCompletedAt())==td(form[str(nr)+'_completed'])):
 1214:                     fields[nr][3].setCompletedAt(form[str(nr)+'_completed'])
 1215:                     logging.info(repr(td(fields[nr][3].getCompletedAt()))+" ---> "+ repr(td(form[str(nr)+'_completed'])))
 1216:                     logging.info("Changed!Completed+++++++++++++++++++++++++++++++++")
 1217:                     entryChanged = True
 1218:                 
 1219:                 if not (td(fields[nr][3].getStartedAt())==td(form[str(nr)+'_started'])):
 1220:                     fields[nr][3].setStartedAt(form[str(nr)+'_started'])
 1221:             
 1222:                     logging.info(repr(td(fields[nr][3].getStartedAt()))+" ---> "+ repr(td(form[str(nr)+'_started'])))
 1223:                     logging.info("Changed!Started+++++++++++++++++++++++++++++++++")
 1224:                     entryChanged = True
 1225:                 
 1226:                 
 1227:                 if entryChanged:
 1228:                     logging.info("Changed!+++++++++++++++++++++++++++++++++")
 1229:                     fields[nr][3].copyObjectToArchive()
 1230:                 
 1231:                     
 1232:         if RESPONSE is not None:
 1233:             RESPONSE.redirect('showTree')
 1234: 
 1235:     def getProjectWithId(self,id):
 1236:         fields=self.getProjectFields('xdata_05')
 1237:         for field in fields:
 1238:             if field[1]==id:
 1239:                 return field[0]
 1240: 
 1241:         return None
 1242:             
 1243:         
 1244:             
 1245:         
 1246:     def getRelativeUrlFromPerson(self,list):
 1247:         """get urls to person list"""
 1248:         ret=[]
 1249:         persons=list.split(";")
 1250:         for person in persons:
 1251:             
 1252:             if len(person)>1: #nicht nur Trennzeichen
 1253:                 splitted=person.split(",")
 1254:                 if len(splitted)==1:
 1255:                     splitted=person.split(" ")
 1256:                 splittedNew=[re.sub(r'\s(.*)','$1',split) for split in splitted]
 1257:                 if splittedNew[0]=='':
 1258:                     del splittedNew[0]
 1259:                 search=string.join(splittedNew,' AND ')
 1260:                 
 1261:                 if not search=='':
 1262: 
 1263:                     try:
 1264:                         proj=self.MembersCatalog({'title':search})
 1265:                     except:
 1266:                         proj=None
 1267: 
 1268:                 if proj:
 1269:                     #ret.append("<a href=%s >%s</a>"%(proj[0].absolute_url,person.encode('utf-8')))
 1270:                     ret.append("<a href=%s >%s</a>"%('members/'+proj[0].id+'/index.html',person))
 1271:                 else:
 1272:                     #ret.append("%s"%person.encode('utf-8'))
 1273:                     ret.append("%s"%person)
 1274:         return string.join(ret,";")
 1275:         
 1276:     def getMemberIdFromKey(self,key):
 1277:         """gibt die ensprechende id  im members Ordner zum key"""
 1278:         
 1279:         if key=="":
 1280:             return ""
 1281:         try:
 1282:             key=utf8ify(key)
 1283:             catalogged=self.MembersCatalog({'getKey':key})
 1284:             if len(catalogged)==0:
 1285:                 return ""
 1286:             else:
 1287:                 return catalogged[0].getObject().getId()
 1288:         
 1289:         except:
 1290:             return ""
 1291: 
 1292:             
 1293: 
 1294:     def getProjectsOfMembers(self,date=None):
 1295:         """give tuple member /projects"""
 1296:         ret=[]
 1297:         members=self.getAllMembers()
 1298:         logging.error("X %s"%repr(members))
 1299:         #return str(members)
 1300:         for x in members:
 1301:             logging.error("X %s"%repr(x))
 1302:             projects=self.getProjectsOfMember(key=x[1],date=date)
 1303:             if len(projects)>0:
 1304:                 ret.append((x[0],projects))
 1305:             
 1306:         return ret
 1307: 
 1308:     def getProjectsOfMember(self,key=None,date=None,onlyArchived=1,onlyActive=1):
 1309:         """get projects of a member
 1310:     
 1311:         @param key: (optional) Key zur Idenfikation des Benutzer
 1312:         @param date: (optional) Version die zum Zeitpunkt date gueltig war
 1313:         @param onlyArchived: 
 1314:         onlyArchived=0: alle Projekte
 1315:         onlyArchived= 1 : nur aktuelle Projekte
 1316:         onlyArchived = 2: nur archivierte Projekte
 1317:         """
 1318:         # TODO: Die ganze Loesung
 1319:         def sortP(x,y):
 1320:             """sort by sorting number"""
 1321:             return cmp(x.WEB_title,y.WEB_title)
 1322:         
 1323:         ret=[]  
 1324:         if key:     
 1325:             logging.debug("MPIWGROOT (getProjectsOfMember):"+key)
 1326:             proj=self.ProjectCatalog({'getPersonKeyList':utf8ify(key)})
 1327:         else:
 1328:             return ret # key muss definiert sein
 1329:         
 1330:         logging.debug("MPIWGROOT (getProjectsOfMember):"+repr(proj))
 1331:         if proj:
 1332:             proj2=[]
 1333:             for x in proj:
 1334:                 #logging.error("proj:%s"%repr(x.getPath()))
 1335:                 if (not getattr(x.getObject(),'invisible',None)) and (getattr(x.getObject(),'archiveTime','')==''):   
 1336:                       proj2.append(x)
 1337: 
 1338:         else:
 1339:             proj2=[]
 1340:             
 1341:        
 1342:        
 1343:         proj2.sort(sortP)
 1344: 
 1345:         projectListe=[]
 1346:         #logging.error("getprojectsofmember proj2: %s"%repr(proj2))
 1347:         for proj in proj2:   
 1348:             obj=proj.getObject()
 1349:             add=False
 1350:             if onlyArchived==1: #nur aktuell projecte
 1351:                 if not obj.isArchivedProject():
 1352:                     add=True
 1353:             elif onlyArchived==2: #nur archivierte
 1354:                 if obj.isArchivedProject():
 1355:                     add=True
 1356:             else: #alle
 1357:                add=True 
 1358:                
 1359:             if onlyActive==1: #nur active projecte
 1360:                 if obj.isActiveProject():
 1361:                     add=add & True
 1362:                 else:
 1363:                     add=add & False
 1364:                 
 1365:             elif onlyArchived==2: #nur nicht aktvive
 1366:                 if not obj.isActiveProject():
 1367:                     add=add & True
 1368:             else: #alle
 1369:                add=add & True
 1370:                     
 1371:             if add:
 1372:                 projectListe.append(obj)
 1373:                 
 1374:         #logging.error("getprojectsofmember projectliste: %s"%repr(projectListe))
 1375:         return projectListe
 1376:      
 1377:     def givePersonList(self,name):
 1378:         """check if person is in personfolder and return list of person objects"""
 1379:         
 1380:         splitted=name.split(",")
 1381:         if len(splitted)==1:
 1382:             splitted=name.lstrip().rstrip().split(" ")
 1383:         splittedNew=[split.lstrip() for split in splitted]
 1384:         
 1385:         if splittedNew[0]=='':
 1386:             del splittedNew[0]
 1387:         search=string.join(splittedNew,' AND ')
 1388:         
 1389:         if not search=='':
 1390:             proj=self.MembersCatalog({'title':search})
 1391: 
 1392:         if proj:
 1393:             return [[x.lastName,x.firstName] for x in proj]
 1394:         else:
 1395:             return []
 1396:             
 1397: ##         splitted=name.split(",") # version nachname, vorname...
 1398: ##         if len(splitted)>1:
 1399: ##             lastName=splitted[0] 
 1400: ##             firstName=splitted[1]
 1401: ##         else: 
 1402: ##             splitted=name.split(" ") #version vorname irgenwas nachnamae
 1403:         
 1404: ##             lastName=splitted[len(splitted)-1]
 1405: ##             firstName=string.join(splitted[0:len(splitted)-1])
 1406: 
 1407: ##         objs=[]
 1408: 
 1409:         #print  self.members 
 1410:       ##   for x in self.members.__dict__:
 1411: ##             obj=getattr(self.members,x)
 1412: ##             if hasattr(obj,'lastName') and hasattr(obj,'firstName'):
 1413:                 
 1414: ##                 if (re.match(".*"+obj.lastName+".*",lastName) or re.match(".*"+lastName+".*",obj.lastName)) and (re.match(".*"+obj.firstName+".*",firstName) or re.match(".*"+firstName+".*",obj.firstName)):
 1415:                     
 1416: ##                     objs.append((obj,lastName+", "+firstName))
 1417: 
 1418:         
 1419: ##        return objs
 1420: 
 1421: 
 1422:     def personCheck(self,names):
 1423:         """all persons for list"""
 1424:         #print "names",names
 1425:         splitted=names.split(";")
 1426:         ret={}
 1427:         for name in splitted:
 1428: 
 1429:             if not (name==""):
 1430:                 try:
 1431:                     ret[name]=self.givePersonList(name)
 1432:                 except:
 1433:                     """NOTHIHN"""
 1434:         #print "RET",ret
 1435:         return ret
 1436: 
 1437:     def giveCheckList(self,person,fieldname):
 1438:         """return checklist"""
 1439:         #print "GCL",fieldname
 1440:         if fieldname=='xdata_01':
 1441:             x=self.personCheck(person.getContent(fieldname))
 1442:             #print "GCLBACKX",x
 1443:             return x
 1444:         
 1445: 
 1446:     def isCheckField(self,fieldname):
 1447:         """return chechfield"""
 1448:         
 1449:         return (fieldname in checkFields)
 1450: 
 1451:     
 1452:     def generateNameIndex(self):
 1453:         """erzeuge einen index verwendeter personen"""
 1454:         import psycopg
 1455:         o = psycopg.connect('dbname=authorities user=dwinter password=3333',serialize=0) 
 1456:         results={}
 1457:         print self.fulltext.historicalNames.items()
 1458:         for nameItem in self.fulltext.historicalNames.items(): #gehe durch alle namen des lexikons
 1459:             
 1460:             c = o.cursor() 
 1461:             name=nameItem[0]
 1462:             print "check",name
 1463:             c.execute("select lastname,firstname from persons where lower(lastname) = '%s'"%quote(name))
 1464:             tmpres=c.fetchall()
 1465:             firstnames=[result[1] for result in tmpres] # find all firstnames
 1466:             if tmpres:
 1467:                 lastname=tmpres[0][0]
 1468:                 
 1469:             for found in self.fulltext({'names':name}):
 1470:                 if found.getObject().isActual():
 1471:                     for nh in found.getObject().getGetNeighbourhood(name, length=50,tagging=False): #hole umgebung
 1472:                         #schaue nun ob der vorname hinter oder vor dem name ist
 1473:                         position=nh.find(lastname)
 1474:                         # vorher
 1475:                         #print "NH",nh
 1476:                         bevorS=nh[0:position].split()
 1477:                         #print "BV",bevorS
 1478:                         if len(bevorS)>1:
 1479:                             try:
 1480:                                 bevor=[bevorS[-1],bevorS[-2]]
 1481:                             except:
 1482:                                 bevor=[bevorS[0]]
 1483:                         else:
 1484:                             bevor=[]
 1485:                         #nachher
 1486:                         behindS= re.split("[,|;| ]",nh[position:]) 
 1487:                         #print "BH",behindS
 1488:                         if len(behindS)>2:
 1489:                             try:
 1490:                                 behind=behindS[1:3]
 1491:                             except:
 1492:                                 behind=[bevorS[1]]
 1493:                         else:
 1494:                             behind=[]
 1495:                         for firstname in firstnames:
 1496:                             if firstname in bevor+behind: #Namen wie mit Adelspraedikaten werden so erstmal nich gefunden
 1497:                                 id="%s,%s"%(lastname,firstname)
 1498:                                 if not results.has_key(id):
 1499:                                     results[id]=[]
 1500:                                 objId=found.getObject().getId()
 1501:                                 if not (objId in results[id]):
 1502:                                     print "d %s for %s"%(id,objId)    
 1503:                                     results[id].append(objId)    
 1504:             self.nameIndex=results
 1505:         return results
 1506:                     
 1507:     def editNameIndexHTML(self):
 1508:         """edit the name index"""
 1509:         if not hasattr(self,'nameIndexEdited'): # falls editierter index noch nicht existiert, kopiere automatisch erstellten
 1510:             self.nameIndexEdited=copy.copy(self.nameIndex)
 1511:             print "huh"
 1512:         #self.nameIndexEdited=copy.copy(self.nameIndex)
 1513:         #print self.nameIndexEdited
 1514:         pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','editHistoricalNames.zpt')).__of__(self)
 1515:         return pt()
 1516:     
 1517:     def getNamesInProject(self,projectId):
 1518:         """get all names ofnameIndexEdited which are references in projec with projectId"""
 1519:         
 1520:         ret=[]
 1521:         for name in self.nameIndexEdited.keys():
 1522:             if projectId in self.nameIndexEdited[name]:
 1523:                 ret.append(name)
 1524:         
 1525:         return ret
 1526:     
 1527:     def editNameIndex(self,RESPONSE=None,name=None,occurrances=None,submit=None):
 1528:         """edit the index"""
 1529:         nI=self.nameIndexEdited # mI introduced to make sure that changes to nameIndexEdited are know to ZODB
 1530:         if submit=="delete":
 1531:            
 1532: 
 1533:             dh=getattr(self,'deletedHistoricalNames',{})
 1534:             
 1535:             if type(dh) is ListType:
 1536:                 dh={}
 1537:             if not dh.has_key(name):
 1538:                 dh[name]=occurrances.split("\n")
 1539:             else:
 1540:                 dh[name]+=occurrances.split("\n")
 1541:             
 1542:             self.deletedHistoricalNames=dh
 1543:             
 1544:             del self.nameIndexEdited[name]
 1545:             
 1546:         
 1547:         elif (submit=="change"):
 1548:             
 1549:             nI[name]=occurrances.split("\n")[0:]
 1550:             
 1551:         elif (submit=="add"):
 1552:             if not nI.has_key(name):
 1553:                 nI[name]=occurrances.split("\n")
 1554:             else:
 1555:                 nI[name]+=occurrances.split("\n")
 1556:     
 1557:         self.nameIndexEdited=nI
 1558:    
 1559:       
 1560:         if RESPONSE is not None:
 1561:             RESPONSE.redirect('editNameIndexHTML')
 1562:         
 1563:     
 1564:     
 1565:     def restoreIndex(self):
 1566:         """restore"""
 1567:         self.nameIndexEdited=self.nameIndex
 1568:         return "done"
 1569:     
 1570: 
 1571:     def sortResults(self,results):
 1572:         """search the catalog and give results back sorted by meta_type"""
 1573:         ret = {}
 1574:         logging.debug(results())
 1575:         for result in results():
 1576:             metaType = result.meta_type
 1577:             resultList= ret.get(metaType,[])
 1578:             resultList.append(result)
 1579:             ret[metaType]=resultList
 1580:         
 1581:         logging.debug(ret)
 1582:         return ret
 1583:         
 1584:             
 1585: def manage_addMPIWGRootForm(self):
 1586:     """form for adding the root"""
 1587:     pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','addMPIWGRootForm.zpt')).__of__(self)
 1588:     return pt()
 1589: 
 1590: def manage_addMPIWGRoot(self,id,title,connection_id="",RESPONSE=None):
 1591:     """add a root folder"""
 1592:     newObj=MPIWGRoot(id,title)
 1593:     self._setObject(id,newObj)
 1594:     ob=getattr(self,id)
 1595:     setattr(ob,'connection_id',connection_id)
 1596:     if RESPONSE is not None:
 1597:         RESPONSE.redirect('manage_main')
 1598:         

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