comparison RestDbInterface.py @ 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
comparison
equal deleted inserted replaced
21:a67b7c1f7ec5 22:1a4b56716902
72 XML_schema = PageTemplateFile('zpt/XML_schema', globals()) 72 XML_schema = PageTemplateFile('zpt/XML_schema', globals())
73 XML_schema_table = PageTemplateFile('zpt/XML_schema_table', globals()) 73 XML_schema_table = PageTemplateFile('zpt/XML_schema_table', globals())
74 HTML_index = PageTemplateFile('zpt/HTML_index', globals()) 74 HTML_index = PageTemplateFile('zpt/HTML_index', globals())
75 HTML_schema = PageTemplateFile('zpt/HTML_schema', globals()) 75 HTML_schema = PageTemplateFile('zpt/HTML_schema', globals())
76 HTML_schema_table = PageTemplateFile('zpt/HTML_schema_table', globals()) 76 HTML_schema_table = PageTemplateFile('zpt/HTML_schema_table', globals())
77 JSONHTML_index = PageTemplateFile('zpt/JSONHTML_index', globals())
78 JSONHTML_schema = PageTemplateFile('zpt/JSONHTML_schema', globals())
79 JSONHTML_schema_table = PageTemplateFile('zpt/JSONHTML_schema_table', globals())
77 80
78 81
79 82
80 def __init__(self, id, title, connection_id=None): 83 def __init__(self, id, title, connection_id=None):
81 """init""" 84 """init"""
84 # database connection id 87 # database connection id
85 self.connection_id = connection_id 88 self.connection_id = connection_id
86 # create template folder 89 # create template folder
87 self.manage_addFolder('template') 90 self.manage_addFolder('template')
88 91
92
93 def getJsonString(self,object):
94 """returns a JSON formatted string from object"""
95 return json.dumps(object)
89 96
90 def getCursor(self,autocommit=True): 97 def getCursor(self,autocommit=True):
91 """returns fresh DB cursor""" 98 """returns fresh DB cursor"""
92 conn = getattr(self,"_v_database_connection",None) 99 conn = getattr(self,"_v_database_connection",None)
93 if conn is None: 100 if conn is None:
189 if pt is not None: 196 if pt is not None:
190 return pt(format=format,type=type,path=path) 197 return pt(format=format,type=type,path=path)
191 198
192 if len(path) == 1: 199 if len(path) == 1:
193 # list of schemas 200 # list of schemas
194 return self.showListOfSchemas(format=format) 201 return self.showListOfSchemas(format=format,REQUEST=REQUEST,RESPONSE=RESPONSE)
195 elif len(path) == 2: 202 elif len(path) == 2:
196 # list of tables 203 # list of tables
197 return self.showListOfTables(format=format,schema=path[1]) 204 return self.showListOfTables(format=format,schema=path[1],REQUEST=REQUEST,RESPONSE=RESPONSE)
198 elif len(path) == 3: 205 elif len(path) == 3:
199 # GIS
200 if format=="GIS":
201 return self.showGoogleMap(schema=path[1],table=path[2],id=id,doc=doc)
202 # table 206 # table
203 return self.showTable(format=format,schema=path[1],table=path[2]) 207 return self.showTable(format=format,schema=path[1],table=path[2],REQUEST=REQUEST,RESPONSE=RESPONSE)
204 208
205 # don't know what to do 209 # don't know what to do
206 return str(REQUEST) 210 return str(REQUEST)
207 211
208 def PUT(self, REQUEST, RESPONSE): 212 def PUT(self, REQUEST, RESPONSE):
240 # 400 Bad Request 244 # 400 Bad Request
241 RESPONSE.setStatus(400) 245 RESPONSE.setStatus(400)
242 return 246 return
243 247
244 248
245 def showTable(self,format='XML',schema='public',table=None): 249 def showTable(self,format='XML',schema='public',table=None,REQUEST=None,RESPONSE=None):
246 """returns PageTemplate with tables""" 250 """returns PageTemplate with tables"""
247 logging.debug("showtable") 251 logging.debug("showtable")
252 # GIS gets special treatment
253 if format=="GIS":
254 return self.showGoogleMap(schema=path[1],table=path[2],id=id,doc=doc)
255
256 # JSON gets special treatment
257 if format == "JSON":
258 data = self.getListOfTables(schema)
259 RESPONSE.setHeader("Content-Type", "application/json")
260 json.dump(data, RESPONSE)
261 return
262
263 # everything else has its own template
248 pt = getattr(self.template, '%s_schema_table'%format, None) 264 pt = getattr(self.template, '%s_schema_table'%format, None)
249 if pt is None: 265 if pt is None:
250 return "ERROR!! template %s_schema_table not found"%format 266 return "ERROR!! template %s_schema_table not found"%format
251 267
252 data = self.getTable(schema,table) 268 data = self.getTable(schema,table)
253 return pt(data=data,tablename=table) 269 return pt(data=data,tablename=table)
254
255 270
256 def getTable(self,schema='public',table=None,username='guest'): 271 def getTable(self,schema='public',table=None,username='guest'):
257 """return table data""" 272 """return table data"""
258 logging.debug("gettable") 273 logging.debug("gettable")
259 data = self.executeSQL('select * from "%s"."%s"'%(schema,table)) 274 data = self.executeSQL('select * from "%s"."%s"'%(schema,table))
260 return data 275 return data
261 276
262 def showListOfTables(self,format='XML',schema='public'): 277 def showListOfTables(self,format='XML',schema='public',REQUEST=None,RESPONSE=None):
263 """returns PageTemplate with list of tables""" 278 """returns PageTemplate with list of tables"""
264 logging.debug("showlistoftables") 279 logging.debug("showlistoftables")
280 # JSON gets special treatment
281 if format == "JSON":
282 data = self.getListOfTables(schema)
283 RESPONSE.setHeader("Content-Type", "application/json")
284 json.dump(data, RESPONSE)
285 return
286
287 # everything else has its own template
265 pt = getattr(self.template, '%s_schema'%format, None) 288 pt = getattr(self.template, '%s_schema'%format, None)
266 if pt is None: 289 if pt is None:
267 return "ERROR!! template %s_schema not found"%format 290 return "ERROR!! template %s_schema not found"%format
268 291
269 data = self.getListOfTables(schema) 292 data = self.getListOfTables(schema)
271 294
272 def getListOfTables(self,schema='public',username='guest'): 295 def getListOfTables(self,schema='public',username='guest'):
273 """return list of tables""" 296 """return list of tables"""
274 logging.debug("getlistoftables") 297 logging.debug("getlistoftables")
275 # get list of fields and types of db table 298 # get list of fields and types of db table
276 qstr="""select c.relname FROM pg_catalog.pg_class c 299 qstr="""SELECT c.relname AS tablename FROM pg_catalog.pg_class c
277 LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace 300 LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
278 WHERE c.relkind IN ('r','') AND n.nspname NOT IN ('pg_catalog', 'pg_toast') 301 WHERE c.relkind IN ('r','') AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
279 AND pg_catalog.pg_table_is_visible(c.oid)""" 302 AND pg_catalog.pg_table_is_visible(c.oid)"""
280 #qstr="select attname, format_type(pg_attribute.atttypid, pg_attribute.atttypmod) from pg_attribute, pg_class where attrelid = pg_class.oid and pg_attribute.attnum > 0" 303 #qstr="select attname, format_type(pg_attribute.atttypid, pg_attribute.atttypmod) from pg_attribute, pg_class where attrelid = pg_class.oid and pg_attribute.attnum > 0"
281 data=self.executeSQL(qstr) 304 data=self.executeSQL(qstr)
282 return data 305 return data
283 306
284 def showListOfSchemas(self,format='XML'): 307 def showListOfSchemas(self,format='XML',REQUEST=None,RESPONSE=None):
285 """returns PageTemplate with list of schemas""" 308 """returns PageTemplate with list of schemas"""
286 logging.debug("showlistofschemas") 309 logging.debug("showlistofschemas")
310 # JSON gets special treatment
311 if format == "JSON":
312 data = self.getListOfSchemas()
313 RESPONSE.setHeader("Content-Type", "application/json")
314 json.dump(data, RESPONSE)
315 return
287 pt = getattr(self.template, '%s_index'%format, None) 316 pt = getattr(self.template, '%s_index'%format, None)
288 if pt is None: 317 if pt is None:
289 return "ERROR!! template %s_index not found"%format 318 return "ERROR!! template %s_index not found"%format
290 319
291 data = self.getListOfSchemas() 320 data = self.getListOfSchemas()