changeset 22:1a4b56716902

NEW - # 12: create table and upload data https://it-dev.mpiwg-berlin.mpg.de/tracs/GIS/ticket/12 JSON data formats
author casties
date Thu, 19 Aug 2010 14:30:51 +0200
parents a67b7c1f7ec5
children 860ec92f99df
files RestDbInterface.py zpt/JSONHTML_index.zpt zpt/JSONHTML_schema.zpt zpt/JSONHTML_schema_table.zpt
diffstat 4 files changed, 67 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/RestDbInterface.py	Wed Aug 18 16:42:07 2010 +0200
+++ b/RestDbInterface.py	Thu Aug 19 14:30:51 2010 +0200
@@ -74,6 +74,9 @@
     HTML_index = PageTemplateFile('zpt/HTML_index', globals())
     HTML_schema = PageTemplateFile('zpt/HTML_schema', globals())
     HTML_schema_table = PageTemplateFile('zpt/HTML_schema_table', globals())
+    JSONHTML_index = PageTemplateFile('zpt/JSONHTML_index', globals())
+    JSONHTML_schema = PageTemplateFile('zpt/JSONHTML_schema', globals())
+    JSONHTML_schema_table = PageTemplateFile('zpt/JSONHTML_schema_table', globals())
 
     
     
@@ -87,6 +90,10 @@
         self.manage_addFolder('template')
 
 
+    def getJsonString(self,object):
+        """returns a JSON formatted string from object"""
+        return json.dumps(object)
+
     def getCursor(self,autocommit=True):
         """returns fresh DB cursor"""
         conn = getattr(self,"_v_database_connection",None)
@@ -191,16 +198,13 @@
             
         if len(path) == 1:
             # list of schemas
-            return self.showListOfSchemas(format=format)
+            return self.showListOfSchemas(format=format,REQUEST=REQUEST,RESPONSE=RESPONSE)
         elif len(path) == 2:
             # list of tables
-            return self.showListOfTables(format=format,schema=path[1])
+            return self.showListOfTables(format=format,schema=path[1],REQUEST=REQUEST,RESPONSE=RESPONSE)
         elif len(path) == 3:
-            # GIS
-            if format=="GIS":
-                return self.showGoogleMap(schema=path[1],table=path[2],id=id,doc=doc)
             # table
-            return self.showTable(format=format,schema=path[1],table=path[2])
+            return self.showTable(format=format,schema=path[1],table=path[2],REQUEST=REQUEST,RESPONSE=RESPONSE)
         
         # don't know what to do
         return str(REQUEST)
@@ -242,9 +246,21 @@
             return
         
 
-    def showTable(self,format='XML',schema='public',table=None):
+    def showTable(self,format='XML',schema='public',table=None,REQUEST=None,RESPONSE=None):
         """returns PageTemplate with tables"""
         logging.debug("showtable")
+        # GIS gets special treatment
+        if format=="GIS":
+            return self.showGoogleMap(schema=path[1],table=path[2],id=id,doc=doc)
+
+        # JSON gets special treatment
+        if format == "JSON":
+            data = self.getListOfTables(schema)
+            RESPONSE.setHeader("Content-Type", "application/json")
+            json.dump(data, RESPONSE)
+            return
+        
+        # everything else has its own template
         pt = getattr(self.template, '%s_schema_table'%format, None)
         if pt is None:
             return "ERROR!! template %s_schema_table not found"%format
@@ -252,16 +268,23 @@
         data = self.getTable(schema,table)
         return pt(data=data,tablename=table)
  
- 
     def getTable(self,schema='public',table=None,username='guest'):
         """return table data"""
         logging.debug("gettable")
         data = self.executeSQL('select * from "%s"."%s"'%(schema,table))
         return data
 
-    def showListOfTables(self,format='XML',schema='public'):
+    def showListOfTables(self,format='XML',schema='public',REQUEST=None,RESPONSE=None):
         """returns PageTemplate with list of tables"""
         logging.debug("showlistoftables")
+        # JSON gets special treatment
+        if format == "JSON":
+            data = self.getListOfTables(schema)
+            RESPONSE.setHeader("Content-Type", "application/json")
+            json.dump(data, RESPONSE)
+            return
+            
+        # everything else has its own template
         pt = getattr(self.template, '%s_schema'%format, None)
         if pt is None:
             return "ERROR!! template %s_schema not found"%format
@@ -273,7 +296,7 @@
         """return list of tables"""
         logging.debug("getlistoftables")
         # get list of fields and types of db table
-        qstr="""select c.relname FROM pg_catalog.pg_class c
+        qstr="""SELECT c.relname AS tablename FROM pg_catalog.pg_class c
             LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
             WHERE c.relkind IN ('r','') AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
             AND pg_catalog.pg_table_is_visible(c.oid)"""
@@ -281,9 +304,15 @@
         data=self.executeSQL(qstr)
         return data
 
-    def showListOfSchemas(self,format='XML'):
+    def showListOfSchemas(self,format='XML',REQUEST=None,RESPONSE=None):
         """returns PageTemplate with list of schemas"""
         logging.debug("showlistofschemas")
+        # JSON gets special treatment
+        if format == "JSON":
+            data = self.getListOfSchemas()
+            RESPONSE.setHeader("Content-Type", "application/json")
+            json.dump(data, RESPONSE)
+            return
         pt = getattr(self.template, '%s_index'%format, None)
         if pt is None:
             return "ERROR!! template %s_index not found"%format
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/zpt/JSONHTML_index.zpt	Thu Aug 19 14:30:51 2010 +0200
@@ -0,0 +1,9 @@
+<html tal:define="layout python:request.get('layout','pre'); element_id python:request.get('element_id',None)">
+  <head>
+    <meta http-equiv="content-type" content="text/html;charset=utf-8">
+    <title tal:content="template/title">The title</title>
+  </head>
+  <body>
+  <pre tal:attributes="id element_id" tal:content="python:here.getJsonString(options['data'])"></pre>
+  </body>
+</html>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/zpt/JSONHTML_schema.zpt	Thu Aug 19 14:30:51 2010 +0200
@@ -0,0 +1,9 @@
+<html tal:define="layout python:request.get('layout','pre'); element_id python:request.get('element_id',None)">
+  <head>
+    <meta http-equiv="content-type" content="text/html;charset=utf-8">
+    <title tal:content="template/title">The title</title>
+  </head>
+  <body>
+  <pre tal:attributes="id element_id" tal:content="python:here.getJsonString(options['data'])"></pre>
+  </body>
+</html>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/zpt/JSONHTML_schema_table.zpt	Thu Aug 19 14:30:51 2010 +0200
@@ -0,0 +1,9 @@
+<html tal:define="layout python:request.get('layout','pre'); element_id python:request.get('element_id',None)">
+  <head>
+    <meta http-equiv="content-type" content="text/html;charset=utf-8">
+    <title tal:content="template/title">The title</title>
+  </head>
+  <body>
+  <pre tal:attributes="id element_id" tal:content="python:here.getJsonString(options['data'])"></pre>
+  </body>
+</html>
\ No newline at end of file