changeset 2:e6c3334e9611

new function: getSingleRelationFromEntity
author dwinter
date Wed, 30 May 2012 16:53:45 +0200
parents 0ed5ecf36693
children 3532f1c49efb
files JSONClient.py zpt/changeJSONClientForm.zpt
diffstat 2 files changed, 53 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/JSONClient.py	Sun Oct 23 21:33:26 2011 +0200
+++ b/JSONClient.py	Wed May 30 16:53:45 2012 +0200
@@ -8,7 +8,10 @@
 import os.path
 import logging
 
+from BTrees.OOBTree import OOBTree
+
 class JSONClient(SimpleItem):
+    
     meta_type="JSONClient"
     
     manage_options=SimpleItem.manage_options+(
@@ -32,13 +35,14 @@
   
     
     def changeJSONClientConfig(self,url,RESPONSE=None):
+        """cahnge the JSON config"""
         self.url=url
         if RESPONSE is not None:
             RESPONSE.redirect('manage_main')
             
       
     def getEntityLink(self,entId):
-        return "http://neruda.mpiwg-berlin.mpg.de:8080/om4-ismi/browse/entityDetails.iface?eid="+str(entId)
+        return "https://openmind-ismi-dev.mpiwg-berlin.mpg.de/om4-ismi/browse/entityDetails.iface?eid="+str(entId)
           
     def json(self,method,params={},cache=True):
         """json aufruf"""
@@ -46,7 +50,7 @@
        
         paramString=urllib.urlencode(params)
         callUrl = self.url%(method,paramString)
-        
+        logging.debug(callUrl)
         if (cache is True) and (self._v_jsonCache.get(callUrl,None)!=None):
             return self._v_jsonCache.get(callUrl)
         else:
@@ -68,9 +72,18 @@
         return attrsHash
         
     def getOV(self,entId):
+        if (entId)<0: #-1 falls keine Entity gefunden wurde
+            return""
         obj=self.json("getEntity", {'entId':entId})
         return obj.get('entity').get('ownValue')
     
+    def getSingleRelationFromEntity(self,jsonHash,type="srcRelations",relName=None,idType="sourceId"):
+        ret = self.getRelationFromEntity(jsonHash,type,relName,maxNum=1)
+        if len(ret)==0:
+            return -1
+        else:
+            return ret[0].get(idType)
+        
     def getRelationFromEntity(self,jsonHash,type="srcRelations",relName=None,maxNum=30):
         hash=jsonHash.get("entity")
         logging.debug(type)
@@ -213,6 +226,43 @@
         for entry in history:
             ret.append("""<a href="%s">%s</a>"""%entry)
         return "-->".join(ret)
+    
+    def filter(self,list,filterValue,letter,cache=True,cacheName=None):
+        """filters a list of jsonObjects, gives only elements where the filterValue, begins with letter."""
+        
+        if cache and cacheName and (self._v_jsonCache.get(cacheName,None)!=None):
+            hash = self._v_jsonCache.get(cacheName).get(filterValue,None)
+            if hash is not None:
+                return hash.get(letter,[])
+            
+        
+        caches = self._v_jsonCache.get(cacheName,None)
+        if caches is None:
+            caches= OOBTree()
+        
+        hash=caches.get(filterValue,None)
+        if hash is None:
+            hash=OOBTree()
+        
+        
+        for object in list:
+            sortValue = object.get(filterValue).lower()
+            if len(sortValue)>0:
+                val = sortValue[0]
+                if val=="[" and len(sortValue)>1:
+                    val = sortValue[1]
+            else:
+                val=""    
+                
+            valList=hash.get(val,[])
+            valList.append(object)
+            hash.update({val:valList})
+        
+        caches.update({filterValue:hash})
+           
+        self._v_jsonCache.update({cacheName:caches}) 
+        
+        return hash.get(letter,[]), hash.keys()
             
 def manage_addJSONClient(self,id,url,RESPONSE=None):
     """add a json client"""
--- a/zpt/changeJSONClientForm.zpt	Sun Oct 23 21:33:26 2011 +0200
+++ b/zpt/changeJSONClientForm.zpt	Wed May 30 16:53:45 2012 +0200
@@ -1,6 +1,6 @@
 <h1 tal:replace="structure here/manage_page_header">Header</h1>
 <h2>Add a JSONClient</h2>
-<form action="manage_addJSONClient" method="POST">
+<form action="changeJSONClientConfig" method="POST">
 <div>URL String:<input tal:attributes="value here/url" type="text" size="30" name="url"></div>
 <div>example: (cgi-like) http://neruda.mpiwg-berlin.mpg.de:8080/ismi-server/jsonInterface?method=%s&%s</div>
 <div>example: (RESTLIKE) <div>example: (cgi-like) http://neruda.mpiwg-berlin.mpg.de:8080/ismi-server/jsonInterface/%s?%s</div></div>