Mercurial > hg > ChinaGisRestApi
view RestDbJsonStore.py @ 47:698ef00f2717
more json store
author | casties |
---|---|
date | Fri, 03 Sep 2010 14:00:09 +0200 |
parents | 562717546168 |
children | 2ba80c8bb47f |
line wrap: on
line source
''' Created on 2.9.2010 @author: casties ''' from OFS.Folder import Folder from Products.PageTemplates.PageTemplateFile import PageTemplateFile import logging import re import psycopg2 import json import time from zope.interface import implements from zope.publisher.interfaces import IPublishTraverse from ZPublisher.BaseRequest import DefaultPublishTraverse import Shared.DC.ZRDB.DA from Products.ZSQLMethods.SQL import SQLConnectionIDs from RestDbInterface import * class RestDbJsonStore(RestDbInterface): """Object for RESTful database queries path schema: /db/{schema}/{table}/ omitting table gives a list of schemas omitting table and schema gives a list of schemas """ implements(IPublishTraverse) meta_type="RESTdbJSONStore" manage_options=Folder.manage_options+( {'label':'Config','action':'manage_editRestDbJsonStoreForm'}, ) # management templates manage_editRestDbJsonStoreForm=PageTemplateFile('zpt/editRestDbJsonStore',globals()) # JSON_* templates are scripts def JSON_index(self,data): """JSON index function""" self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json") json.dump(data, self.REQUEST.RESPONSE) def JSON_schema(self,data,schema): """JSON index function""" self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json") json.dump(data, self.REQUEST.RESPONSE) def JSON_schema_table(self,data,tablename): """JSON index function""" self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json") json.dump(data, self.REQUEST.RESPONSE) def __init__(self, id, title, connection_id=None): """init""" self.id = id self.title = title # database connection id self.connection_id = connection_id def getJsonString(self,object): """returns a JSON formatted string from object""" return json.dumps(object) def publishTraverse(self,request,name): """change the traversal""" # get stored path path = request.get('restdb_path', []) logging.debug("publishtraverse: name=%s restdb_path=%s"%(name,path)) if name in ("index_html", "PUT"): # end of traversal if request.get("method") == "POST" and request.get("action",None) == "PUT": # fake PUT by POST with action=PUT name = "PUT" return getattr(self, name) #TODO: should we check more? else: # traverse if len(path) == 0: # first segment if name == 'db': # virtual path -- continue traversing path = [name] request['restdb_path'] = path else: # try real path tr = DefaultPublishTraverse(self, request) ob = tr.publishTraverse(request, name) return ob else: path.append(name) # continue traversing return self def index_html(self,REQUEST,RESPONSE): """index method""" # ReST path was stored in request path = REQUEST.get('restdb_path',[]) # type and format are real parameter resultFormat = REQUEST.get('format','HTML').upper() queryType = REQUEST.get('type',None) logging.debug("index_html path=%s resultFormat=%s queryType=%s"%(path,resultFormat,queryType)) if queryType is not None: # 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) if len(path) == 1: # list of schemas return self.showListOfSchemas(resultFormat=resultFormat) elif len(path) == 2: # list of tables return self.showListOfTables(resultFormat=resultFormat,schema=path[1]) elif len(path) == 3: # table if REQUEST.get("method") == "POST" and REQUEST.get("create_table_file",None) is not None: # POST to table to check return self.checkTable(resultFormat=resultFormat,schema=path[1],table=path[2]) # else show table return self.showTable(resultFormat=resultFormat,schema=path[1],table=path[2]) # don't know what to do return str(REQUEST) def PUT(self, REQUEST, RESPONSE): """ Implement WebDAV/HTTP PUT/FTP put method for this object. """ logging.debug("RestDbInterface PUT") #logging.debug("req=%s"%REQUEST) #self.dav__init(REQUEST, RESPONSE) #self.dav__simpleifhandler(REQUEST, RESPONSE) # ReST path was stored in request path = REQUEST.get('restdb_path',[]) if len(path) == 3: schema = path[1] tablename = path[2] file = REQUEST.get("create_table_file",None) if file is None: RESPONSE.setStatus(400) return fields = None fieldsStr = REQUEST.get("create_table_fields",None) logging.debug("put with schema=%s table=%s file=%s fields=%s"%(schema,tablename,file,repr(fieldsStr))) if fieldsStr is not None: # unpack fields fields = [{"name":n, "type": t} for (n,t) in [f.split(":") for f in fieldsStr.split(",")]] ret = self.createTableFromXML(schema, tablename, file, fields) # return the result as JSON format = REQUEST.get("format","JSON") if format == "JSON": RESPONSE.setHeader("Content-Type", "application/json") json.dump(ret, RESPONSE) elif format == "JSONHTML": RESPONSE.setHeader("Content-Type", "text/html") RESPONSE.write("<html>\n<body>\n<pre>") json.dump(ret, RESPONSE) RESPONSE.write("</pre>\n</body>\n</html>") else: # 400 Bad Request RESPONSE.setStatus(400) return manage_addRestDbInterfaceForm=PageTemplateFile('zpt/addRestDbInterface',globals()) def manage_addRestDbInterface(self, id, title='', label='', description='', createPublic=0, createUserF=0, REQUEST=None): """Add a new object with id *id*.""" ob=RestDbInterface(str(id),title) self._setObject(id, ob) #checkPermission=getSecurityManager().checkPermission REQUEST.RESPONSE.redirect('manage_main')