comparison RestDbJsonStore.py @ 52:435d6664ed90

more json store -- stores now
author casties
date Thu, 09 Sep 2010 20:39:03 +0200
parents 17dcf148beb3
children 54940a99f12d
comparison
equal deleted inserted replaced
51:17dcf148beb3 52:435d6664ed90
124 """ 124 """
125 Implement WebDAV/HTTP PUT/FTP put method for this object. 125 Implement WebDAV/HTTP PUT/FTP put method for this object.
126 """ 126 """
127 path = REQUEST.get('restdb_path',[]) 127 path = REQUEST.get('restdb_path',[])
128 logging.debug("RestDbInterface PUT (path=%s)"%repr(path)) 128 logging.debug("RestDbInterface PUT (path=%s)"%repr(path))
129 logging.debug("PUT request=%s"%REQUEST)
129 #logging.debug("req=%s"%REQUEST) 130 #logging.debug("req=%s"%REQUEST)
130 #self.dav__init(REQUEST, RESPONSE) 131 #self.dav__init(REQUEST, RESPONSE)
131 #self.dav__simpleifhandler(REQUEST, RESPONSE) 132 #self.dav__simpleifhandler(REQUEST, RESPONSE)
132 # ReST path was stored in request 133 # ReST path was stored in request
133 if len(path) == 3: 134 if len(path) == 6:
134 schema = path[1] 135 schema = path[1]
135 tablename = path[2] 136 table = path[2]
136 file = REQUEST.get("create_table_file",None) 137 tag = path[3]
137 if file is None: 138 type = path[4]
138 RESPONSE.setStatus(400) 139 item = path[5]
139 return 140 # maybe the data was form-encoded
140 141 value = REQUEST.get('json_string', None)
141 fields = None 142 if value is None:
142 fieldsStr = REQUEST.get("create_table_fields",None) 143 # then maybe in the body (the hard way...)
143 logging.debug("put with schema=%s table=%s file=%s fields=%s"%(schema,tablename,file,repr(fieldsStr))) 144 REQUEST.stdin.seek(0)
144 if fieldsStr is not None: 145 value = REQUEST.stdin.read()
145 # unpack fields 146
146 fields = [{"name":n, "type": t} for (n,t) in [f.split(":") for f in fieldsStr.split(",")]] 147 logging.debug("put with schema=%s table=%s tag=%s type=%s item=%s value=%s"%(schema,table,tag,type,item,value))
147 148 res = self.storeItem(schema,table,tag,type,item,value)
148 ret = self.createTableFromXML(schema, tablename, file, fields)
149 # return the result as JSON
150 format = REQUEST.get("format","JSON")
151 if format == "JSON":
152 RESPONSE.setHeader("Content-Type", "application/json")
153 json.dump(ret, RESPONSE)
154
155 elif format == "JSONHTML":
156 RESPONSE.setHeader("Content-Type", "text/html")
157 RESPONSE.write("<html>\n<body>\n<pre>")
158 json.dump(ret, RESPONSE)
159 RESPONSE.write("</pre>\n</body>\n</html>")
160 149
161 else: 150 else:
162 # 400 Bad Request 151 # 400 Bad Request
163 RESPONSE.setStatus(400) 152 RESPONSE.setStatus(400)
164 return 153 return
165 154
166 def showListOfTags(self,resultFormat,schema,table): 155 def showListOfTags(self,resultFormat,schema,table):
167 """returns the list of existing tags""" 156 """shows the list of existing tags"""
168 tags = self.getListOfTags(schema, table) 157 tags = self.getListOfTags(schema, table)
169 if resultFormat == 'JSON': 158 if resultFormat == 'JSON':
170 self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json") 159 self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json")
171 json.dump(tags, self.REQUEST.RESPONSE) 160 json.dump(tags, self.REQUEST.RESPONSE)
172 else: 161 else:
182 return tags 171 return tags
183 172
184 return [] 173 return []
185 174
186 def showListOfTypes(self,resultFormat,schema,table,tag): 175 def showListOfTypes(self,resultFormat,schema,table,tag):
176 """shows the list of existing types"""
177 types = self.getListOfTypes(schema, table,tag)
178 if resultFormat == 'JSON':
179 self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json")
180 json.dump(types, self.REQUEST.RESPONSE)
181 else:
182 json.dump(types, self.REQUEST.RESPONSE)
183
184 def getListOfTypes(self,schema,table,tag):
187 """returns the list of existing types""" 185 """returns the list of existing types"""
188 types = self.getListOfTypes(schema, table,tag)
189 if resultFormat == 'JSON':
190 self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json")
191 json.dump(types, self.REQUEST.RESPONSE)
192 else:
193 json.dump(types, self.REQUEST.RESPONSE)
194
195 def getListOfTypes(self,schema,table,tag):
196 """returns the list of existing tags"""
197 logging.debug("getlistoftypes schema=%s table=%s tag=%s"%(schema,table,tag)) 186 logging.debug("getlistoftypes schema=%s table=%s tag=%s"%(schema,table,tag))
198 sql = 'select distinct json_type from "%s"."%s" where json_tag = %%s'%(schema,table) 187 sql = 'select distinct json_type from "%s"."%s" where json_tag = %%s'%(schema,table)
199 res = self.executeSQL(sql,(tag,)) 188 res = self.executeSQL(sql,(tag,))
200 if len(res['rows']) > 0: 189 if len(res['rows']) > 0:
201 tags = [r[0] for r in res['rows']] 190 tags = [r[0] for r in res['rows']]
202 return tags 191 return tags
203 192
204 return [] 193 return []
205 194
206 def showListOfItems(self,resultFormat,schema,table,tag,type): 195 def showListOfItems(self,resultFormat,schema,table,tag,type):
196 """shows the list of existing items"""
197 items = self.getListOfItems(schema, table, tag, type)
198 if resultFormat == 'JSON':
199 self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json")
200 json.dump(items, self.REQUEST.RESPONSE)
201 else:
202 json.dump(items, self.REQUEST.RESPONSE)
203
204 def getListOfItems(self,schema,table,tag,type):
207 """returns the list of existing items""" 205 """returns the list of existing items"""
208 items = self.getListOfItems(schema, table, tag, type)
209 if resultFormat == 'JSON':
210 self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json")
211 json.dump(items, self.REQUEST.RESPONSE)
212 else:
213 json.dump(items, self.REQUEST.RESPONSE)
214
215 def getListOfItems(self,schema,table,tag,type):
216 """returns the list of existing tags"""
217 logging.debug("getlistofitems schema=%s table=%s tag=%s type=%s"%(schema,table,tag,type)) 206 logging.debug("getlistofitems schema=%s table=%s tag=%s type=%s"%(schema,table,tag,type))
218 sql = 'select distinct json_item from "%s"."%s" where json_tag = %%s and json_type = %%s'%(schema,table) 207 sql = 'select distinct json_item from "%s"."%s" where json_tag = %%s and json_type = %%s'%(schema,table)
219 res = self.executeSQL(sql,(tag,type)) 208 res = self.executeSQL(sql,(tag,type))
220 if len(res['rows']) > 0: 209 if len(res['rows']) > 0:
221 items = [r[0] for r in res['rows']] 210 items = [r[0] for r in res['rows']]
222 return items 211 return items
223 212
224 return [] 213 return []
225 214
226 def showItem(self,resultFormat,schema,table,tag,type,item): 215 def showItem(self,resultFormat,schema,table,tag,type,item):
216 """shows the item"""
217 item = self.getItem(schema, table, tag, type, item)
218 # item is a string
219 if resultFormat == 'JSON':
220 self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json")
221 self.REQUEST.RESPONSE.write(item)
222 else:
223 self.REQUEST.RESPONSE.write(item)
224
225 def getItem(self,schema,table,tag,type,item):
227 """returns the item""" 226 """returns the item"""
228 item = self.getItem(schema, table, tag, type, item)
229 if resultFormat == 'JSON':
230 self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json")
231 json.dump(item, self.REQUEST.RESPONSE)
232 else:
233 json.dump(item, self.REQUEST.RESPONSE)
234
235 def getItem(self,schema,table,tag,type,item):
236 """returns the list of existing tags"""
237 logging.debug("getitem schema=%s table=%s tag=%s type=%s item=%s"%(schema,table,tag,type,item)) 227 logging.debug("getitem schema=%s table=%s tag=%s type=%s item=%s"%(schema,table,tag,type,item))
238 sql = 'select json_value from "%s"."%s" where json_tag = %%s and json_type = %%s and json_item = %%s'%(schema,table) 228 sql = 'select json_value from "%s"."%s" where json_tag = %%s and json_type = %%s and json_item = %%s'%(schema,table)
239 res = self.executeSQL(sql,(tag,type,item)) 229 res = self.executeSQL(sql,(tag,type,item))
240 if len(res['rows']) > 0: 230 if len(res['rows']) > 0:
241 # just one item 231 # just one item
242 item = res['rows'][0][0] 232 item = res['rows'][0][0]
243 return item 233 return item
244 234
245 return {} 235 return "{}"
236
237 def storeItem(self,schema,table,tag,type,item,value):
238 """sets the item to value"""
239 logging.debug("storeitem schema=%s table=%s tag=%s type=%s item=%s value=%s"%(schema,table,tag,type,item,value))
240 # see if the item exists
241 sql = 'select 1 from "%s"."%s" where json_tag = %%s and json_type = %%s and json_item = %%s'%(schema,table)
242 res = self.executeSQL(sql,(tag,type,item))
243 if len(res['rows']) > 0:
244 # then update
245 sql = 'update "%s"."%s" set json_value = %%s where json_tag = %%s and json_type = %%s and json_item = %%s'%(schema,table)
246
247 else:
248 # then insert
249 sql = 'insert into "%s"."%s" (json_value, json_tag, json_type, json_item) values (%%s,%%s,%%s,%%s)'%(schema,table)
250
251 self.executeSQL(sql,(value,tag,type,item), hasResult=False)
252 return "Ok"
246 253
247 manage_addRestDbJsonStoreForm=PageTemplateFile('zpt/addRestDbJsonStore',globals()) 254 manage_addRestDbJsonStoreForm=PageTemplateFile('zpt/addRestDbJsonStore',globals())
248 255
249 def manage_addRestDbJsonStore(self, id, title='', label='', description='', 256 def manage_addRestDbJsonStore(self, id, title='', label='', description='',
250 createPublic=0, 257 createPublic=0,