comparison JSONClient.py @ 2:e6c3334e9611

new function: getSingleRelationFromEntity
author dwinter
date Wed, 30 May 2012 16:53:45 +0200
parents 0ed5ecf36693
children 3532f1c49efb
comparison
equal deleted inserted replaced
1:0ed5ecf36693 2:e6c3334e9611
6 from Products.PageTemplates.PageTemplateFile import PageTemplateFile 6 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
7 import os 7 import os
8 import os.path 8 import os.path
9 import logging 9 import logging
10 10
11 from BTrees.OOBTree import OOBTree
12
11 class JSONClient(SimpleItem): 13 class JSONClient(SimpleItem):
14
12 meta_type="JSONClient" 15 meta_type="JSONClient"
13 16
14 manage_options=SimpleItem.manage_options+( 17 manage_options=SimpleItem.manage_options+(
15 {'label':'Main Config','action':'changeJSONClientConfigPT'}, 18 {'label':'Main Config','action':'changeJSONClientConfigPT'},
16 ) 19 )
30 pt.content_type="text/html" 33 pt.content_type="text/html"
31 return pt() 34 return pt()
32 35
33 36
34 def changeJSONClientConfig(self,url,RESPONSE=None): 37 def changeJSONClientConfig(self,url,RESPONSE=None):
38 """cahnge the JSON config"""
35 self.url=url 39 self.url=url
36 if RESPONSE is not None: 40 if RESPONSE is not None:
37 RESPONSE.redirect('manage_main') 41 RESPONSE.redirect('manage_main')
38 42
39 43
40 def getEntityLink(self,entId): 44 def getEntityLink(self,entId):
41 return "http://neruda.mpiwg-berlin.mpg.de:8080/om4-ismi/browse/entityDetails.iface?eid="+str(entId) 45 return "https://openmind-ismi-dev.mpiwg-berlin.mpg.de/om4-ismi/browse/entityDetails.iface?eid="+str(entId)
42 46
43 def json(self,method,params={},cache=True): 47 def json(self,method,params={},cache=True):
44 """json aufruf""" 48 """json aufruf"""
45 49
46 50
47 paramString=urllib.urlencode(params) 51 paramString=urllib.urlencode(params)
48 callUrl = self.url%(method,paramString) 52 callUrl = self.url%(method,paramString)
49 53 logging.debug(callUrl)
50 if (cache is True) and (self._v_jsonCache.get(callUrl,None)!=None): 54 if (cache is True) and (self._v_jsonCache.get(callUrl,None)!=None):
51 return self._v_jsonCache.get(callUrl) 55 return self._v_jsonCache.get(callUrl)
52 else: 56 else:
53 57
54 txt=SrvTxtUtils.getHttpData(callUrl) 58 txt=SrvTxtUtils.getHttpData(callUrl)
66 attrsHash[attr.get('name')]=attr.get('ownValue') 70 attrsHash[attr.get('name')]=attr.get('ownValue')
67 attrsHash['@type']=type 71 attrsHash['@type']=type
68 return attrsHash 72 return attrsHash
69 73
70 def getOV(self,entId): 74 def getOV(self,entId):
75 if (entId)<0: #-1 falls keine Entity gefunden wurde
76 return""
71 obj=self.json("getEntity", {'entId':entId}) 77 obj=self.json("getEntity", {'entId':entId})
72 return obj.get('entity').get('ownValue') 78 return obj.get('entity').get('ownValue')
73 79
80 def getSingleRelationFromEntity(self,jsonHash,type="srcRelations",relName=None,idType="sourceId"):
81 ret = self.getRelationFromEntity(jsonHash,type,relName,maxNum=1)
82 if len(ret)==0:
83 return -1
84 else:
85 return ret[0].get(idType)
86
74 def getRelationFromEntity(self,jsonHash,type="srcRelations",relName=None,maxNum=30): 87 def getRelationFromEntity(self,jsonHash,type="srcRelations",relName=None,maxNum=30):
75 hash=jsonHash.get("entity") 88 hash=jsonHash.get("entity")
76 logging.debug(type) 89 logging.debug(type)
77 logging.debug(".................") 90 logging.debug(".................")
78 logging.debug(hash) 91 logging.debug(hash)
211 224
212 ret=[] 225 ret=[]
213 for entry in history: 226 for entry in history:
214 ret.append("""<a href="%s">%s</a>"""%entry) 227 ret.append("""<a href="%s">%s</a>"""%entry)
215 return "-->".join(ret) 228 return "-->".join(ret)
229
230 def filter(self,list,filterValue,letter,cache=True,cacheName=None):
231 """filters a list of jsonObjects, gives only elements where the filterValue, begins with letter."""
232
233 if cache and cacheName and (self._v_jsonCache.get(cacheName,None)!=None):
234 hash = self._v_jsonCache.get(cacheName).get(filterValue,None)
235 if hash is not None:
236 return hash.get(letter,[])
237
238
239 caches = self._v_jsonCache.get(cacheName,None)
240 if caches is None:
241 caches= OOBTree()
242
243 hash=caches.get(filterValue,None)
244 if hash is None:
245 hash=OOBTree()
246
247
248 for object in list:
249 sortValue = object.get(filterValue).lower()
250 if len(sortValue)>0:
251 val = sortValue[0]
252 if val=="[" and len(sortValue)>1:
253 val = sortValue[1]
254 else:
255 val=""
256
257 valList=hash.get(val,[])
258 valList.append(object)
259 hash.update({val:valList})
260
261 caches.update({filterValue:hash})
262
263 self._v_jsonCache.update({cacheName:caches})
264
265 return hash.get(letter,[]), hash.keys()
216 266
217 def manage_addJSONClient(self,id,url,RESPONSE=None): 267 def manage_addJSONClient(self,id,url,RESPONSE=None):
218 """add a json client""" 268 """add a json client"""
219 269
220 270