annotate RestDbJsonStore.py @ 48:2ba80c8bb47f

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