File:  [Repository] / MPIWGWeb / MPIWGProjects.py
Revision 1.46: download - view: text, annotated - select for diffs - revision graph
Mon Apr 25 16:58:02 2005 UTC (19 years, 6 months ago) by dwinter
Branches: MAIN
CVS tags: HEAD
compatibility with Zope 2.7

"""This contains the class MPIWG Projects
for organizing and maintaining the different project pages

"""
from Products.PageTemplates.PageTemplateFile import PageTemplateFile
from Products.PageTemplates.PageTemplate import PageTemplate
from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
from Products.ZSQLExtend.ZSQLExtend import ZSQLExtendFolder
from Products.ZCatalog.CatalogPathAwareness import CatalogAware
from Globals import package_home
import urllib
import MPIWGStaff
import string
import re
import os
from types import *
import zLOG
import xmlhelper # Methoden zur Verwaltung der projekt xmls
from OFS.SimpleItem import SimpleItem
from OFS.Folder import Folder
from Products.ZSQLMethods.SQL import SQLConnectionIDs

from bibliography import *
import time

definedFields=['WEB_title','xdata_01','xdata_02','xdata_03','xdata_04','xdata_05','xdata_06','xdata_07','xdata_08','xdata_09','xdata_10','xdata_11','xdata_12','xdata_13','WEB_project_header','WEB_project_description','WEB_related_pub']

checkFields = ['xdata_01']



def sortF(x,y):
    try:
        return cmp(x[1],y[1])
    except:
        try:
            return cmp(str(x[1]),str(y[1]))
        except:           
            
            return 0 
    
def sortI(x,y):
    xsplit=x[1].split(".")
    ysplit=y[1].split(".")
    xret=""
    yret=""
    try:
        for i in range(5):
            try:
                yret=yret+"%04i"%int(xsplit[i])
            except:
                yret=yret+"%04i"%0

            try:
                xret=xret+"%04i"%int(ysplit[i])
            except:
                xret=xret+"%04i"%0
                
        
        return cmp(int(yret),int(xret))
    except:
        return cmp(x[1],y[1])

class MPIWGTemplate(ZopePageTemplate):
    """Create a layout Template for different purposes"""

    meta_type="MPIWGTemplate"

    def __init__(self, id, text=None, content_type=None,MPIWGType=None):
        self.id = str(id)



        self.ZBindings_edit(self._default_bindings)
        if text is None:
            self._default_content_fn = os.path.join(package_home(globals()),
                                               'zpt/MPIWG_%s_template_standard.zpt'%MPIWGType)
            text = open(self._default_content_fn).read()
        self.pt_edit(text, content_type)

    
        """change form"""


def manage_addMPIWGTemplateForm(self):
    """Form for adding"""
    pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt/AddMPIWGTemplate.zpt')).__of__(self)
    return pt()

from urllib import quote


def manage_addMPIWGTemplate(self, MPIWGType,id, title=None,REQUEST=None):
    "Add a Page Template with optional file content."
    if type(MPIWGType)==StringType:
        MPIWGTypes=[MPIWGType]
    else:
        MPIWGTypes=MPIWGType
        
    for singleType in MPIWGTypes:


        if REQUEST is None:
            self._setObject(id, MPIWGTemplate(id, text,EchoType=singleType))
            ob = getattr(self, id)
            
            if title:
                ob.pt_setTitle(title)
            return ob
        else:
            file = REQUEST.form.get('file')
            headers = getattr(file, 'headers', None)
            if headers is None or not file.filename:
                zpt = MPIWGTemplate(id,MPIWGType=singleType)
            else:
                zpt = MPIWGTemplate(id, file, headers.get('content_type'))

            self._setObject(id, zpt)
            ob = getattr(self, id)
            if title:
                ob.pt_setTitle(title)

            try:
                u = self.DestinationURL()
            except AttributeError:
                u = REQUEST['URL1']

            
    REQUEST.RESPONSE.redirect(u+'/manage_main')
    return ''

    
class MPIWGRoot(ZSQLExtendFolder):
    """Stammordner für den Web-Server"""

    fieldLabels={'WEB_title':'WEB_Title','xdata_01':'Responsible Scientists','xdata_02':'Department',
             'xdata_03':'Historical Persons','xdata_04':'Time period',
             'xdata_05':'Sorting number','xdata_06':'Keywords','xdata_07':'Short title',
             'xdata_08':'Other involved scholars' ,'xdata_09':'Part of','xdata_10':'Covered by',
             'xdata_11':'Object Digitallibrary','xdata_12':'Cooperation partners',
             'xdata_13':'Funding institutions','WEB_project_header':'WEB_project_header',
             'WEB_project_description':'WEB_project_description','WEB_related_pub':'WEB_related_pub'}
    
    folders=['MPIWGProject','Folder','ECHO_Navigation']
    meta_type='MPIWGRoot'

    def upDateSQL(self,fileName):
        """updates SQL databases using fm.jar"""
        fmJarPath=os.path.join(package_home(globals()), 'updateSQL/fm.jar')
        xmlPath=os.path.join(package_home(globals()), "updateSQL/%s"%fileName)
        zLOG.LOG("MPIWG Web",zLOG.INFO,"java -classpath %s -Djava.awt.headless=true Convert %s"%(fmJarPath,xmlPath))
        ret=os.popen("java -classpath %s -Djava.awt.headless=true Convert %s"%(fmJarPath,xmlPath),"r").read()
	zLOG.LOG("MPIWG Web",zLOG.INFO,"result convert: %s"%ret)
        return 1
    
    def patchProjects(self,RESPONSE):
        """patch"""
        projects=self.ZopeFind(self.projects,obj_metatypes=['MPIWGProject'])
        for project in projects:
		tmp=project[1].WEB_project_description[0].replace("/CD/projects/","")[0:]
            	setattr(project[1],'WEB_project_description',[tmp[0:]])
            	RESPONSE.write("<p>%s</p>\n"%project[0])
            
    def replaceNotEmpty(self,format,field):
        """replace not empty"""
        if field and (not field.lstrip()==''):
            return format%field
        else:
            return ""
        

    def isActual(self,project):
        """checke if project is actual"""
        actualTime=time.localtime()
        obj= project.getObject()
        
        if getattr(obj,'archiveTime',actualTime)< actualTime:
            return False
        else:
            return True
        
    def redirectIndex_html(self,request):
        #return request['URL1']+'/index_html'
        
        return urllib.urlopen(request['URL1']+'/index_html').read()

    
    def formatBibliography(self,here,found):
        """format"""
        return formatBibliography(here,found)
    
    def getValue(self,fieldStr):
        """Inhalt des Feldes"""
        
        if type(fieldStr)==StringType:
            field=fieldStr
        else:
            field=fieldStr[0]
        try:
            if field[len(field)-1]==";":
                field=field[0:len(field)-1]
        except:

            """nothing"""
        field=re.sub(r';([^\s])','; \g<1>',field)
        return field.encode('utf-8')


    
    def sortedNames(self,list):
        """sort names"""

        def sortLastName(x_c,y_c):
            try:
                x=urllib.unquote(x_c).encode('utf-8','ignore')
            except:
                x=urllib.unquote(x_c)

            try:
                y=urllib.unquote(y_c).encode('utf-8','ignore')
            except:
                x=urllib.unquote(y_c)
                

            
            try:
                last_x=x.split()[len(x.split())-1]
                last_y=y.split()[len(y.split())-1]

            except:

                last_x=""
                last_y=""
            
            
            
            if last_x<last_y:
                return 1
            elif last_x>last_y:
                return -1
            else:
                return 0
            
        list.sort(sortLastName)
        list.reverse()
        
        return list
    
    def __init__(self, id, title):
        """init"""
        self.id=id
        self.title=title

    def urlQuote(self,str):
        """quote"""
        return urllib.quote(str)

    def urlUnQuote(self,str):
        """quote"""
        return urllib.unquote(str)
    
    def harvestHistoricalPersons(self):
        """erstelle liste aller erwaehnten actors"""

        def normalize(str):
            """loesche fuhrendes space"""
            if (len(str)>1) and (str[0]==" "):
                ret=str[1:]
            else:
                ret=str
            return ret
        
        list={}
        projects=self.ZopeFind(self.projects,obj_metatypes=['MPIWGProject'])
        
        for project in projects:
            lg=len(project[1].xdata_03[0])-1

            if (lg>1) and (project[1].xdata_03[0][lg]==";"):
                project[1].xdata_03[0]=project[1].xdata_03[0][0:lg]

          
     
                
            try:
                if len(project[1].xdata_03[0].split(";"))>1: # guess if separator is ;
                    for person in project[1].xdata_03[0].split(";"):
                        personNormal=normalize(person)
                        if personNormal in list.keys():
                            list[urllib.quote(personNormal)][1].append(project[1])
                        else:
                            list[urllib.quote(personNormal)]=(personNormal,[project[1]])
                else: #guess , is sepeator
                    for person in project[1].xdata_03[0].split(","):
                        personNormal=normalize(person)
                        if urllib.quote(personNormal) in list.keys():
                            list[urllib.quote(personNormal)][1].append(project[1])
                        else:
                            list[urllib.quote(personNormal)]=(personNormal,[project[1]])

            except:
                zLOG.LOG("MPIWG Web (harvestHistoricalPerson)",zLOG.ERROR,"cannot annalyize: %s"%repr(project))

                
        return list

    def storeHistoricalPersons(self,RESPONSE=None):
        """store persons"""
        self.personDict={}
        personDict=self.harvestHistoricalPersons()
        for person in personDict.keys():
            for project in personDict[person][1]:
                if person in self.personDict.keys():
                    self.personDict[person][1].append((project.absolute_url(),project.WEB_title[0],project.xdata_01[0]))
                else:
                    self.personDict[person]=(personDict[person][0],[(project.absolute_url(),project.WEB_title[0],project.xdata_01[0])])

        if RESPONSE is not None:
            RESPONSE.redirect("showHistoricalPersons")
            

    def getPersonDict(self,name):
        """name von dict"""

        try:
            return self.personDict[name][0].encode('utf-8')
        except:
            return self.personDict[name][0]
            return self.personDict[name][0].decode('latin-1').encode('utf-8')
        

    def showHistoricalPersons(self):
        """show persons"""
        pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','showHistoricalPersons')).__of__(self)
        return pt()

        
    def editHistoricalPersonsForm(self):
        """edit historical persons for consistency"""
        pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','editHistoricalPersonsForm')).__of__(self)
        return pt()

    def getProjectsByFieldContent(self,fieldName,fieldContentsEntry):
        """gib alle Projekte aus mit Value von field mit fieldName enthält ein Element der Liste fieldContents"""
        if type(fieldContentsEntry) is StringType:
            fieldContents=[fieldContentsEntry]
        else:
            fieldContents=fieldContentsEntry
            
        projects=self.ProjectCatalog({fieldName:string.join(fieldContents,' OR')})
        #print projects
        return projects

    def changeMPIWGRootForm(self):
        """edit"""
        pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','changeMPIWGRootForm')).__of__(self)
        return pt()

    def changeMPIWGRoot(self,title,disciplineList,themesList,connection_id,RESPONSE=None):
        """change"""
        self.title=title
        self.connection_id=connection_id
        self.disciplineList=disciplineList
        self.themesList=themesList

        if RESPONSE is not None:
            RESPONSE.redirect('manage_main')

    def getDisciplineList(self):
        """get disciplines as list"""
        return self.disciplineList.split("\n")
    
    def getThemeList(self):
        """get themes as list"""
        return self.themesList.split("\n")
    
    def test(self):
        """test"""
        return self.getProjectsByFieldContent('xdata_09',['biology'])[0].absolute_url

    def getContexts(self,childs=None,parents=None,depth=None):
        """childs alle childs, alle parents"""
        ret=[]
        if parents:
            splitted=parents.split(".")
            parentId=string.join(splitted[0:len(splitted)-1],".")
        
            for project in self.getProjectFields('xdata_05',sort='int'):
                if project[1]==parentId:
                    ret.append(project)

        if childs:
            for project in self.getProjectFields('xdata_05',sort='int'):
                searchStr=childs+"(\..*)"
                if re.match(searchStr,project[1]):
                    
                    if depth:

                        if int(depth)>=len(project[1].split("."))-len(childs.split(".")):
                    
                            ret.append(project)
                    else:
                        ret.append(project)
        return ret
    
    def getProjectFields(self,fieldName,folder=None,sort=None):
        """getListofFieldNames"""
        ret=[]
    
        objects=self.ZopeFind(self.projects,obj_metatypes=['MPIWGProject'],search_sub=0)

                
        for object in objects:
            obj=object[1]

            if not getattr(obj,'invisible',None):
                if fieldName=="WEB_title_or_short":

                    if len(obj.getContent('xdata_07'))<3: # hack weil z.Z. manchmal noch ein Trennzeichen ; oder , im Feld statt leer
                        fieldNameTmp="WEB_title"
                    else:
                        fieldNameTmp="xdata_07"
                else:
                    fieldNameTmp=fieldName

                ret.append((obj,obj.getContent(fieldNameTmp)))

        
        if sort=="int":
            ret.sort(sortI)
        else:
            ret.sort(sortF)
        
        return ret

    def showNewProjects(self):
        projects=[]
        for objs in self.getProjectFields('WEB_title_or_short'): # Get all Projets
            if objs[0].xdata_05[0] == "":
                
                projects.append(objs)
                
        return projects
    
        
    manage_options = Folder.manage_options+(
        {'label':'Update Personal Homepages','action':'updateHomepages'},
        {'label':'Main config','action':'changeMPIWGRootForm'},
        {'label':'Edit Historical Persons','action':'editHistoricalPersonsForm'},
        {'label':'Store Historical Persons','action':'storeHistoricalPersons'},
        )
    
    def importNamesForm(self):
        """Form"""
        pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','MPIWGNamesForm.zpt')).__of__(self)
        return pt()

    def importNames(self,fileupload,folderName,RESPONSE=None):
        """import komma-sep list email,lastName,firstName"""
        project=getattr(self,folderName)
        load=fileupload.read()
        
        for line in load.split('\r'):
            
            
            splitted=line.split(",")
           # print splitted
        
            if not (splitted[0]==""):
                newObj=MPIWGStaff.MPIWGStaff(splitted[0],splitted[1],splitted[2])
                
                try:
                    project._setObject(splitted[0],newObj)
                    #print "done:",splitted[0]
                except:
                    zLOG.LOG("MPIWG Web (importNames)",zLOG.INFO,"cannot import: %s"%splitted[0])

                    
        if RESPONSE is not None:
            RESPONSE.redirect('manage_main')

    def updateHomepages(self,RESPONSE):
        """lege members an"""
        
        self.upDateSQL('personalwww.xml')
        founds=self.ZSQLInlineSearch(_table='personal_www',publish_the_data='yes')
        project=getattr(self,'members')
        for found in founds:
            
            if not (found.e_mail==""):
                newObj=MPIWGStaff.MPIWGStaff(found.username,found.name,found.vorname)
                
                try:
                    project._setObject(found.username,newObj)
                    RESPONSE.write("<p>new:%s</p>\n"%found.username)
                except:
                    RESPONSE.write("<p>old:%s (%s,%s)</p>\n"%(found.username,found.name,found.vorname))


        #delete non existing


        foundUserNames=[x.username for x in founds]
        for member in self.ZopeFind(self,obj_metatypes=["MPIWGStaff"],search_sub=1):            
            
            if member[1].getId() not in foundUserNames:
                member[1].aq_parent.manage_delObjects(ids=[member[1].getId()])
                RESPONSE.write("<p>deleted:%s</p>\n"%member[1].getId())
        try:
            self.MembersCatalog.manage_catalogReindex(self.REQUEST,RESPONSE,self.REQUEST['URL1'])
        except:
            pass
        self.ProjectCatalog.manage_catalogReindex(self.REQUEST,RESPONSE,self.REQUEST['URL1'])
    
                    
        if RESPONSE is not None:
            RESPONSE.redirect('manage_main')

        
    def getAllMembers(self):
        """give list of all members"""
        ret=[]

        #for x in self.members.objectValues('MPIWGStaff'):
            #print x.title
        #    ret.append(x.title.decode('utf-8'))

        for x in self.ZopeFind(self.members,obj_metatypes=['MPIWGStaff']):
                               ret.append(x[1].title.decode('utf-8'))
            
        ret.sort()
        #print ret
  
        return ret

    def printAllMembers(self):
        """prin"""
        members=self.getAllMembers()
        ret=""
        for x in members:
            ret+="<p>%s</p>"%x
        return ret
    
        
    def makeList(self,entry):
        """makes a list out of one entry or repeat a list"""
        if type(entry) is StringType:
            return [entry]
        else:
            return entry


    def getTree(self):
        """generate Tree from project list"""
        returnList=[]
        for project in self.getProjectFields('xdata_05',sort="int"): # get Projects sorted by xdata_05

            for idNr in project[1].split(";"): # more than one number
                if not idNr=="":
                    splittedId=idNr.split(".")
                    depth=len(splittedId)
                    nr=idNr
                    #title=project[0].WEB_title
                    title=[project[0].getContent('WEB_title')]
                    #print title
                    returnList.append((depth,nr,title,project[0]))
        
        return returnList


    def formatElementForOverview(self,element):
        """format the element for output in overview"""
        if element[0]==1: #department
            #print element[3].getContent('xdata_05')
            if element[3].getContent('xdata_05') == "4":
                return """<h2 class="hier"><a href="%s">Ind. Research Group I: %s</a></h2>"""%(element[3].absolute_url()+"/index.html",element[3].getContent('WEB_title'))
            if element[3].getContent('xdata_05') == "5":
                return """<h2 class="hier"><a href="%s">Ind. Research Group II: %s</a></h2>"""%(element[3].absolute_url()+"/index.html",element[3].getContent('WEB_title'))
            
            return """<h2 class="hier"><a href="%s">Department %s: %s</a></h2>"""%(element[3].absolute_url()+"/index.html",element[3].getContent('xdata_05'),element[3].getContent('WEB_title'))

        elif element[0]==2: #mainprojects
            return """<h3 class="hier"><a href="%s">%s</a></h3><p>"""%(element[3].absolute_url()+"/index.html",element[3].getContent('WEB_title'))

        elif element[0]==3:
            return """<p class="hier"><a href="%s">%s</a></p>"""%(element[3].absolute_url()+"/index.html",element[3].getContent('WEB_title'))

        
    def changePosition(self,treeId,select,RESPONSE=None):
        """Change Postion Entry"""
        numbers=[]

        # Suche hoechste bisherige nummer
        projects=self.getProjectFields('xdata_05') # get Projects sorted by xdata_05
        #print "pj",projects
        for project in projects: #suche alle subtrees der treeId
            #print treeId
            
            founds=re.match(treeId+"\.(.*)",project[1].split(";")[0])
            if founds:
                #print "x",founds.group(0),len(founds.group(0).split("."))
                if len(founds.group(0).split("."))==len(treeId.split("."))+1: # nur ein punkt mehr, d.h. untere ebene
                    try:
                        numbers.append(int(founds.group(0).split(".")[len(founds.group(0).split("."))-1]))
                    except:
                        numbers.append(int(0))

        try:
            highest=max(numbers)
        except:
            highest=0
        projects=self.showNewProjects()
        for i in self.makeList(select):
            highest+=10
            projects[int(i)][0].xdata_05=treeId+"."+str(highest)


        if RESPONSE is not None:
            RESPONSE.redirect('showTree')
        
    def changeTree(self,RESPONSE=None):
        """change the complete tree"""
        form=self.REQUEST.form
        hashList={}
        fields=self.getTree()

        
        for idNr in form.keys():
            fields[int(idNr)][3].xdata_05=form[idNr]

        

        if RESPONSE is not None:
            RESPONSE.redirect('showTree')

    def getProjectWithId(self,id):
        fields=self.getProjectFields('xdata_05')
        for field in fields:
            if field[1]==id:
                return field[0]

        return None
            
        
            
        
    def getRelativeUrlFromPerson(self,list):
        """get urls to person list"""
        ret=[]
        persons=list.split(";")
        for person in persons:
            
            if len(person)>1: #nicht nur Trennzeichen
                splitted=person.split(",")
                if len(splitted)==1:
                    splitted=person.split(" ")
                splittedNew=[re.sub(r'\s(.*)','$1',split) for split in splitted]
                if splittedNew[0]=='':
                    del splittedNew[0]
                search=string.join(splittedNew,' AND ')
                
                if not search=='':

                    try:
                        proj=self.MembersCatalog({'title':search})
                    except:
                        proj=None

                if proj:
                    #ret.append("<a href=%s >%s</a>"%(proj[0].absolute_url,person.encode('utf-8')))
                    ret.append("<a href=%s >%s</a>"%('members/'+proj[0].id+'/index.html',person))
                else:
                    #ret.append("%s"%person.encode('utf-8'))
                    ret.append("%s"%person)
        return string.join(ret,";")
        
            
    def getUrlFromPerson(self,list):
        """get urls to person list"""
        ret=[]
        persons=list.split(";")
        for person in persons:
            
            if len(person)>1: #nicht nur Trennzeichen
                splitted=person.split(",")
                if len(splitted)==1:
                    splitted=person.lstrip().rstrip().split(" ")
                splittedNew=[re.sub(r'\s(.*)','$1',split) for split in splitted]
                if splittedNew[0]=='':
                    del splittedNew[0]
                search=string.join(splittedNew,' AND ')
                
                if not search=='':

                    try:
                        proj=self.MembersCatalog({'title':search})
                    except:
                        proj=None

                if proj:
		    if person =="Otto Sibum" : person="H. Otto Sibum"
		    if person =="Norton Wise" : person="M. Norton Wise"
                    #ret.append("<a href=%s >%s</a>"%(proj[0].absolute_url,person.encode('utf-8')))
                    ret.append("<a href=%s >%s</a>"%(proj[0].absolute_url+"/index.html",person))
                else:
                    #ret.append("%s"%person.encode('utf-8'))
                    ret.append("%s"%person)
        return string.join(ret,";")
                         
    def getProjectsOfMembers(self):
        """give tuple member /projects"""
        ret=[]
        members=self.getAllMembers()
        #return str(members)
        for x in members:
            ret+=self.getProjectsOfMember(name=x)
            
        return ret

    def getProjectsOfMember(self,name=None,email=None):
        """get project of a member"""
        def sortP(x,y):
            """sort by sorting number"""
            #print x.xdata_05,y.xdata_05
            return cmp(x.WEB_title,y.WEB_title)
        
       
        ret=[]
        splitNeu=[]
        if email:
            #print "IIII:",self.getId()
            members=self.ZopeFind(self,obj_metatypes=['MPIWGStaff'],obj_ids=[email],search_sub=1)
            name =  members[0][1].title.decode('utf-8')

        y=name
        splitted=y.split(",")
        #XXXX
        splitNeu=["\'"+splitted[1]+" "+splitted[0]+"\'"]
        
        #for s in splitted:
        #    splitNeu.append("\""+s+"\"")
        search=string.join(splitNeu,' AND ')
        
        proj=self.ProjectCatalog({'xdata_01':search})

        if proj:
            proj2=[]
            for x in proj:

                if not getattr(x.getObject(),'invisible',None):
                    if not((splitted[1]==" Christoph") and (splitted[0]=="Hoffmann") and (str(x.WEB_title).find('Einstein')>0)):
                       proj2.append(x)

        else:
            proj2=[]
            
        proj=self.ProjectCatalog({'xdata_08':search})
        if proj:
            names=[x.WEB_title for x in proj]
            for x in proj:

                if not x.WEB_title in names:
                    proj2.append(x)


        proj2.sort(sortP)

        if len(proj2)>0:
            ret.append((y,proj2))

        return ret
     
    def givePersonList(self,name):
        """check if person is in personfolder and return list of person objects"""
        
        splitted=name.split(",")
        if len(splitted)==1:
            splitted=name.lstrip().rstrip().split(" ")
        splittedNew=[split.lstrip() for split in splitted]
        
        if splittedNew[0]=='':
            del splittedNew[0]
        search=string.join(splittedNew,' AND ')
        if not search=='':
            proj=self.MembersCatalog({'title':search})

        if proj:
            return [[x.lastName,x.firstName] for x in proj]
        else:
            return []
            
##         splitted=name.split(",") # version nachname, vorname...
##         if len(splitted)>1:
##             lastName=splitted[0] 
##             firstName=splitted[1]
##         else: 
##             splitted=name.split(" ") #version vorname irgenwas nachnamae
        
##             lastName=splitted[len(splitted)-1]
##             firstName=string.join(splitted[0:len(splitted)-1])

##         objs=[]

        #print  self.members 
      ##   for x in self.members.__dict__:
##             obj=getattr(self.members,x)
##             if hasattr(obj,'lastName') and hasattr(obj,'firstName'):
                
##                 if (re.match(".*"+obj.lastName+".*",lastName) or re.match(".*"+lastName+".*",obj.lastName)) and (re.match(".*"+obj.firstName+".*",firstName) or re.match(".*"+firstName+".*",obj.firstName)):
                    
##                     objs.append((obj,lastName+", "+firstName))

        
        return objs


    def personCheck(self,names):
        """all persons for list"""
        #print "names",names
        splitted=names.split(";")
        ret={}
        for name in splitted:

            if not (name==""):
                try:
                    ret[name]=self.givePersonList(name)
                except:
                    """NOTHIHN"""
        #print "RET",ret
        return ret

    def giveCheckList(self,person,fieldname):
        """return checklist"""
        #print "GCL",fieldname
        if fieldname=='xdata_01':
            x=self.personCheck(person.getContent(fieldname))
            #print "GCLBACKX",x
            return x
        

    def isCheckField(self,fieldname):
        """return chechfield"""
        
        return (fieldname in checkFields)


    
    
        
def manage_addMPIWGRootForm(self):
    """form for adding the root"""
    pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','addMPIWGRootForm.zpt')).__of__(self)
    return pt()

def manage_addMPIWGRoot(self,id,title,connection_id="",RESPONSE=None):
    """add a root folder"""
    newObj=MPIWGRoot(id,title)
    self._setObject(id,newObj)
    ob=getattr(self,id)
    setattr(ob,'connection_id',connection_id)
    if RESPONSE is not None:
        RESPONSE.redirect('manage_main')
        

class MPIWGProject(CatalogAware,Folder):
    """Class for Projects"""

    meta_type='MPIWGProject'
    default_catalog='ProjectCatalog'
    
    def PrincipiaSearchSource(self):
        """Return cataloguable key for ourselves."""
        return str(self)

    def versionHeader(self):
        """version Header, gibt header text entsprechend der aktuellen version aus"""
        
        actualTime=time.localtime()
        retTXT="""<h2>This is an outdated version, for the actual version please refer to <a href="%s">%s</a></h2>"""
        s=self.aq_parent.absolute_url()
        #print getattr(self,'archiveTime',actualTime)
        if getattr(self,'archiveTime',actualTime)< actualTime:
            return retTXT%(s,s)
        else:
            return ""
    def isActual(self):
        """gibt 1 zurueck wenn aktuell, 0 sonst"""
        actualTime=time.localtime()
        
        
        #print getattr(self,'archiveTime',actualTime)
        if getattr(self,'archiveTime',actualTime)< actualTime:
            return 0
        else:
            return 1
        
    def copyObjectToArchive(self):
        """kopiere aktuelles objekt ins archiv"""
        cb=self.aq_parent.manage_copyObjects(self.getId())
        self.manage_pasteObjects(cb)
        actualTime=time.localtime()
        
        self.manage_renameObject(self.getId(),self.getId()+"_"+time.strftime("%Y%m%d%H%M%S",actualTime))
        obj=getattr(self,self.getId()+"_"+time.strftime("%Y%m%d%H%M%S",actualTime))
        obj.setArchiveTime(time.strftime("%Y%m%d%H%M%S",actualTime))
        ids=[x[0] for x in self.ZopeFind(obj,obj_metatypes=['MPIWGProject'])]
        obj.manage_delObjects(ids)
        
    def setArchiveTime(self,time):
        """set Archive Time"""
        self.archiveTime=time[0:]
        
    def versionManageForm(self):
        """version Manage form:currently only set to invisible"""
        pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','MPIWGProject_versionManageForm.zpt')).__of__(self)
        return pt()

    def versionManage(self,invisible=None,RESPONSE=None):
        """version Manage form:currently only set to invisible"""
        self.invisible=invisible

        if RESPONSE is not None:
            RESPONSE.redirect('manage_main')

   
    def crossLinker(self):
        """experimental crosslinker"""
        splitted=self.WEB_project_description[0].split()
        new=[]
        for split in splitted:
            try:
                found=self.DescriptionCatalog({'fulltext':split})
          
                if len(found)>1:
             
                    new.append("<a href=%s>%s</a>"%(split,split))
                else:
                    new.append(split)
            except:
                new.append(split)
        return string.join(new)
            
            


    def generateTemplate(self,RESPONSE=None):
        """Erzeuge Template für defined fields not_used"""

        id="index_html"
        title=id
        if self._getOb('index_html'):
            self._delObject('index_html')

        
        newObj=ZopePageTemplate(id,'TEXT')
        self._setObject(id,newObj)
        #self.manage_addPageTemplate(id,title)
        if RESPONSE is not None:
            RESPONSE.redirect('manage_main')
            
    def __init__(self, id, argv=None):
        """initieriere classe"""
        
        self.id=id
        self.title=id
        if argv:
            for arg in definedFields:
		try:
                	setattr(self,arg,argv[arg])
		except:
			setattr(self,arg,"")
        else:
            for arg in definedFields:
                setattr(self,arg,'')
            
    manage_options = Folder.manage_options+(
        {'label':'Load New File','action':'loadNewFileForm'},
        {'label':'Edit ProjectInfo','action':'editMPIWGProjectForm'},
        {'label':'Edit BasisInfo','action':'editMPIWGBasisForm'},
        {'label':'Edit Publications','action':'editMPIWGRelatedPublicationsForm'},
        {'label':'Edit Themes & Disciplines','action':'editMPIWGDisciplinesThemesForm'},
        {'label':'Versionmanager','action':'versionManageForm'},
        )


    def checkDate(self,date):
        """teste ob zum Zeitpunkt date eine andere version existierte"""
        

        def sortProjectsByTime(x,y):
            return cmp(x[1].archiveTime,y[1].archiveTime)
        
        finds=self.ZopeFind(self,obj_metatypes=['MPIWGProject'])
        if not finds:
            return self.absolute_url()
        else:
            finds.sort(sortProjectsByTime)

        for find in finds:
            if int(find[1].archiveTime) > int(date):
                return find[1].absolute_url()

        return self.absolute_url()
        
            
            
    def index_html(self):
        """show homepage"""
        
        if self.REQUEST.has_key('date') and self.REQUEST.SESSION.get('MPI_redirected',None)==None:
            self.REQUEST.SESSION['MPI_redirected']=1
            self.REQUEST.RESPONSE.redirect(self.checkDate(self.REQUEST['date']))
        else:
            self.REQUEST.SESSION['MPI_redirected']=None
        
	ext=self.ZopeFind(self.aq_parent,obj_ids=["project_main"])

        if ext:
            return getattr(self,ext[0][1].getId())()

        pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','project_main')).__of__(self)
        return pt()

    
    def getDataFields(self):
        """giveListofDatafields"""
        ret=[]
        for x in range(1,14):
            if not x in [6,10,9]: # not used fields
                ret.append('xdata_%02i'%x)
        return ret
            
    def getDefinedFields(self):
        """show all defined fields"""
        
        return definedFields

    def getAttribute(self,field):
        """get attrbiute"""
        return getattr(self,field)

    def getContent(self,field):
        """Inhalt des Feldes"""
        
        text=u''

        for x in getattr(self,field):
            
            try:
                text +=x
            except:
                text = x
            
        

        try:
            if text[len(text)-1]==";":
                text=text[0:len(text)-1]
          
                
        except:
            pass
        
        if text=='':
            text2=text
        else:
            text2=re.sub(r';([^\s])','; \g<1>',text)

        #teste ob ergebnis leer und header dann nehme title
        
        if (text2=='') and (field=='WEB_project_header'):
            return self.getContent('WEB_title')

        #teste ob WEB_project_description und keine führenden p tags
        if (len(text2)>4) and (not text2[0:3]=='<p>') and (field=='WEB_project_description'):
            return "<p>"+text2+"</p>"


        return text2.encode('utf-8')
    
    def show_html(self):
        """simple index"""
        #return "HI"
        pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','MPIWGProject_index.zpt')).__of__(self)
        return pt()

    def editMPIWGProjectForm(self):
        """editform"""
        pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','edit_MPIWGProject.zpt')).__of__(self)
        return pt()

    def editMPIWGProject(self,RESPONSE=None):
        """edit the project and archive the old version"""
        
        self.copyObjectToArchive() # archive the object


        for x in definedFields:
            if self.REQUEST.has_key(x):
                
                setattr(self,x,[self.REQUEST[x].decode('utf-8')])


        
        if RESPONSE is not None:
            RESPONSE.redirect('manage_main')

    def editMPIWGDisciplinesThemesForm(self):
        """edit the disciplines and themes Form"""
        pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','edit_MPIWGDisciplinesThemes.zpt')).__of__(self)
        return pt()

    def editMPIWGDisciplinesThemes(self,disciplines=None,themes=None,RESPONSE=None):
        """edit disciplin and form"""
        if disciplines:
            if type(disciplines) is StringType:
                self.xdata_09=disciplines
            else:
                self.xdata_09=string.join(disciplines,";")
        else:
                self.xdata_09=""
        if themes:
            if type(themes) is StringType:
                self.xdata_10=themes
            else:
                self.xdata_10=string.join(themes,";")
        else:
            self.xdata_10=""
            
        if RESPONSE is not None:
            RESPONSE.redirect('manage_main')


    def isChecked(self,wert,list):
        """check if wert is in ; seperated list"""

        #felder sind manchmnal als liste mit einem element definiert
        if type(list) is StringType or UnicodeType: 
            splitted=list.split(";")
        else:
            splitted=list[0].split(";")


        for x in splitted:
            if (not x==u'') and x in wert:
                return 1
        return 0
    
    def editMPIWGBasisForm(self):
        """editform"""
        pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','edit_MPIWGBasis.zpt')).__of__(self)
        return pt()
    def editMPIWGRelatedPublicationsForm(self):
        """Edit related Publications"""
        pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','edit_MPIWGRelatedPublications.zpt')).__of__(self)
        return pt()
        
    
    def loadNewFileForm(self):
        """Neues XML-File einlesen"""
        pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','MPIWGProject_newfile.zpt')).__of__(self)
        return pt()

    def loadNewFile(self,RESPONSE=None):
        """einlesen des neuen files"""
        fileupload=self.REQUEST['fileupload']
        if fileupload:
            file_name=fileupload.filename
            filedata=fileupload.read()

            argv=xmlhelper.proj2hash(filedata)
            #print argv.keys()
            for arg in definedFields:
                
                #print arg,argv[arg],getattr(self,arg)
                try:
                    temp=argv[arg][0:]
                    #old=getattr(self,arg)
                    setattr(self,arg,temp)
                    #print old,getattr(self,arg)
                except:
                    """nothing"""
                    
        if RESPONSE is not None:
            RESPONSE.redirect('manage_main')

def manage_addMPIWGProjectForm(self):
    """form for adding the project"""
    pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','addMPIWGProjectForm.zpt')).__of__(self)
    return pt()

def manage_addMPIWGProject(self,id,RESPONSE=None):
    """method to add a project"""
    #print argv
    fileupload=self.REQUEST.get('fileupload',None)
    if fileupload:

        file_name=fileupload.filename
        filedata=fileupload.read()
        
        argv=xmlhelper.proj2hash(filedata)
        
        #print argv
        newObj=MPIWGProject(id,argv)
    else:
        newObj=MPIWGProject(id)

    self._setObject(id,newObj)


    if RESPONSE is not None:
        RESPONSE.redirect('manage_main')

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