Mercurial > hg > MPIWGThesaurus
comparison MPIWGThesaurus.py @ 0:b2e4605f20b2
beta version
author | dwinter |
---|---|
date | Thu, 30 Jun 2011 09:07:49 +0200 |
parents | |
children | 95e1e37915c6 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:b2e4605f20b2 |
---|---|
1 # TODO: generell verwaltung der tags im triple store und nicht in zope. | |
2 from OFS.Folder import Folder | |
3 from Globals import package_home | |
4 from Products.PageTemplates.PageTemplateFile import PageTemplateFile | |
5 from BTrees.OOBTree import OOBTree | |
6 from BTrees.OOBTree import OOSet | |
7 | |
8 #import os | |
9 import os.path | |
10 import logging | |
11 import time | |
12 import email.Utils | |
13 from types import ListType | |
14 import transaction | |
15 import random | |
16 import urllib2 | |
17 import urllib | |
18 | |
19 class Entry: | |
20 """publicClass""" | |
21 def __init__(self,title,entries=[]): | |
22 self.title=title | |
23 self.entries=entries | |
24 | |
25 def getTitle(self): | |
26 """getTitle""" | |
27 return self.title | |
28 | |
29 class SuggestedTag: | |
30 """publicClass""" | |
31 type="" | |
32 userName="" | |
33 def __init__(self,name,comment,type,userName): | |
34 self.name=name | |
35 self.comment=comment | |
36 self.type=type | |
37 self.userName=userName | |
38 | |
39 | |
40 class MPIWGThesaurus(Folder): | |
41 """MPIWGThesaurus main folder""" | |
42 meta_type="MPIWGThesaurus" | |
43 | |
44 TMP_PERSON_NS="http://ontologies.mpiwg-berlin.mpg.de/tempObjects/person/" | |
45 ONTOLOGY_NS="http://ontologies.mpiwg-berlin.mpg.de/authorities/namedIdentities/" | |
46 #personproviderURL="http://127.0.0.1:8280/MetaDataManagerRestlet/person/" | |
47 personproviderURL="http://virtuoso.mpiwg-berlin.mpg.de:8080/MetaDataManagerRestlet/person/" | |
48 additionalNamesGraphURL="file://newpersonsFromProjects" | |
49 | |
50 #virtuosoServer="http://ontologies.mpiwg-berlin.mpg.de" | |
51 virtuosoServer="http://virtuoso.mpiwg-berlin.mpg.de:8890" | |
52 virtuosoDAV="/DAV/home/websiteuser/" | |
53 virtuosoDAVUser="websiteuser" | |
54 virtuosoDAVPW="w3s45us3" | |
55 virtuosoGraph="file://newpersonsFromProjects" | |
56 | |
57 projectPersons= OOBTree() | |
58 projectObjects= OOBTree() | |
59 projectHistoricalPlaces= OOBTree() | |
60 projectSuggestedTags= OOBTree() | |
61 persons2Projects= OOBTree() | |
62 object2Projects=OOBTree() | |
63 historicalPlaces2Projects=OOBTree() | |
64 suggestedTags2Projects=OOBTree() | |
65 personIDtoNames=OOBTree() | |
66 suggestedPersons= OOBTree(); #TODO: das sollte die ueberfluessig werde und nur im triple store stehen. | |
67 | |
68 tagList=[] | |
69 approachesTxt="""A | |
70 --- | |
71 ent1 | |
72 --- | |
73 B | |
74 --- | |
75 enta | |
76 entb | |
77 entc | |
78 """ | |
79 | |
80 disciplines=[] | |
81 disciplinesTxt="""A | |
82 B | |
83 C | |
84 """ | |
85 | |
86 periods=[] | |
87 periodsTxt="""PA | |
88 PB | |
89 PC | |
90 """ | |
91 | |
92 spaces=[] | |
93 spacesTxt="""SPA | |
94 --- | |
95 sPent1 | |
96 --- | |
97 SPB | |
98 --- | |
99 SPenta | |
100 SPentb | |
101 SPentc | |
102 """ | |
103 technologies=[] | |
104 technologiesTxt="""TPA | |
105 --- | |
106 TPent1 | |
107 --- | |
108 TPB | |
109 --- | |
110 TPenta | |
111 TPentb | |
112 TPentc | |
113 """ | |
114 | |
115 knowledgeTransfers=[] | |
116 knowledgeTransfersTxt="""KTPA | |
117 --- | |
118 TPent1 | |
119 --- | |
120 TPB | |
121 --- | |
122 TPenta | |
123 TPentb | |
124 TPentc | |
125 """ | |
126 | |
127 | |
128 | |
129 | |
130 | |
131 | |
132 manage_options = Folder.manage_options+( | |
133 {'label':'Main Config','action':'changeMPIWGThesaurusForm'}, | |
134 #{'label':'Change Publications Special','action':'changePublications_specialForm'}, | |
135 ) | |
136 | |
137 def __init__(self,id): | |
138 """init""" | |
139 self.id=id | |
140 | |
141 | |
142 def index_html(self): | |
143 """index file""" | |
144 | |
145 # if hasattr(self,"storedProjectPersons"): | |
146 # self.projectPersons= getattr(self,"storedProjectPersons"); | |
147 # | |
148 # if hasattr(self,"storedProjectObjects"): | |
149 # self.projectPersons= getattr(self,"storedProjectObjects"); | |
150 # | |
151 # | |
152 | |
153 pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','index.html')).__of__(self) | |
154 return pt() | |
155 | |
156 | |
157 def changeMPIWGThesaurusForm(self): | |
158 """form for changing the project""" | |
159 pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','changeMPIWGThesaurusForm.zpt')).__of__(self) | |
160 return pt() | |
161 | |
162 def changeMPIWGThesaurus(self,approachesTxt,disciplinesTxt,periodsTxt,spacesTxt,technologiesTxt,knowledgeTransfersTxt,RESPONSE=None): | |
163 """change it""" | |
164 self.approachesTxt=approachesTxt | |
165 self.disciplinesTxt=disciplinesTxt | |
166 self.periodsTxt=periodsTxt | |
167 self.spacesTxt=spacesTxt | |
168 self.technologiesTxt=technologiesTxt | |
169 self.knowledgeTransfersTxt=knowledgeTransfersTxt | |
170 | |
171 if RESPONSE is not None: | |
172 redirect(RESPONSE,'manage_main') | |
173 | |
174 #In der Anzeige soll der Name der zitierten Personen in Klartext angezeigt werden, ausserdem die Varianten, wie sie tatsaechlich | |
175 #in den Projekten benutzt werden | |
176 def collectPersonNamesToIds(self): | |
177 """finde Namen zu den personen ids""" | |
178 self.personIDtoNames=OOBTree() # loessche alle | |
179 | |
180 for personID in self.persons2Projects: | |
181 mainName=self.getMainNameFromTripleStore(personID); # hole die hauptbezeichnung aus dem triplestore | |
182 | |
183 personNames=[] | |
184 for project in self.persons2Projects.get(personID): | |
185 #hole die personen aus dem projekte | |
186 logging.debug("Found:project:"+project) | |
187 namesInProject = self.projectPersons.get(project) | |
188 | |
189 for nameInProjectTuple in namesInProject: | |
190 logging.debug(repr(nameInProjectTuple)) | |
191 nameInProject=nameInProjectTuple[1] | |
192 logging.debug(nameInProjectTuple[0]+"??"+personID) | |
193 if nameInProjectTuple[0]==personID: | |
194 logging.debug("changing") | |
195 if mainName=="": # es gibt keinen Eintrag im Triplestore fuer name (sollte eigentlich nicht sein, ist dann ein Fehler dort) | |
196 mainName=nameInProject #nimm dann aber den ersten aus den projekten | |
197 if nameInProject!=mainName: | |
198 personNames.append(nameInProject) | |
199 | |
200 logging.debug(str(mainName)+"- foundalso :"+",".join(personNames)) | |
201 self.personIDtoNames.update({personID:(mainName,personNames)}) | |
202 | |
203 self.personIDtoNames=self.personIDtoNames # make clear that it has changed #TODO: change this to variablen with names _p | |
204 | |
205 def getNamesFromID(self,personID): | |
206 retStr="" | |
207 retStr+=self.personIDtoNames.get(personID)[0] # hole die Hauptbezeichnung | |
208 additionalNames = self.personIDtoNames.get(personID)[1] | |
209 if len(additionalNames)>0: | |
210 retStr+=" ("+",".join(additionalNames)+","+")" | |
211 return retStr | |
212 | |
213 | |
214 def getMainNameFromTripleStore(self,personID): | |
215 | |
216 | |
217 cmdString ="""select * where { <%s> <http://xmlns.com/foaf/0.1/name> ?name}"""%personID | |
218 | |
219 | |
220 auth_handler = urllib2.HTTPBasicAuthHandler() | |
221 auth_handler.add_password(realm='sparql', | |
222 uri=self.virtuosoServer+"/sparql", | |
223 user=self.virtuosoDAVUser, | |
224 passwd=self.virtuosoDAVPW) | |
225 | |
226 opener = urllib2.build_opener(auth_handler) | |
227 opener.addheaders = [('Content-Type','application/sparql-query')] | |
228 | |
229 logging.debug(cmdString) | |
230 try: | |
231 r= opener.open(self.virtuosoServer+"/sparql", urllib.urlencode({'query':cmdString,'default-graph-uri':self.virtuosoGraph,'named-graph-uri':None,'format':'text/csv'})) | |
232 namesTxt=r.read() | |
233 except urllib2.URLError, e: | |
234 logging.error(e.code) | |
235 logging.error(e.read()) | |
236 return | |
237 | |
238 names=namesTxt.split("\n") | |
239 if len(names) < 2: #in der ersten Zeile stehen bei der Rückgabe die Spaltennamen, <2 heiss also es gibt keinen Eintrag | |
240 return | |
241 | |
242 return names[1].replace('"','') # wir nehmen nur den ersten treffer | |
243 | |
244 | |
245 def changeTags(self,projectID,approaches=[],disciplines=[],periods=[],spaces=[],technologies=[],transfers=[],RESPONSE=None): | |
246 """change tags""" | |
247 self.changeTagFromList(projectID,"approaches", approaches) | |
248 self.changeTagFromList(projectID,"disciplines", disciplines) | |
249 self.changeTagFromList(projectID,"periods", periods) | |
250 self.changeTagFromList(projectID,"spaces", spaces) | |
251 self.changeTagFromList(projectID,"technologies", technologies) | |
252 self.changeTagFromList(projectID,"transfers", transfers) | |
253 | |
254 self.projectObjects=self.projectObjects #necessesary to make the object persistent after the first call | |
255 self.projectHistoricalPlaces=self.projectHistoricalPlaces #necessesary to make the object persistent after the first call | |
256 self.projectPersons=self.projectPersons #necessesary to make the object persistent after the first call | |
257 self.projectSuggestedTags=self.projectSuggestedTags | |
258 | |
259 self.persons2Projects=self.persons2Projects | |
260 self.object2Projects=self.object2Projects | |
261 self.historicalPlaces2Projects=self.historicalPlaces2Projects | |
262 self.suggestedTags2Projects=self.suggestedTags2Projects; | |
263 | |
264 self.suggestedPersons=self.suggestedPersons; | |
265 | |
266 def deleteAllTags(self): | |
267 """deleteAllTags - TODO: take this out!!,""" | |
268 setattr(self,"tags",None) | |
269 | |
270 return "DONE!" | |
271 | |
272 def deleteAllPersons(self): | |
273 """deleteAllTags - TODO: take this out!!,""" | |
274 self.projectPersons= OOBTree() | |
275 self.persons2Projects= OOBTree() | |
276 self.personIDtoNames=OOBTree() | |
277 self.suggestedPersons= OOBTree(); #TODO: das sollte die ueberfluessig werde und nur im triple store stehen. | |
278 | |
279 | |
280 return "DONE!" | |
281 def changeTagFromList(self,projectID,tagType,tagList): | |
282 | |
283 logging.debug("changing:"+projectID) | |
284 logging.debug("asking tag::"+tagType) | |
285 if type(tagList) is not ListType: | |
286 tagList=[tagList] | |
287 | |
288 | |
289 tags = getattr(self,"tags",None) | |
290 if tags is None: | |
291 logging.debug("Create new tags") | |
292 tags=OOBTree() | |
293 setattr(self,"tags",tags) | |
294 | |
295 | |
296 tagsStored=tags.get(tagType,None) | |
297 if tagsStored is None: | |
298 tagsStored=OOBTree(); | |
299 | |
300 | |
301 | |
302 for tag in tagsStored.keys(): #gehe durch alle gespeichteren tags | |
303 logging.debug("checking:"+tag) | |
304 currentApproach = tagsStored.get(tag,None) | |
305 logging.debug(currentApproach) | |
306 if currentApproach is None: | |
307 currentApproach=OOSet() | |
308 | |
309 if tag in tagList: #dieser ist getagged | |
310 currentApproach.update([projectID]); | |
311 tagList.remove(tag); | |
312 else: | |
313 if currentApproach.has_key(projectID): | |
314 currentApproach.remove(projectID); | |
315 | |
316 tagsStored.update({tag:currentApproach}); | |
317 | |
318 for tag in tagList: # alle die nich nicht abearbeitet worden muessen noch angelegt werden | |
319 logging.debug("adding:"+tag) | |
320 newApproach=OOSet() | |
321 newApproach.update([projectID]); | |
322 tagsStored.insert(tag, newApproach); | |
323 | |
324 tags.update({tagType:tagsStored}) | |
325 | |
326 transaction.commit() | |
327 | |
328 def getTitle(self,entry): | |
329 return entry.title | |
330 | |
331 def getEntries(self,entry): | |
332 return entry.entries | |
333 | |
334 def getEntriesFromTxt(self,txt): | |
335 apps=[] | |
336 lines=txt.split("\n"); | |
337 level=False | |
338 for line in lines: | |
339 line=line.rstrip().lstrip() | |
340 if line=="": | |
341 continue | |
342 if line=="---": | |
343 | |
344 if level: | |
345 level=False | |
346 else: | |
347 level=True | |
348 else: | |
349 if not level: | |
350 entry=Entry(line,[]) | |
351 apps.append(entry) | |
352 else: | |
353 entry.entries.append(Entry(line)) | |
354 | |
355 return apps[0:] | |
356 | |
357 def getApproaches(self): | |
358 | |
359 self.tagList=self.getEntriesFromTxt(self.approachesTxt) | |
360 return self.tagList | |
361 | |
362 def getTagsJSON(self,projectID,tagType,RESPONSE=None): | |
363 """get all selected tagList""" | |
364 logging.debug("asking:"+projectID) | |
365 logging.debug("asking tag::"+tagType) | |
366 retList=[] | |
367 tags = getattr(self,"tags",None) | |
368 if tags is None: | |
369 return; | |
370 | |
371 tagsStored=tags.get(tagType,None) | |
372 if tagsStored is None: | |
373 return; | |
374 | |
375 for tag in tagsStored.keys(): #gehe durch alle gespeichteren approached | |
376 logging.debug("checking:"+tag) | |
377 currentTag = tagsStored.get(tag,None) | |
378 if currentTag is None: | |
379 continue | |
380 if currentTag.has_key(projectID): | |
381 retList.append(""" "%s" """%tag) | |
382 | |
383 if RESPONSE: | |
384 RESPONSE.setHeader('Content-Type', "application/json") | |
385 | |
386 logging.debug(retList) | |
387 return "["+",".join(retList)+"]" | |
388 | |
389 def getDisciplines(self): | |
390 | |
391 self.disciplines=self.getEntriesFromTxt(self.disciplinesTxt) | |
392 return self.disciplines | |
393 | |
394 | |
395 def getObjectsJSON(self,term="",RESPONSE=None): | |
396 """ get all stored objects as json list starting with term""" | |
397 term=term.lower(); | |
398 logging.debug("Asking:"+term) | |
399 #objectList=["baus","berg","lurg"] | |
400 objects = self.objects2Projects; | |
401 | |
402 objectList= [x for x in objects.keys()] | |
403 | |
404 logging.debug("Create:"+repr(objectList)) | |
405 retList=[] | |
406 for object in objectList: | |
407 if object.find(term)>-1: | |
408 retList.append(""" "%s" """%object) | |
409 ret="[" | |
410 ret+=",".join(retList) | |
411 ret+="]" | |
412 logging.debug("Objects:"+ret) | |
413 if RESPONSE: | |
414 RESPONSE.setHeader('Content-Type', "application/json") | |
415 return ret | |
416 | |
417 | |
418 def getHistoricalPlacesJSON(self,term="",RESPONSE=None): | |
419 """ get all stored historicalPlaces as json list starting with term""" | |
420 term=term.lower(); | |
421 logging.debug("Asking:"+term) | |
422 #historicalPlaceList=["baus","berg","lurg"] | |
423 historicalPlaces = self.historicalPlaces2Projects; | |
424 | |
425 historicalPlaceList= [x for x in historicalPlaces.keys()] | |
426 | |
427 logging.debug("Create:"+repr(historicalPlaceList)) | |
428 retList=[] | |
429 for historicalPlace in historicalPlaceList: | |
430 if historicalPlace.find(term)>-1: | |
431 retList.append(""" "%s" """%historicalPlace) | |
432 ret="[" | |
433 ret+=",".join(retList) | |
434 ret+="]" | |
435 logging.debug("historicalPlaces:"+ret) | |
436 if RESPONSE: | |
437 RESPONSE.setHeader('Content-Type', "application/json") | |
438 return ret | |
439 | |
440 | |
441 def getPeriods(self): | |
442 | |
443 self.periodes=self.getEntriesFromTxt(self.periodsTxt) | |
444 return self.periodes | |
445 | |
446 def getSpaces(self): | |
447 | |
448 self.spaces=self.getEntriesFromTxt(self.spacesTxt) | |
449 return self.spaces | |
450 | |
451 def getTechnologies(self): | |
452 | |
453 self.technologies=self.getEntriesFromTxt(self.technologiesTxt) | |
454 return self.technologies | |
455 | |
456 def getKnowledgeTransfers(self): | |
457 | |
458 self.knowledgeTransfers=self.getEntriesFromTxt(self.knowledgeTransfersTxt) | |
459 return self.knowledgeTransfers | |
460 | |
461 | |
462 def getObjectsFromProject(self,projectID): | |
463 return self.getFromProject(self.projectObjects,projectID); | |
464 | |
465 def getHistoricalPlacesFromProject(self,projectID): | |
466 return self.getFromProject(self.projectHistoricalPlaces,projectID); | |
467 | |
468 def getSuggestedTagsFromProject(self,projectID): | |
469 return self.getFromProject(self.projectSuggestedTags,projectID); | |
470 | |
471 | |
472 def getPersonsFromProject(self,projectID): | |
473 return self.getFromProject(self.projectPersons,projectID); | |
474 | |
475 def getFromProject(self,objects,projectID): | |
476 if objects.has_key(projectID): | |
477 logging.debug("key exists:"+projectID) | |
478 persons=objects.get(projectID); | |
479 else: | |
480 persons=OOSet() | |
481 objects.update({projectID:persons}) | |
482 logging.debug("key does not exists:"+projectID) | |
483 | |
484 | |
485 return persons | |
486 | |
487 def getHistoricalPlacesFromProjectAsHTML(self,projectID): | |
488 """gertobjects""" | |
489 return self.getFromProjectAsHTML(self.projectHistoricalPlaces, projectID, "HistoricalPlace"); | |
490 | |
491 def getObjectsFromProjectAsHTML(self,projectID): | |
492 """gertobjects""" | |
493 return self.getFromProjectAsHTML(self.projectObjects, projectID, "Object"); | |
494 | |
495 def getPersonsFromProjectAsHTML(self,projectID): | |
496 """getpersons""" | |
497 return self.getFromProjectAsHTML(self.projectPersons, projectID, "Person"); | |
498 | |
499 | |
500 def getFromProjectAsHTML(self,objects,projectID,type): | |
501 """getpersons""" | |
502 ret="" | |
503 if objects.has_key(projectID): | |
504 persons=objects.get(projectID); | |
505 else: | |
506 persons=OOSet() | |
507 | |
508 | |
509 for person in persons: | |
510 ret+="""<div class="removeButton"> | |
511 <span>%s </span><div style="display:inline" class="remove%s"><span style="display:none" class="removeValue">%s</span>remove</div> | |
512 </div>"""%(person[1],type,person[0]) | |
513 | |
514 | |
515 # ret+="""<div> | |
516 # <input type="checkbox" name="persons_remove" | |
517 # value="%s"> | |
518 # <span>%s</span> | |
519 # </div>"""%(person[0],person[1]) | |
520 # | |
521 logging.debug(type+":"+ret) | |
522 return ret | |
523 | |
524 | |
525 def removeHistoricalPlaceFromProject(self,projectID,value): | |
526 """remove""" | |
527 logging.debug("removeHP:"+projectID+":"+value); | |
528 | |
529 HistoricalPlaces = self.getHistoricalPlacesFromProject(projectID) | |
530 for HistoricalPlace in HistoricalPlaces.keys(): | |
531 | |
532 logging.debug("trying:"+repr(HistoricalPlace[0])+"::"+repr(value)+"__") | |
533 if HistoricalPlace[0]==value: | |
534 logging.debug("---removed!") | |
535 HistoricalPlaces.remove(HistoricalPlace) | |
536 | |
537 self.projectHistoricalPlaces.update({projectID:HistoricalPlaces}) | |
538 retstring = self.getHistoricalPlacesFromProjectAsHTML(projectID) | |
539 logging.debug(retstring) | |
540 return retstring | |
541 | |
542 def removeObjectFromProject(self,projectID,value): | |
543 """remove""" | |
544 logging.debug("remove:"+projectID+":"+value); | |
545 | |
546 objects = self.getObjectsFromProject(projectID) | |
547 for object in objects.keys(): | |
548 | |
549 logging.debug("trying:"+repr(object[0])+"::"+repr(value)+"__") | |
550 if object[0]==value: | |
551 logging.debug("---removed!") | |
552 objects.remove(object) | |
553 | |
554 self.projectObjects.update({projectID:objects}) | |
555 retstring = self.getObjectsFromProjectAsHTML(projectID) | |
556 logging.debug(retstring) | |
557 return retstring | |
558 | |
559 def removePersonFromProject(self,projectID,value): | |
560 """remove""" | |
561 logging.debug("remove:"+projectID+":"+value); | |
562 | |
563 persons = self.getPersonsFromProject(projectID) | |
564 for person in persons.keys(): | |
565 | |
566 logging.debug("trying:"+repr(person[0])+"::"+repr(value)+"__") | |
567 if person[0]==value: | |
568 logging.debug("---removed!") | |
569 persons.remove(person) | |
570 | |
571 self.projectPersons.update({projectID:persons}) | |
572 retstring = self.getPersonsFromProjectAsHTML(projectID) | |
573 logging.debug(retstring) | |
574 return retstring | |
575 | |
576 #fuegt die neue Person zur personIDtoNames hinzu, für die spätere Anzeige in der Personenliste | |
577 def addNameTOPersonIDNames(self,personID,label): | |
578 personName = self.personIDtoNames.get(personID) | |
579 if personName==None: # Anlegen falls noch nicht existent | |
580 personName=(label,[]) | |
581 else: | |
582 if (not label in personName[1]) and (not label==personName[0]): # hinzufuegen falls der neuen name noch nicht als bezeichnugn fuer personID eingetragen ist | |
583 personName[1].append(label) | |
584 | |
585 self.personIDtoNames.update({personID:personName}) | |
586 | |
587 | |
588 def addPersonToProject(self,projectID,value,label): | |
589 """ add a person so the project""" | |
590 persons=self.getPersonsFromProject(projectID); | |
591 | |
592 logging.debug("adding:"+projectID+":"+value+":"+label); | |
593 | |
594 persons.insert([value,label]) | |
595 self.projectPersons.update({projectID:persons}) | |
596 | |
597 for personTuple in persons: | |
598 #hole die abgespeicherten Projekte zu einer Person | |
599 person=personTuple[0] #hole die ID | |
600 | |
601 | |
602 if getattr(self,"persons2Projects",None)==None: | |
603 setattr(self,"persons2Projects",OOBTree()) | |
604 | |
605 projects = self.persons2Projects.get(person,None) | |
606 | |
607 if projects==None: | |
608 projects=OOSet(); | |
609 | |
610 projects.insert(projectID ) | |
611 logging.debug("update:"+person) | |
612 self.persons2Projects.update({person:projects}) | |
613 | |
614 self.addNameTOPersonIDNames(value,label) | |
615 retstring = self.getPersonsFromProjectAsHTML(projectID) | |
616 logging.debug(retstring) | |
617 transaction.commit() | |
618 return retstring | |
619 | |
620 def getPersonsWithProjectIDs(self): | |
621 """holt die getaggted Personen mit Projekten""" | |
622 persons = self.persons2Projects; | |
623 ret={} | |
624 logging.debug("Check Person:"+repr(persons.keys())) | |
625 for person in persons.keys(): | |
626 logging.debug("Check:"+person) | |
627 #TODO: person muss duch den namen von provuder geholt werden | |
628 list=[] | |
629 for projectID in persons.get(person): | |
630 list.append(self.getProjectDetails(projectID)) | |
631 | |
632 ret[person]=list[0:] | |
633 return ret | |
634 | |
635 def getHistoricalPlacesWithProjectIDs(self): | |
636 """holt die getaggted Personen mit Projekten""" | |
637 HistoricalPlaces = self.HistoricalPlaces2Projects; | |
638 ret={} | |
639 logging.debug("Check HistoricalPlace:"+repr(HistoricalPlaces.keys())) | |
640 for HistoricalPlace in HistoricalPlaces.keys(): | |
641 logging.debug("Check:"+HistoricalPlace) | |
642 #TODO: HistoricalPlace muss duch den namen von provuder geholt werden | |
643 list=[] | |
644 for projectID in HistoricalPlaces.get(HistoricalPlace): | |
645 list.append(self.getProjectDetails(projectID)) | |
646 | |
647 ret[HistoricalPlace]=list[0:] | |
648 return ret | |
649 | |
650 def getObjectsWithProjectIDs(self): | |
651 """holt die getaggted Personen mit Projekten""" | |
652 objects = self.objects2Projects; | |
653 ret={} | |
654 logging.debug("Check Object:"+repr(objects.keys())) | |
655 for object in objects.keys(): | |
656 logging.debug("Check:"+object) | |
657 #TODO: object muss duch den namen von provuder geholt werden | |
658 list=[] | |
659 for projectID in objects.get(object): | |
660 list.append(self.getProjectDetails(projectID)) | |
661 | |
662 ret[object]=list[0:] | |
663 return ret | |
664 | |
665 def listAllSuggestedTags(self): | |
666 """list all tags""" | |
667 ret="<html><body>" | |
668 for projectID in self.projectSuggestedTags: | |
669 ret+="<h3>"+projectID+"</h3><table>" | |
670 for tags in self.projectSuggestedTags.get(projectID): | |
671 ret+="<tr><td>"+tags[0]+"</td><td>"+tags[1].type+"</td><td>"+tags[1].comment+"</td><td>"+tags[1].userName+"</td></tr>" | |
672 ret+="</table>" | |
673 ret+="</body></html>" | |
674 logging.debug(ret); | |
675 return ret | |
676 | |
677 def suggestTag(self,projectID,tagName,tagComment,tagType,tagUserName): | |
678 """suggest a new tag""" | |
679 """ add a person so the project""" | |
680 SuggestedTags=self.getSuggestedTagsFromProject(projectID); | |
681 | |
682 logging.debug("suggested:"+projectID+":"+tagName); | |
683 | |
684 | |
685 #TODO: Im moment noch keine Zuordnung zu einer ID. | |
686 newTag = SuggestedTag(tagName,tagComment,tagType,tagUserName) | |
687 SuggestedTags.insert([tagName,newTag]) | |
688 self.projectSuggestedTags.update({projectID:SuggestedTags}) | |
689 | |
690 | |
691 | |
692 for SuggestedTagTuple in SuggestedTags: | |
693 #hole die abgespeicherten Projekte zu einer Person | |
694 person=SuggestedTagTuple[0] #hile die ID | |
695 | |
696 | |
697 if getattr(self,"SuggestedTags2Projects",None)==None: | |
698 setattr(self,"SuggestedTags2Projects",OOBTree()) | |
699 | |
700 projects = self.SuggestedTags2Projects.get(person,None) | |
701 | |
702 if projects==None: | |
703 projects=OOSet(); | |
704 | |
705 projects.insert(projectID ) | |
706 logging.debug("update:"+person) | |
707 self.SuggestedTags2Projects.update({person:projects}) | |
708 | |
709 | |
710 | |
711 #retstring = self.getSuggestedTagsFromProjectAsHTML(projectID) | |
712 #logging.debug(retstring) | |
713 | |
714 | |
715 | |
716 #return retstring | |
717 | |
718 | |
719 def addHistoricalPlaceToProject(self,projectID,value): | |
720 """ add a person so the project""" | |
721 HistoricalPlaces=self.getHistoricalPlacesFromProject(projectID); | |
722 | |
723 logging.debug("adding:"+projectID+":"+value); | |
724 | |
725 | |
726 #TODO: Im moment noch keine Zuordnung zu einer ID. | |
727 HistoricalPlaces.insert([value,value]) | |
728 self.projectHistoricalPlaces.update({projectID:HistoricalPlaces}) | |
729 | |
730 | |
731 | |
732 for HistoricalPlaceTuple in HistoricalPlaces: | |
733 #hole die abgespeicherten Projekte zu einer Person | |
734 person=HistoricalPlaceTuple[0] #hile die ID | |
735 | |
736 | |
737 if getattr(self,"HistoricalPlaces2Projects",None)==None: | |
738 setattr(self,"HistoricalPlaces2Projects",OOBTree()) | |
739 | |
740 projects = self.HistoricalPlaces2Projects.get(person,None) | |
741 | |
742 if projects==None: | |
743 projects=OOSet(); | |
744 | |
745 projects.insert(projectID ) | |
746 logging.debug("update:"+person) | |
747 self.HistoricalPlaces2Projects.update({person:projects}) | |
748 | |
749 | |
750 | |
751 retstring = self.getHistoricalPlacesFromProjectAsHTML(projectID) | |
752 logging.debug(retstring) | |
753 | |
754 | |
755 | |
756 return retstring | |
757 | |
758 def addObjectToProject(self,projectID,value): | |
759 """ add a person so the project""" | |
760 objects=self.getObjectsFromProject(projectID); | |
761 | |
762 logging.debug("adding:"+projectID+":"+value); | |
763 | |
764 | |
765 #TODO: Im moment noch keine Zuordnung zu einer ID. | |
766 objects.insert([value,value]) | |
767 self.projectObjects.update({projectID:objects}) | |
768 | |
769 | |
770 | |
771 for objectTuple in objects: | |
772 #hole die abgespeicherten Projekte zu einer Person | |
773 person=objectTuple[0] #hile die ID | |
774 | |
775 | |
776 if getattr(self,"objects2Projects",None)==None: | |
777 setattr(self,"objects2Projects",OOBTree()) | |
778 | |
779 projects = self.objects2Projects.get(person,None) | |
780 | |
781 if projects==None: | |
782 projects=OOSet(); | |
783 | |
784 projects.insert(projectID ) | |
785 logging.debug("update:"+person) | |
786 self.objects2Projects.update({person:projects}) | |
787 | |
788 | |
789 | |
790 retstring = self.getObjectsFromProjectAsHTML(projectID) | |
791 logging.debug(retstring) | |
792 | |
793 | |
794 | |
795 return retstring | |
796 | |
797 | |
798 def getProjectsWithTag(self,tagType,tagName): | |
799 tags = getattr(self,"tags",None) | |
800 if tags is None: | |
801 logging.debug("can't find Tags") | |
802 return [] | |
803 | |
804 | |
805 | |
806 tagsStored=tags.get(tagType,None) | |
807 if tagsStored is None: | |
808 logging.debug("can't find Tag:"+tagType) | |
809 return [] | |
810 | |
811 projects = tagsStored.get(tagName,None) | |
812 if projects is None: | |
813 logging.debug("can't find any projects for Tag:"+tagName+"("+tagType+")") | |
814 return [] | |
815 | |
816 ret=[] | |
817 for project in projects: | |
818 logging.debug("found:"+project) | |
819 ret.append(self.getProjectDetails(project)) | |
820 | |
821 return ret | |
822 | |
823 def getProjectDetails(self,getID): | |
824 project=getattr(self.www_neu.projects,getID,None) | |
825 if project is None: | |
826 logging.debug("no project!! " +getID) | |
827 return | |
828 | |
829 link="/en/research/projects/"+getID | |
830 | |
831 title=project.getContent("short_title") | |
832 #persons=project.getPersonKeyList() | |
833 persons=project.getContent('xdata_01') | |
834 logging.debug("%s %s %s"%(link,title,persons)) | |
835 return link,title,persons | |
836 | |
837 # $.post("addNewPerson",{"projectID": | |
838 # $('#projectID').val(), | |
839 # "personName":$("#suggest_newPerson").val(), | |
840 # "personComment":$("#suggest_newPerson_comment").val()} | |
841 # | |
842 # ); | |
843 # | |
844 | |
845 def addAndCreateNewPerson(self,projectID,personName,personComment,personUserName): | |
846 """adds anew person to the project which is not in the authority file""" | |
847 | |
848 id = random.randint(0, 1000000); | |
849 idstring="http://ontologies.mpiwg-berlin.mpg.de/tempObjects/person/"+str(id) | |
850 while self.suggestedPersons.has_key(idstring): | |
851 id = random.randint(0, 1000000); | |
852 #idstring="http://ontologies.mpiwg-berlin.mpg.de/tempObjects/person/"+str(id); | |
853 idstring=self.TEMP_PERSON_NS+str(id); | |
854 | |
855 | |
856 self.suggestedPersons.update({idstring:(personName,personComment,projectID,personUserName)}) | |
857 self.createTempPersonInVirtuoso(projectID, idstring, personName, personComment) #TODO: add suername to the triplestore | |
858 return self.addPersonToProject(projectID, idstring, personName); | |
859 | |
860 def createTempPersonInVirtuoso(self,projectID,personURI, personName,personComment): | |
861 """add a new person to the triple store of tempory persons""" | |
862 triples=[]; | |
863 personID="<"+personURI+">" | |
864 triples.append((personID,"<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>","<http://ontologies.mpiwg-berlin.mpg.de/authorities/namedIdentities/Person>")) | |
865 triples.append((personID,"<http://erlangen-crm.org/110404/P3_has_note>",'"'+personComment+'"')) | |
866 triples.append((personID,"<http://xmlns.com/foaf/0.1/name>",'"'+personName+'"')) | |
867 triples.append((personID,"<http://ontologies.mpiwg-berlin.mpg.de/general/isMentionedOn>","<http://www.mpiwg-berlin.mpg.de/en/research/projects/"+projectID+">")) | |
868 | |
869 cmdString ="insert in GRAPH <"+self.virtuosoGraph+"> {" | |
870 for triple in triples: | |
871 cmdString+=" ".join(triple)+"." | |
872 | |
873 cmdString+="}" | |
874 | |
875 auth_handler = urllib2.HTTPBasicAuthHandler() | |
876 auth_handler.add_password(realm='sparql', | |
877 uri=self.virtuosoServer+"/sparql", | |
878 user=self.virtuosoDAVUser, | |
879 passwd=self.virtuosoDAVPW) | |
880 | |
881 opener = urllib2.build_opener(auth_handler) | |
882 opener.addheaders = [('Content-Type','application/sparql-query')] | |
883 | |
884 logging.debug(cmdString) | |
885 try: | |
886 r= opener.open(self.virtuosoServer+"/sparql", urllib.urlencode({'query':cmdString,'default-graph-uri':self.virtuosoGraph,'named-graph-uri':None})) | |
887 logging.debug(r.read()) | |
888 except urllib2.URLError, e: | |
889 logging.error(e.code) | |
890 logging.error(e.read()) | |
891 return | |
892 | |
893 opener2 = urllib2.build_opener() | |
894 | |
895 | |
896 personIDencoded=urllib.quote(personURI,safe="") | |
897 graphEncoded=urllib.quote(self.additionalNamesGraphURL,safe="") | |
898 url = self.personproviderURL+"id/"+personIDencoded+"/"+graphEncoded | |
899 | |
900 try: | |
901 request = urllib2.Request(url); | |
902 request.get_method = lambda : 'PUT' | |
903 r= opener2.open(request); | |
904 logging.debug(r.read()) | |
905 except urllib2.URLError, e: | |
906 logging.error(e.code) | |
907 logging.error(e.read()) | |
908 return | |
909 | |
910 | |
911 | |
912 return | |
913 | |
914 def redirect(RESPONSE,url): | |
915 """mache ein redirect mit einem angehaengten time stamp um ein reload zu erzwingen""" | |
916 | |
917 timeStamp=time.time() | |
918 | |
919 if url.find("?")>-1: #giebt es schon parameter | |
920 addStr="&time=%s" | |
921 else: | |
922 addStr="?time=%s" | |
923 | |
924 RESPONSE.setHeader('Last-Modified',email.Utils.formatdate().split("-")[0]+'GMT') | |
925 logging.error(email.Utils.formatdate()+' GMT') | |
926 RESPONSE.redirect(url+addStr%timeStamp) | |
927 | |
928 def manage_addMPIWGThesaurusForm(self): | |
929 """form for adding the project""" | |
930 pt=PageTemplateFile(os.path.join(package_home(globals()),'zpt','addMPIWGThesaurusForm.zpt')).__of__(self) | |
931 return pt() | |
932 | |
933 | |
934 | |
935 def manage_addMPIWGThesaurus(self,id,RESPONSE=None): | |
936 """add it""" | |
937 newObj=MPIWGThesaurus(id) | |
938 | |
939 self._setObject(id,newObj) | |
940 | |
941 | |
942 if RESPONSE is not None: | |
943 redirect(RESPONSE,'manage_main') |