annotate RestDbJsonStore.py @ 235:ed0cb46fb936

show line and polygon-layer
author fknauft
date Mon, 05 Sep 2011 11:37:10 +0200
parents 9ec7e32e8ad3
children 4ade9b80b563
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:
49
e4828cb72ce2 working on json store
casties
parents: 48
diff changeset
111 # list of types
e4828cb72ce2 working on json store
casties
parents: 48
diff changeset
112 return self.showListOfTypes(resultFormat=resultFormat,schema=path[1],table=path[2],tag=path[3])
e4828cb72ce2 working on json store
casties
parents: 48
diff changeset
113 elif len(path) == 5:
e4828cb72ce2 working on json store
casties
parents: 48
diff changeset
114 # list of types
e4828cb72ce2 working on json store
casties
parents: 48
diff changeset
115 return self.showListOfItems(resultFormat=resultFormat,schema=path[1],table=path[2],tag=path[3],type=path[4])
e4828cb72ce2 working on json store
casties
parents: 48
diff changeset
116 elif len(path) == 6:
e4828cb72ce2 working on json store
casties
parents: 48
diff changeset
117 # show item
e4828cb72ce2 working on json store
casties
parents: 48
diff changeset
118 return self.showItem(resultFormat=resultFormat,schema=path[1],table=path[2],tag=path[3],type=path[4],item=path[5])
42
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
119
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
120 # don't know what to do
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
121 return str(REQUEST)
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
122
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
123 def PUT(self, REQUEST, RESPONSE):
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
124 """
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
125 Implement WebDAV/HTTP PUT/FTP put method for this object.
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
126 """
49
e4828cb72ce2 working on json store
casties
parents: 48
diff changeset
127 path = REQUEST.get('restdb_path',[])
e4828cb72ce2 working on json store
casties
parents: 48
diff changeset
128 logging.debug("RestDbInterface PUT (path=%s)"%repr(path))
52
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
129 logging.debug("PUT request=%s"%REQUEST)
42
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
130 #logging.debug("req=%s"%REQUEST)
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
131 #self.dav__init(REQUEST, RESPONSE)
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
132 #self.dav__simpleifhandler(REQUEST, RESPONSE)
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
133 # ReST path was stored in request
52
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
134 if len(path) == 6:
42
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
135 schema = path[1]
52
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
136 table = path[2]
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
137 tag = path[3]
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
138 type = path[4]
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
139 item = path[5]
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
140 # maybe the data was form-encoded
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
141 value = REQUEST.get('json_string', None)
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
142 if value is None:
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
143 # then maybe in the body (the hard way...)
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
144 REQUEST.stdin.seek(0)
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
145 value = REQUEST.stdin.read()
42
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
146
52
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
147 logging.debug("put with schema=%s table=%s tag=%s type=%s item=%s value=%s"%(schema,table,tag,type,item,value))
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
148 res = self.storeItem(schema,table,tag,type,item,value)
42
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
149
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
150 else:
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
151 # 400 Bad Request
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
152 RESPONSE.setStatus(400)
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
153 return
50
29c822d15bc1 more JSON store
casties
parents: 49
diff changeset
154
49
e4828cb72ce2 working on json store
casties
parents: 48
diff changeset
155 def showListOfTags(self,resultFormat,schema,table):
52
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
156 """shows the list of existing tags"""
50
29c822d15bc1 more JSON store
casties
parents: 49
diff changeset
157 tags = self.getListOfTags(schema, table)
29c822d15bc1 more JSON store
casties
parents: 49
diff changeset
158 if resultFormat == 'JSON':
29c822d15bc1 more JSON store
casties
parents: 49
diff changeset
159 self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json")
29c822d15bc1 more JSON store
casties
parents: 49
diff changeset
160 json.dump(tags, self.REQUEST.RESPONSE)
29c822d15bc1 more JSON store
casties
parents: 49
diff changeset
161 else:
29c822d15bc1 more JSON store
casties
parents: 49
diff changeset
162 json.dump(tags, self.REQUEST.RESPONSE)
29c822d15bc1 more JSON store
casties
parents: 49
diff changeset
163
29c822d15bc1 more JSON store
casties
parents: 49
diff changeset
164 def getListOfTags(self,schema,table):
29c822d15bc1 more JSON store
casties
parents: 49
diff changeset
165 """returns the list of existing tags"""
29c822d15bc1 more JSON store
casties
parents: 49
diff changeset
166 logging.debug("getlistoftags schema=%s table=%s"%(schema,table))
29c822d15bc1 more JSON store
casties
parents: 49
diff changeset
167 sql = 'select distinct json_tag from "%s"."%s"'%(schema,table)
49
e4828cb72ce2 working on json store
casties
parents: 48
diff changeset
168 res = self.executeSQL(sql)
e4828cb72ce2 working on json store
casties
parents: 48
diff changeset
169 if len(res['rows']) > 0:
51
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
170 tags = [r[0] for r in res['rows']]
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
171 return tags
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
172
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
173 return []
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
174
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
175 def showListOfTypes(self,resultFormat,schema,table,tag):
52
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
176 """shows the list of existing types"""
51
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
177 types = self.getListOfTypes(schema, table,tag)
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
178 if resultFormat == 'JSON':
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
179 self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json")
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
180 json.dump(types, self.REQUEST.RESPONSE)
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
181 else:
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
182 json.dump(types, self.REQUEST.RESPONSE)
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
183
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
184 def getListOfTypes(self,schema,table,tag):
52
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
185 """returns the list of existing types"""
51
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
186 logging.debug("getlistoftypes schema=%s table=%s tag=%s"%(schema,table,tag))
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
187 sql = 'select distinct json_type from "%s"."%s" where json_tag = %%s'%(schema,table)
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
188 res = self.executeSQL(sql,(tag,))
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
189 if len(res['rows']) > 0:
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
190 tags = [r[0] for r in res['rows']]
49
e4828cb72ce2 working on json store
casties
parents: 48
diff changeset
191 return tags
42
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
192
49
e4828cb72ce2 working on json store
casties
parents: 48
diff changeset
193 return []
e4828cb72ce2 working on json store
casties
parents: 48
diff changeset
194
51
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
195 def showListOfItems(self,resultFormat,schema,table,tag,type):
52
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
196 """shows the list of existing items"""
54
54940a99f12d more work on json store
casties
parents: 52
diff changeset
197 recursive = self.REQUEST.get("recursive", False)
54940a99f12d more work on json store
casties
parents: 52
diff changeset
198 if recursive:
54940a99f12d more work on json store
casties
parents: 52
diff changeset
199 items = self.getListOfItemsAndValues(schema, table, tag, type)
54940a99f12d more work on json store
casties
parents: 52
diff changeset
200 # items contain JSON-strings
54940a99f12d more work on json store
casties
parents: 52
diff changeset
201 its = ",".join(['{"key":"%s","val":%s}'%(i[0],i[1]) for i in items])
70
9ec7e32e8ad3 working on maps
casties
parents: 54
diff changeset
202 its = "[%s]"%its
54
54940a99f12d more work on json store
casties
parents: 52
diff changeset
203 if resultFormat == 'JSON':
54940a99f12d more work on json store
casties
parents: 52
diff changeset
204 self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json")
70
9ec7e32e8ad3 working on maps
casties
parents: 54
diff changeset
205 # we assume utf-8
9ec7e32e8ad3 working on maps
casties
parents: 54
diff changeset
206 self.REQUEST.RESPONSE.write(utf8ify(its))
54
54940a99f12d more work on json store
casties
parents: 52
diff changeset
207 else:
70
9ec7e32e8ad3 working on maps
casties
parents: 54
diff changeset
208 # we assume utf-8
9ec7e32e8ad3 working on maps
casties
parents: 54
diff changeset
209 self.REQUEST.RESPONSE.write(utf8ify(its))
54
54940a99f12d more work on json store
casties
parents: 52
diff changeset
210
51
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
211 else:
54
54940a99f12d more work on json store
casties
parents: 52
diff changeset
212 items = self.getListOfItems(schema, table, tag, type)
54940a99f12d more work on json store
casties
parents: 52
diff changeset
213 if resultFormat == 'JSON':
54940a99f12d more work on json store
casties
parents: 52
diff changeset
214 self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json")
54940a99f12d more work on json store
casties
parents: 52
diff changeset
215 json.dump(items, self.REQUEST.RESPONSE)
54940a99f12d more work on json store
casties
parents: 52
diff changeset
216 else:
54940a99f12d more work on json store
casties
parents: 52
diff changeset
217 json.dump(items, self.REQUEST.RESPONSE)
54940a99f12d more work on json store
casties
parents: 52
diff changeset
218
51
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
219
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
220 def getListOfItems(self,schema,table,tag,type):
52
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
221 """returns the list of existing items"""
51
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
222 logging.debug("getlistofitems schema=%s table=%s tag=%s type=%s"%(schema,table,tag,type))
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
223 sql = 'select distinct json_item from "%s"."%s" where json_tag = %%s and json_type = %%s'%(schema,table)
54
54940a99f12d more work on json store
casties
parents: 52
diff changeset
224
51
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
225 res = self.executeSQL(sql,(tag,type))
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
226 if len(res['rows']) > 0:
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
227 items = [r[0] for r in res['rows']]
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
228 return items
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
229
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
230 return []
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
231
54
54940a99f12d more work on json store
casties
parents: 52
diff changeset
232 def getListOfItemsAndValues(self,schema,table,tag,type):
54940a99f12d more work on json store
casties
parents: 52
diff changeset
233 """returns the list of existing items and their values"""
54940a99f12d more work on json store
casties
parents: 52
diff changeset
234 logging.debug("getlistofitemsandvalues schema=%s table=%s tag=%s type=%s"%(schema,table,tag,type))
54940a99f12d more work on json store
casties
parents: 52
diff changeset
235 sql = 'select json_item, json_value from "%s"."%s" where json_tag = %%s and json_type = %%s'%(schema,table)
54940a99f12d more work on json store
casties
parents: 52
diff changeset
236
54940a99f12d more work on json store
casties
parents: 52
diff changeset
237 res = self.executeSQL(sql,(tag,type))
54940a99f12d more work on json store
casties
parents: 52
diff changeset
238 if len(res['rows']) > 0:
54940a99f12d more work on json store
casties
parents: 52
diff changeset
239 return res['rows']
54940a99f12d more work on json store
casties
parents: 52
diff changeset
240
54940a99f12d more work on json store
casties
parents: 52
diff changeset
241 return []
54940a99f12d more work on json store
casties
parents: 52
diff changeset
242
51
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
243 def showItem(self,resultFormat,schema,table,tag,type,item):
52
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
244 """shows the item"""
51
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
245 item = self.getItem(schema, table, tag, type, item)
52
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
246 # item is a string
51
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
247 if resultFormat == 'JSON':
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
248 self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json")
52
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
249 self.REQUEST.RESPONSE.write(item)
51
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
250 else:
52
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
251 self.REQUEST.RESPONSE.write(item)
51
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
252
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
253 def getItem(self,schema,table,tag,type,item):
52
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
254 """returns the item"""
51
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
255 logging.debug("getitem schema=%s table=%s tag=%s type=%s item=%s"%(schema,table,tag,type,item))
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
256 sql = 'select json_value from "%s"."%s" where json_tag = %%s and json_type = %%s and json_item = %%s'%(schema,table)
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
257 res = self.executeSQL(sql,(tag,type,item))
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
258 if len(res['rows']) > 0:
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
259 # just one item
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
260 item = res['rows'][0][0]
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
261 return item
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
262
52
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
263 return "{}"
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
264
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
265 def storeItem(self,schema,table,tag,type,item,value):
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
266 """sets the item to value"""
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
267 logging.debug("storeitem schema=%s table=%s tag=%s type=%s item=%s value=%s"%(schema,table,tag,type,item,value))
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
268 # see if the item exists
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
269 sql = 'select 1 from "%s"."%s" where json_tag = %%s and json_type = %%s and json_item = %%s'%(schema,table)
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
270 res = self.executeSQL(sql,(tag,type,item))
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
271 if len(res['rows']) > 0:
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
272 # then update
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
273 sql = 'update "%s"."%s" set json_value = %%s where json_tag = %%s and json_type = %%s and json_item = %%s'%(schema,table)
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
274
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
275 else:
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
276 # then insert
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
277 sql = 'insert into "%s"."%s" (json_value, json_tag, json_type, json_item) values (%%s,%%s,%%s,%%s)'%(schema,table)
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
278
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
279 self.executeSQL(sql,(value,tag,type,item), hasResult=False)
435d6664ed90 more json store -- stores now
casties
parents: 51
diff changeset
280 return "Ok"
51
17dcf148beb3 more JSON store
casties
parents: 50
diff changeset
281
50
29c822d15bc1 more JSON store
casties
parents: 49
diff changeset
282 manage_addRestDbJsonStoreForm=PageTemplateFile('zpt/addRestDbJsonStore',globals())
42
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
283
50
29c822d15bc1 more JSON store
casties
parents: 49
diff changeset
284 def manage_addRestDbJsonStore(self, id, title='', label='', description='',
42
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
285 createPublic=0,
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
286 createUserF=0,
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
287 REQUEST=None):
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
288 """Add a new object with id *id*."""
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
289
50
29c822d15bc1 more JSON store
casties
parents: 49
diff changeset
290 ob=RestDbJsonStore(str(id),title)
42
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
291 self._setObject(id, ob)
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
292
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
293 #checkPermission=getSecurityManager().checkPermission
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
294 REQUEST.RESPONSE.redirect('manage_main')
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
295
291aed5f0e0d new class for JSON store
casties
parents:
diff changeset
296