Mercurial > hg > ISMI_JSONClient
changeset 1:0ed5ecf36693
history managememt added
author | dwinter |
---|---|
date | Sun, 23 Oct 2011 21:33:26 +0200 |
parents | e8640aa396cd |
children | e6c3334e9611 |
files | JSONClient.py zpt/AddJSONClientForm.zpt zpt/changeJSONClientForm.zpt |
diffstat | 3 files changed, 163 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/JSONClient.py Thu Oct 13 10:51:54 2011 +0200 +++ b/JSONClient.py Sun Oct 23 21:33:26 2011 +0200 @@ -11,19 +11,51 @@ class JSONClient(SimpleItem): meta_type="JSONClient" + manage_options=SimpleItem.manage_options+( + {'label':'Main Config','action':'changeJSONClientConfigPT'}, + ) + + + + _v_jsonCache={} + def __init__(self,id,url): """init""" self.id=id self.url=url - def json(self,method,params={}): + def changeJSONClientConfigPT(self): + """change form""" + pt=zptFile(self, 'zpt/changeJSONClientForm.zpt') + pt.content_type="text/html" + return pt() + + + def changeJSONClientConfig(self,url,RESPONSE=None): + 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) + + def json(self,method,params={},cache=True): """json aufruf""" + paramString=urllib.urlencode(params) callUrl = self.url%(method,paramString) - txt=SrvTxtUtils.getHttpData(callUrl) - logging.debug(txt) - obj= json.loads(txt) + + if (cache is True) and (self._v_jsonCache.get(callUrl,None)!=None): + return self._v_jsonCache.get(callUrl) + else: + + txt=SrvTxtUtils.getHttpData(callUrl) + logging.debug(txt) + obj= json.loads(txt) + + self._v_jsonCache[callUrl]=obj return obj @@ -65,7 +97,123 @@ logging.debug("++++++++++++++++++++++++++++++") logging.debug(rels) return ret + + def getPubmanOutput(self,url="http://pubman.mpiwg-berlin.mpg.de/search/SearchAndExport?cqlQuery=escidoc.context.objid=escidoc:79281&exportFormat=APA&outputFormat=html_linked&sortKeys=&sortOrder=ascending&startRecord=1&maximumRecords=50",cache=True): + """getPubmanOutput""" + + callUrl = url + if (cache is True) and (self._v_jsonCache.get(callUrl,None)!=None): + return self._v_jsonCache.get(callUrl) + else: + + txt=SrvTxtUtils.getHttpData(callUrl) + logging.debug(txt) + #obj= json.loads(txt) + + self._v_jsonCache[callUrl]=txt + return txt + + def activateUrl(self,value): + #wenn ein wert eine url ist, dass mache daraus einen entsprechende a link. + if value.startswith("http://") or value.startswith("https://"): + return """<a class="activatedLink" href="%s">%s</a>"""%(value,value) + else: + return value + + + def getISMIIDfromBiographie(self,name): + #nimmt den Filenamen und gibt die ISMI Personen ID zurueck diese liegt auf dem Server in einer Datei bio2ismi.txt + data = str(getattr(self.biographies,'bio2ismi.txt')) + logging.debug(data) + for line in data.split("\n"): + splitted= line.split("\t") + filename=splitted[1].replace(r"\w","_") + filename=filename.replace(r"%\d+","_") + logging.debug(line) + if (len(splitted)> 1) and (name in filename): + return splitted[0] + return "" + + def collectURL(self,url,name,REQUEST): + history=REQUEST.SESSION.get('history',None); + if history is None: + history=[] + + + if len(history)>0:#wenn der letzte Eintrag gleich dem jetzigen ist, dann nicht eintragen + urlStored,nameStored=history[-1] + if urlStored==url: + return self.createBreadcrumb(REQUEST); + + + history.append((url,name)) + REQUEST.SESSION.set('history',history); + return self.createBreadcrumb(REQUEST); + + def collectTriple(self,s,p,o,inverse=False,REQUEST=None,RESPONSE=None): + """collect a triple and redirect""" + + history=REQUEST.SESSION.get('triple',None); + if history is None: + history=[] + + + + if not (s,p,o) in history: + + history.append((s,p,o)) + REQUEST.SESSION.set('triple',history); + + if inverse: + RESPONSE.redirect(s) + else: + RESPONSE.redirect(o) + + def cleanHistoryAndTriple(self,REQUEST): + """clean history""" + REQUEST.SESSION.set('triple',[]); + REQUEST.SESSION.set('history',[]); + REQUEST.response.redirect(REQUEST['HTTP_REFERER']) + + def getTripleAsRDF(self,REQUEST): + """show the triple as RDF""" + ret="""<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:ismi="http://ismi.mpiwg-berlin.mpg.de/ns/"> """ + + rdfString="""<rdf:Description rdf:about="%s"> + <ismi:%s rdf:resource="%s"/> + <ismi:has_ownValue>%s</ismi:has_ownValue> + </rdf:Description>""" + + ownValueString="""<rdf:Description rdf:about="%s"> + <ismi:has_ownValue>%s</ismi:has_ownValue> + </rdf:Description>""" + + history=REQUEST.SESSION.get('triple',None); + if history is not None: + for triple in history: + s,p,o=triple + ownValue=self.getOV(s.split("=")[1]) + ret+=rdfString%(s,p,o,ownValue) + ret+=ownValueString%(o,self.getOV(o.split("=")[1])) + + + ret+="</rdf:RDF>" + + urlBase=REQUEST['URL0'] + ret=ret.replace("../",urlBase+"ISMI/database/") + REQUEST.response.setHeader("Content-type", "text/rdf") + return ret + def createBreadcrumb(self,REQUEST): + history=REQUEST.SESSION.get('history',None); + if history is None: + return "" + + ret=[] + for entry in history: + ret.append("""<a href="%s">%s</a>"""%entry) + return "-->".join(ret) + def manage_addJSONClient(self,id,url,RESPONSE=None): """add a json client"""
--- a/zpt/AddJSONClientForm.zpt Thu Oct 13 10:51:54 2011 +0200 +++ b/zpt/AddJSONClientForm.zpt Sun Oct 23 21:33:26 2011 +0200 @@ -7,4 +7,4 @@ <div>example: (RESTLIKE) <div>example: (cgi-like) http://neruda.mpiwg-berlin.mpg.de:8080/ismi-server/jsonInterface/%s?%s</div></div> <div> first %s ist for the method name second %s for the parameter </div> <div><input type="submit"></div> -</form> \ No newline at end of file +</form>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/zpt/changeJSONClientForm.zpt Sun Oct 23 21:33:26 2011 +0200 @@ -0,0 +1,10 @@ +<h1 tal:replace="structure here/manage_page_header">Header</h1> +<h2>Add a JSONClient</h2> +<form action="manage_addJSONClient" 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> +<div> first %s ist for the method name second %s for the parameter </div> +<div><input type="submit"></div> +</form> +