changeset 273:d1b43624cc63

some hacks to make the european4D connection work
author dwinter
date Thu, 23 Feb 2012 11:44:38 +0100
parents 3a5d51c60e40
children 55bc9972fb1b
files RestDbGisApi.py RestDbInterface.py gis_gui/blocks/table.html.pt gis_gui/lib/blocks.js zpt/HTML_schema_table.zpt zpt/KML_schema_table.zpt
diffstat 6 files changed, 78 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/RestDbGisApi.py	Fri Jan 27 20:02:41 2012 +0100
+++ b/RestDbGisApi.py	Thu Feb 23 11:44:38 2012 +0100
@@ -148,8 +148,9 @@
     def getLiveKmlUrl(self,schema,table,useTimestamp=True,REQUEST=None):
         return self.getLiveUrl(schema,table,useTimestamp,REQUEST)
 
-    def getKmlData(self, schema, table, ids=None, sortBy=1, gisIdField=None, latField=None, lonField=None, geomField="point", colorField="red_big"):
+    def getKmlData(self, schema, table, ids=None, sortBy=1, gisIdField=None, latField=None, lonField=None, geomField="point", colorField="red_big",from_year_name='from_year',until_year_name=''):
         """returns data structure for KML template"""
+        logging.debug("getKMLDATA")
         logging.debug("getKMLdata gid=%s lat=%s lon=%s sortBy=%s geom=%s color=%s"%(gisIdField,latField,lonField,sortBy,geomField,colorField))
         if geomField is None:
             geomField="point"
@@ -322,18 +323,24 @@
             sql = "SELECT field_name FROM public.gis_table_meta_rows WHERE table_name = %s and gis_type = %s"
             # from_year and until_year in metadata first
             try:
-                res = self.executeSQL(sql, (table,'from_year'))
+                res = self.executeSQL(sql, (table,from_year_name))
                 if len(res['rows']) > 0:
                     from_year = res['rows'][0][0]
             except:
                     from_year = "from_year_dummy"
             try:
-                res = self.executeSQL(sql, (table,'until_year'))
+                res = self.executeSQL(sql, (table,until_year_name))
                 if len(res['rows']) > 0:
                     until_year = res['rows'][0][0]
             except:
                     until_year = "until_year_dummy"
+                    
+            #DW added for testing E4D with names        
+            from_year=from_year_name
+            until_year=until_year_name
             
+            logging.debug("from_year:"+from_year)
+            logging.debug("until_year:"+until_year)
             for i in range (len(dataset)):
                 value = dataset[i]
                 
@@ -349,6 +356,7 @@
                     #    continue
                     
                     val = "%s: %s"%(name, value)
+                    logging.debug(name)
                     value=unicode(value)
                     if name == from_year:
                        beg_yr= value
@@ -616,6 +624,7 @@
         return kmlFileName
     
     def trydatahas_key(self,data,index,key_string):
+        logging.debug("trying:"+key_string)
         try:
             return data[index].has_key(key_string) 
         except:
--- a/RestDbInterface.py	Fri Jan 27 20:02:41 2012 +0100
+++ b/RestDbInterface.py	Thu Feb 23 11:44:38 2012 +0100
@@ -123,9 +123,10 @@
 
     def JSON_schema_table(self,schema,table):
         """JSON index function"""
+        logging.debug("start: json_schema")
         self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json")
         json.dump(self.getTable(schema, table), self.REQUEST.RESPONSE)        
-
+        logging.debug("end: json_schema")
     
     def __init__(self, id, title, connection_id=None):
         """init"""
@@ -185,6 +186,16 @@
             
         return map
     
+    def getFieldNames(self,fields):
+        """returns a dict mapping field names to row indexes"""
+        map = []
+        i = 0
+        for f in fields:
+            map.append(f[0])
+            
+            
+        return map
+    
     def executeSQL(self, query, args=None, hasResult=True, autocommit=True):
         """execute query with args on database and return all results.
         result format: {"fields":fields, "rows":data}"""
@@ -260,6 +271,8 @@
         # type and format are real parameter
         resultFormat = REQUEST.get('format','HTML').upper()
         queryType = REQUEST.get('type',None)
+        from_year_name = REQUEST.get('from_year_name',None)
+        until_year_name = REQUEST.get('until_year_name',None)
         
         logging.debug("index_html path=%s resultFormat=%s queryType=%s"%(path,resultFormat,queryType))
 
@@ -267,7 +280,7 @@
             # non-empty queryType -- look for template
             pt = getattr(self.template, "%s_%s"%(resultFormat,queryType), None)
             if pt is not None:
-                return pt(format=resultFormat,type=queryType,path=path)
+                return pt(format=resultFormat,type=queryType,path=path,from_year_name=from_year_name,until_year_name=until_year_name)
             
         if len(path) == 1:
             # list of schemas
@@ -281,8 +294,10 @@
                 # POST to table to check
                 return self.checkTable(format=resultFormat,schema=path[1],table=path[2])
             # else show table
-            return self.showTable(format=resultFormat,schema=path[1],table=path[2],REQUEST=REQUEST, RESPONSE=RESPONSE)
-        
+            logging.debug("index_html:will showTable")
+            x= self.showTable(format=resultFormat,schema=path[1],table=path[2],REQUEST=REQUEST, RESPONSE=RESPONSE)
+            logging.debug("index_html:have done showTable")
+            return x
         # don't know what to do
         return str(REQUEST)
 
@@ -351,10 +366,15 @@
         
         # everything else has its own template
         pt = getattr(self.template, '%s_schema_table'%format, REQUEST)
+        logging.debug("showtable: gottemplate")
         if pt is None:
             return "ERROR!! template %s_schema_table not found at %s"%(format, self.template )
         #data = self.getTable(schema,table)
-        return pt(schema=schema,table=table,args=queryArgs)
+        logging.debug("table:"+repr(table))
+        #x = pt(schema=schema,table=table,args={})
+        x = pt(schema=schema,table=table,args=queryArgs)
+        logging.debug("showtable: executed Table")
+        return x
 
     def getLiveUrl(self,schema,table,useTimestamp=True,REQUEST=None):
         if REQUEST is None:
@@ -381,7 +401,7 @@
         attrString=""
  #        try:
         for name in attrNames['rows']:
-              logging.debug("name: ", name[0])
+              logging.debug("name: "+repr( name[0]))
               not_added=True
               if name[0] == "the_geom":                        #FJK: the table column is "the_geom"
                      attrString=attrString+"ST_AsText("+name[0]+"),"
@@ -389,9 +409,9 @@
                      break
               for a_iter in attrTypes['rows']:
                  not_added = True
-                 logging.debug("attrTypes.field_name: ", a_iter[0])
+                 logging.debug("attrTypes.field_name: "+ repr(a_iter[0]))
                  if a_iter[0]==name[0]:            
-                     logging.debug("attrTypes.gis_type: ", a_iter[1])            
+                     logging.debug("attrTypes.gis_type: "+ repr(a_iter[1]))            
                      if a_iter[1] == "the_geom":                        #FJK: the table column is registered in gis_table_meta_rows as type "the_geom"
                          attrString=attrString+"ST_AsText("+name[0]+"),"
                          not_added=False
@@ -407,6 +427,7 @@
             """ table does not exist """
  #           fields=self.get
   #          self.createEmptyTable(schema, table, fields)
+        logging.debug("getTable: done")
         return data
 
     def hasTable(self,schema='public',table=None,username='guest'):
--- a/gis_gui/blocks/table.html.pt	Fri Jan 27 20:02:41 2012 +0100
+++ b/gis_gui/blocks/table.html.pt	Thu Feb 23 11:44:38 2012 +0100
@@ -28,7 +28,11 @@
       <br/>
       <input type="button" value="Create map layer" class="" tal:attributes="onClick string:blocks.addLayerBlock('blocks/layer.html?table=$table','layer','${table}')"/>
      <br/>
-      <input type="button" value="Open Map in Europeana4D" class="" tal:attributes='onClick string:blocks.e4DOpen("Europeana4D","${table}")'/>
+      <input type="button" value="Open Map in Europeana4D" class="" tal:attributes="onClick string:blocks.e4DOpen('Europeana4D','${table}',jQuery('#from_year_name').val(),jQuery('#until_year_name').val())"/>
+      from data (or date):<input type="text" id="from_year_name">
+      to date (if existing):<input type="text" id="until_year_name">
+      
+     
     </div>
  </tal:block>
 
--- a/gis_gui/lib/blocks.js	Fri Jan 27 20:02:41 2012 +0100
+++ b/gis_gui/lib/blocks.js	Thu Feb 23 11:44:38 2012 +0100
@@ -109,15 +109,29 @@
     jQuery.getJSON(url,callback);
 };
 
-guiBlocks.prototype.e4DOpen = function(titel,tables){
+guiBlocks.prototype.e4DOpen = function(titel,tablesParam,from_year_name,until_year_name){
 	var e4D_URL="http://mappit.mpiwg-berlin.mpg.de:8080/e4D?";
-	var paramstr = "format=KML";
+	var paramstr;
+	if (until_year_name!='') {
+		paramstr = "format=KML&from_year_name="+from_year_name+"&until_year_name="+until_year_name;
+	}else{
+		paramstr = "format=KML&from_year_name="+from_year_name
+	}
+	
 //	var layer_URL = "http://chinagis-develop.mpiwg-berlin.mpg.de:9080/db/RESTdb/db/public/" + escape(table)  + "?" + paramstr;
 	var layer_URL = "";
 	
+	var tables;
+	
+	if (tablesParam instanceof Array) {
+		tables=tablesParam;
+	} else {
+		tables=new Array(tablesParam);
+	}
 	for (var i=0;i<tables.length;i++){
 		if (i!=0) {e4D_URL=e4D_URL+"&";}
-		layer_URL= "http://mappit.mpiwg-berlin.mpg.de/db/RESTdb/db/public/" + escape(tables[i])  + "?" + paramstr;
+		layer_URL= escape("http://mappit.mpiwg-berlin.mpg.de/db/RESTdb/db/public/" + tables[i] + "?" + paramstr);
+		//layer_URL= escape("http://dw.mpiwg-berlin.mpg.de:8080/db/RESTdb/db/public/" + tables[i]  + "?" + paramstr);
         e4D_URL=e4D_URL+"kml"+[i+1]+"="+layer_URL+"&source"+[i+1]+"=1";
 	}
     console.debug("                e4D_URL=", e4D_URL );
--- a/zpt/HTML_schema_table.zpt	Fri Jan 27 20:02:41 2012 +0100
+++ b/zpt/HTML_schema_table.zpt	Thu Feb 23 11:44:38 2012 +0100
@@ -9,20 +9,25 @@
     <title tal:content="template/title">The title</title>
   </head>
   <body>
+  
     <h2>table <span tal:replace="table"/></h2>
-    <tal:block tal:condition="python:layout=='table'">
+      <tal:block tal:condition="python:layout=='table'">
     <table tal:attributes="id element_id">
       <thead>
         <tr>
-	      <th tal:repeat="field fields" tal:content="python:field[0].replace('st_astext','geometry',1)"/>
+	    <th tal:repeat="field python:here.getFieldNames(fields)" tal:content="python:field.replace('st_astext','geometry',1)"/>
+	   
         </tr>
       </thead>
-      <tbody>
+            <tbody>
         <tr tal:repeat="row rows">
 	      <td tal:repeat="col row" tal:content="col"/>
         </tr>
       </tbody>
+      
     </table>
+    -
     </tal:block>
+  
   </body>
 </html>
\ No newline at end of file
--- a/zpt/KML_schema_table.zpt	Fri Jan 27 20:02:41 2012 +0100
+++ b/zpt/KML_schema_table.zpt	Thu Feb 23 11:44:38 2012 +0100
@@ -3,8 +3,14 @@
   tal:define="gisIdField python:request.get('gis_id_field',None); colorField python:request.get('color_field','red');
   latField python:request.get('lat_field',None); lonField python:request.get('lon_field',None); geomField python:request.get('geometry_field','point');
   sortBy python:request.get('sort_field',1); ids python:request.get('id');
+  from_year_name python:request.get('from_year_name','from_year');
+  until_year_name python:request.get('until_year_name','until_year');
   schema options/schema; table options/table; 
-  data python:here.getKmlData(schema=schema,table=table,sortBy=sortBy,ids=ids,gisIdField=gisIdField,latField=latField,lonField=lonField,geomField=geomField,colorField=colorField ); 
+  data python:here.getKmlData(schema=schema,table=table,
+  sortBy=sortBy,ids=ids,gisIdField=gisIdField,
+  latField=latField,lonField=lonField,
+  geomField=geomField,colorField=colorField,
+  from_year_name=from_year_name,until_year_name=until_year_name ); 
   withLine python:request.get('connect_line',None);">
   <Document>
     <Style id="marker_icon_red">