42
|
1 '''
|
|
2 Created on 2.9.2010
|
|
3
|
|
4 @author: casties
|
|
5 '''
|
|
6
|
|
7 from OFS.Folder import Folder
|
|
8 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
|
|
9 import logging
|
|
10 import re
|
|
11 import psycopg2
|
|
12 import json
|
|
13 import time
|
|
14
|
|
15 from zope.interface import implements
|
|
16 from zope.publisher.interfaces import IPublishTraverse
|
|
17 from ZPublisher.BaseRequest import DefaultPublishTraverse
|
|
18 import Shared.DC.ZRDB.DA
|
|
19 from Products.ZSQLMethods.SQL import SQLConnectionIDs
|
|
20
|
43
|
21 from RestDbInterface import *
|
42
|
22
|
|
23
|
|
24 class RestDbJsonStore(RestDbInterface):
|
46
|
25 """Object for RESTful access to JSON objects
|
|
26 path schema: /db/{schema}/{table}/{tag}/{type}/{item}
|
42
|
27 """
|
|
28 implements(IPublishTraverse)
|
|
29
|
46
|
30 meta_type="RESTjson"
|
42
|
31 manage_options=Folder.manage_options+(
|
|
32 {'label':'Config','action':'manage_editRestDbJsonStoreForm'},
|
|
33 )
|
|
34
|
|
35 # management templates
|
|
36 manage_editRestDbJsonStoreForm=PageTemplateFile('zpt/editRestDbJsonStore',globals())
|
|
37
|
|
38 # JSON_* templates are scripts
|
|
39 def JSON_index(self,data):
|
|
40 """JSON index function"""
|
|
41 self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json")
|
|
42 json.dump(data, self.REQUEST.RESPONSE)
|
|
43
|
|
44
|
|
45 def __init__(self, id, title, connection_id=None):
|
|
46 """init"""
|
|
47 self.id = id
|
|
48 self.title = title
|
|
49 # database connection id
|
|
50 self.connection_id = connection_id
|
|
51
|
|
52 def getJsonString(self,object):
|
|
53 """returns a JSON formatted string from object"""
|
|
54 return json.dumps(object)
|
|
55
|
|
56
|
|
57 def publishTraverse(self,request,name):
|
|
58 """change the traversal"""
|
|
59 # get stored path
|
|
60 path = request.get('restdb_path', [])
|
|
61 logging.debug("publishtraverse: name=%s restdb_path=%s"%(name,path))
|
|
62
|
|
63 if name in ("index_html", "PUT"):
|
|
64 # end of traversal
|
|
65 if request.get("method") == "POST" and request.get("action",None) == "PUT":
|
|
66 # fake PUT by POST with action=PUT
|
|
67 name = "PUT"
|
|
68
|
|
69 return getattr(self, name)
|
|
70 #TODO: should we check more?
|
|
71 else:
|
|
72 # traverse
|
|
73 if len(path) == 0:
|
|
74 # first segment
|
|
75 if name == 'db':
|
|
76 # virtual path -- continue traversing
|
|
77 path = [name]
|
|
78 request['restdb_path'] = path
|
|
79 else:
|
|
80 # try real path
|
|
81 tr = DefaultPublishTraverse(self, request)
|
|
82 ob = tr.publishTraverse(request, name)
|
|
83 return ob
|
|
84 else:
|
|
85 path.append(name)
|
|
86
|
|
87 # continue traversing
|
|
88 return self
|
|
89
|
|
90 def index_html(self,REQUEST,RESPONSE):
|
|
91 """index method"""
|
|
92 # ReST path was stored in request
|
|
93 path = REQUEST.get('restdb_path',[])
|
|
94
|
|
95 # type and format are real parameter
|
|
96 resultFormat = REQUEST.get('format','HTML').upper()
|
|
97 queryType = REQUEST.get('type',None)
|
|
98
|
|
99 logging.debug("index_html path=%s resultFormat=%s queryType=%s"%(path,resultFormat,queryType))
|
|
100
|
|
101 if queryType is not None:
|
|
102 # non-empty queryType -- look for template
|
|
103 pt = getattr(self.template, "%s_%s"%(resultFormat,queryType), None)
|
|
104 if pt is not None:
|
|
105 return pt(format=resultFormat,type=queryType,path=path)
|
|
106
|
46
|
107 if len(path) == 3:
|
|
108 # list of tags
|
|
109 return self.showListOfTags(resultFormat=resultFormat,schema=path[1],table=path[2])
|
|
110 elif len(path) == 4:
|
42
|
111 # list of tables
|
|
112 return self.showListOfTables(resultFormat=resultFormat,schema=path[1])
|
|
113 elif len(path) == 3:
|
|
114 # table
|
|
115 if REQUEST.get("method") == "POST" and REQUEST.get("create_table_file",None) is not None:
|
|
116 # POST to table to check
|
|
117 return self.checkTable(resultFormat=resultFormat,schema=path[1],table=path[2])
|
|
118 # else show table
|
|
119 return self.showTable(resultFormat=resultFormat,schema=path[1],table=path[2])
|
|
120
|
|
121 # don't know what to do
|
|
122 return str(REQUEST)
|
|
123
|
|
124 def PUT(self, REQUEST, RESPONSE):
|
|
125 """
|
|
126 Implement WebDAV/HTTP PUT/FTP put method for this object.
|
|
127 """
|
|
128 logging.debug("RestDbInterface PUT")
|
|
129 #logging.debug("req=%s"%REQUEST)
|
|
130 #self.dav__init(REQUEST, RESPONSE)
|
|
131 #self.dav__simpleifhandler(REQUEST, RESPONSE)
|
|
132 # ReST path was stored in request
|
|
133 path = REQUEST.get('restdb_path',[])
|
|
134 if len(path) == 3:
|
|
135 schema = path[1]
|
|
136 tablename = path[2]
|
|
137 file = REQUEST.get("create_table_file",None)
|
|
138 if file is None:
|
|
139 RESPONSE.setStatus(400)
|
|
140 return
|
|
141
|
|
142 fields = None
|
|
143 fieldsStr = REQUEST.get("create_table_fields",None)
|
|
144 logging.debug("put with schema=%s table=%s file=%s fields=%s"%(schema,tablename,file,repr(fieldsStr)))
|
|
145 if fieldsStr is not None:
|
|
146 # unpack fields
|
|
147 fields = [{"name":n, "type": t} for (n,t) in [f.split(":") for f in fieldsStr.split(",")]]
|
|
148
|
|
149 ret = self.createTableFromXML(schema, tablename, file, fields)
|
|
150 # return the result as JSON
|
|
151 format = REQUEST.get("format","JSON")
|
|
152 if format == "JSON":
|
|
153 RESPONSE.setHeader("Content-Type", "application/json")
|
|
154 json.dump(ret, RESPONSE)
|
|
155
|
|
156 elif format == "JSONHTML":
|
|
157 RESPONSE.setHeader("Content-Type", "text/html")
|
|
158 RESPONSE.write("<html>\n<body>\n<pre>")
|
|
159 json.dump(ret, RESPONSE)
|
|
160 RESPONSE.write("</pre>\n</body>\n</html>")
|
|
161
|
|
162 else:
|
|
163 # 400 Bad Request
|
|
164 RESPONSE.setStatus(400)
|
|
165 return
|
|
166
|
|
167
|
|
168 manage_addRestDbInterfaceForm=PageTemplateFile('zpt/addRestDbInterface',globals())
|
|
169
|
|
170 def manage_addRestDbInterface(self, id, title='', label='', description='',
|
|
171 createPublic=0,
|
|
172 createUserF=0,
|
|
173 REQUEST=None):
|
|
174 """Add a new object with id *id*."""
|
|
175
|
|
176 ob=RestDbInterface(str(id),title)
|
|
177 self._setObject(id, ob)
|
|
178
|
|
179 #checkPermission=getSecurityManager().checkPermission
|
|
180 REQUEST.RESPONSE.redirect('manage_main')
|
|
181
|
|
182
|