Mercurial > hg > ChinaGisRestApi
comparison RestDbInterface.py @ 254:901c1f745d13
GIS-Links from MPDL-Documents now served by Mappit-Server
author | fknauft |
---|---|
date | Wed, 28 Sep 2011 15:09:14 +0200 |
parents | efd2469d1722 |
children | 3a10287447b1 |
comparison
equal
deleted
inserted
replaced
253:d6c7bedf4370 | 254:901c1f745d13 |
---|---|
11 import logging | 11 import logging |
12 import re | 12 import re |
13 import json | 13 import json |
14 import time | 14 import time |
15 import psycopg2 | 15 import psycopg2 |
16 import urllib | |
17 | |
16 # make psycopg use unicode objects | 18 # make psycopg use unicode objects |
17 import psycopg2.extensions | 19 import psycopg2.extensions |
18 psycopg2.extensions.register_type(psycopg2.extensions.UNICODE) | 20 psycopg2.extensions.register_type(psycopg2.extensions.UNICODE) |
19 psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY) | 21 psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY) |
20 | 22 |
95 XML_schema = PageTemplateFile('zpt/XML_schema', globals()) | 97 XML_schema = PageTemplateFile('zpt/XML_schema', globals()) |
96 XML_schema_table = PageTemplateFile('zpt/XML_schema_table', globals()) | 98 XML_schema_table = PageTemplateFile('zpt/XML_schema_table', globals()) |
97 HTML_index = PageTemplateFile('zpt/HTML_index', globals()) | 99 HTML_index = PageTemplateFile('zpt/HTML_index', globals()) |
98 HTML_schema = PageTemplateFile('zpt/HTML_schema', globals()) | 100 HTML_schema = PageTemplateFile('zpt/HTML_schema', globals()) |
99 HTML_schema_table = PageTemplateFile('zpt/HTML_schema_table', globals()) | 101 HTML_schema_table = PageTemplateFile('zpt/HTML_schema_table', globals()) |
102 GIS_schema_table = PageTemplateFile('zpt/GIS_schema_table', globals()) | |
103 KML_schema_table = PageTemplateFile('zpt/KML_schema_table', globals()) | |
104 HTML_schema_usertables = PageTemplateFile('zpt/HTML_schema_usertables', globals()) | |
105 | |
106 | |
107 | |
100 JSONHTML_index = PageTemplateFile('zpt/JSONHTML_index', globals()) | 108 JSONHTML_index = PageTemplateFile('zpt/JSONHTML_index', globals()) |
101 JSONHTML_schema = PageTemplateFile('zpt/JSONHTML_schema', globals()) | 109 JSONHTML_schema = PageTemplateFile('zpt/JSONHTML_schema', globals()) |
102 JSONHTML_schema_table = PageTemplateFile('zpt/JSONHTML_schema_table', globals()) | 110 JSONHTML_schema_table = PageTemplateFile('zpt/JSONHTML_schema_table', globals()) |
103 # JSON_* templates are scripts | 111 # JSON_* templates are scripts |
112 | |
113 | |
104 def JSON_index(self): | 114 def JSON_index(self): |
105 """JSON index function""" | 115 """JSON index function""" |
106 self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json") | 116 self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json") |
107 json.dump(self.getListOfSchemas(), self.REQUEST.RESPONSE) | 117 json.dump(self.getListOfSchemas(), self.REQUEST.RESPONSE) |
108 | 118 |
269 # table | 279 # table |
270 if REQUEST.get("method") == "POST" and REQUEST.get("create_table_file",None) is not None: | 280 if REQUEST.get("method") == "POST" and REQUEST.get("create_table_file",None) is not None: |
271 # POST to table to check | 281 # POST to table to check |
272 return self.checkTable(format=resultFormat,schema=path[1],table=path[2]) | 282 return self.checkTable(format=resultFormat,schema=path[1],table=path[2]) |
273 # else show table | 283 # else show table |
274 return self.showTable(format=resultFormat,schema=path[1],table=path[2]) | 284 return self.showTable(format=resultFormat,schema=path[1],table=path[2],REQUEST=REQUEST, RESPONSE=RESPONSE) |
275 | 285 |
276 # don't know what to do | 286 # don't know what to do |
277 return str(REQUEST) | 287 return str(REQUEST) |
278 | 288 |
279 def PUT(self, REQUEST, RESPONSE): | 289 def PUT(self, REQUEST, RESPONSE): |
327 def showTable(self,format='XML',schema='public',table=None,REQUEST=None,RESPONSE=None): | 337 def showTable(self,format='XML',schema='public',table=None,REQUEST=None,RESPONSE=None): |
328 """returns PageTemplate with tables""" | 338 """returns PageTemplate with tables""" |
329 logging.debug("showtable") | 339 logging.debug("showtable") |
330 if REQUEST is None: | 340 if REQUEST is None: |
331 REQUEST = self.REQUEST | 341 REQUEST = self.REQUEST |
332 | 342 queryArgs={'doc':None,'id':None} |
343 queryArgs['doc'] = REQUEST.get('doc') | |
344 queryArgs['id'] = REQUEST.get('id') | |
345 | |
333 # should be cross-site accessible | 346 # should be cross-site accessible |
334 if RESPONSE is None: | 347 if RESPONSE is None: |
335 RESPONSE = self.REQUEST.RESPONSE | 348 RESPONSE = self.REQUEST.RESPONSE |
336 | 349 |
337 RESPONSE.setHeader('Access-Control-Allow-Origin', '*') | 350 RESPONSE.setHeader('Access-Control-Allow-Origin', '*') |
338 | 351 |
339 # everything else has its own template | 352 # everything else has its own template |
340 pt = getattr(self.template, '%s_schema_table'%format, None) | 353 pt = getattr(self.template, '%s_schema_table'%format, REQUEST) |
341 if pt is None: | 354 if pt is None: |
342 return "ERROR!! template %s_schema_table not found"%format | 355 return "ERROR!! template %s_schema_table not found at %s"%(format, self.template ) |
343 | |
344 #data = self.getTable(schema,table) | 356 #data = self.getTable(schema,table) |
345 return pt(schema=schema,table=table) | 357 return pt(schema=schema,table=table,args=queryArgs) |
358 | |
359 def getLiveUrl(self,schema,table,useTimestamp=True,REQUEST=None): | |
360 if REQUEST is None: | |
361 REQUEST = self.REQUEST | |
362 logging.debug("getLiveUrl") | |
363 baseUrl = self.absolute_url() | |
364 timestamp = time.time() | |
365 # filter parameters in URL and add to new URL | |
366 params = [p for p in REQUEST.form.items() if p[0] not in ('format','timestamp')] | |
367 params.append(('format','KML')) | |
368 if useTimestamp: | |
369 # add timestamp so URL changes every time | |
370 params.append(('timestamp',timestamp)) | |
371 paramstr = urllib.urlencode(params) | |
372 return "%s/db/%s/%s?%s"%(baseUrl,schema,table,paramstr) | |
373 | |
374 | |
346 | 375 |
347 def getTable(self,schema='public',table=None,sortBy=1,username='guest'): | 376 def getTable(self,schema='public',table=None,sortBy=1,username='guest'): |
348 """return table data""" | 377 """return table data""" |
349 logging.debug("gettable") | 378 logging.debug("gettable") |
350 attrNames=self.getAttributeNames(schema,table) | 379 attrNames=self.getAttributeNames(schema,table) |