Mercurial > hg > ChinaGisRestApi
annotate RestDbInterface.py @ 218:1ac5bcf3dad2
implement Google Search
| author | fknauft |
|---|---|
| date | Tue, 29 Mar 2011 15:58:35 +0200 |
| parents | 893efd0ac54b |
| children | 11dea0923d2f |
| rev | line source |
|---|---|
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
1 ''' |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
2 Created on 19.5.2010 |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
3 |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
4 @author: casties |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
5 ''' |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
6 |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
7 from OFS.Folder import Folder |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
8 from Products.PageTemplates.PageTemplateFile import PageTemplateFile |
| 60 | 9 from AccessControl import getSecurityManager, Unauthorized |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
10 from Products.ZSQLExtend import ZSQLExtend |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
11 import logging |
| 15 | 12 import re |
| 18 | 13 import json |
| 19 | 14 import time |
| 70 | 15 import psycopg2 |
| 16 # make psycopg use unicode objects | |
| 17 import psycopg2.extensions | |
| 18 psycopg2.extensions.register_type(psycopg2.extensions.UNICODE) | |
| 19 psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY) | |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
20 |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
21 from zope.interface import implements |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
22 from zope.publisher.interfaces import IPublishTraverse |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
23 from ZPublisher.BaseRequest import DefaultPublishTraverse |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
24 |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
25 |
| 70 | 26 def unicodify(s,alternate='latin-1'): |
| 27 """decode str (utf-8 or latin-1 representation) into unicode object""" | |
| 28 if not s: | |
| 29 return u"" | |
| 30 if isinstance(s, str): | |
| 31 try: | |
| 32 return s.decode('utf-8') | |
| 33 except: | |
| 34 return s.decode(alternate) | |
| 35 else: | |
| 36 return s | |
| 37 | |
| 38 def utf8ify(s): | |
| 39 """encode unicode object or string into byte string in utf-8 representation. | |
| 40 assumes string objects to be utf-8""" | |
| 41 if not s: | |
| 42 return "" | |
| 43 if isinstance(s, str): | |
| 44 return s | |
| 45 else: | |
| 46 return s.encode('utf-8') | |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
47 |
| 16 | 48 def getTextFromNode(node): |
| 14 | 49 """get the cdata content of a XML node""" |
| 16 | 50 if node is None: |
| 14 | 51 return "" |
| 16 | 52 |
| 53 if isinstance(node, list): | |
| 54 nodelist = node | |
| 55 else: | |
| 56 nodelist=node.childNodes | |
| 57 | |
| 14 | 58 rc = "" |
| 59 for node in nodelist: | |
| 60 if node.nodeType == node.TEXT_NODE: | |
| 61 rc = rc + node.data | |
| 62 return rc | |
| 63 | |
| 16 | 64 def sqlName(s,lc=True): |
| 65 """returns restricted ASCII-only version of string""" | |
| 15 | 66 if s is None: |
| 67 return "" | |
| 68 | |
| 69 # all else -> "_" | |
| 16 | 70 s = re.sub(r'[^A-Za-z0-9_]','_',s) |
| 15 | 71 if lc: |
| 72 return s.lower() | |
| 73 | |
| 74 return s | |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
75 |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
76 |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
77 class RestDbInterface(Folder): |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
78 """Object for RESTful database queries |
| 7 | 79 path schema: /db/{schema}/{table}/ |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
80 omitting table gives a list of schemas |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
81 omitting table and schema gives a list of schemas |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
82 """ |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
83 implements(IPublishTraverse) |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
84 |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
85 meta_type="RESTdb" |
|
4
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
86 manage_options=Folder.manage_options+( |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
87 {'label':'Config','action':'manage_editRestDbInterfaceForm'}, |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
88 ) |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
89 |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
90 # management templates |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
91 manage_editRestDbInterfaceForm=PageTemplateFile('zpt/editRestDbInterface',globals()) |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
92 |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
93 # data templates |
| 8 | 94 XML_index = PageTemplateFile('zpt/XML_index', globals()) |
| 95 XML_schema = PageTemplateFile('zpt/XML_schema', globals()) | |
| 96 XML_schema_table = PageTemplateFile('zpt/XML_schema_table', globals()) | |
| 97 HTML_index = PageTemplateFile('zpt/HTML_index', globals()) | |
| 98 HTML_schema = PageTemplateFile('zpt/HTML_schema', globals()) | |
| 99 HTML_schema_table = PageTemplateFile('zpt/HTML_schema_table', globals()) | |
| 22 | 100 JSONHTML_index = PageTemplateFile('zpt/JSONHTML_index', globals()) |
| 101 JSONHTML_schema = PageTemplateFile('zpt/JSONHTML_schema', globals()) | |
| 102 JSONHTML_schema_table = PageTemplateFile('zpt/JSONHTML_schema_table', globals()) | |
| 24 | 103 # JSON_* templates are scripts |
| 45 | 104 def JSON_index(self): |
| 30 | 105 """JSON index function""" |
| 106 self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json") | |
| 45 | 107 json.dump(self.getListOfSchemas(), self.REQUEST.RESPONSE) |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
108 |
| 45 | 109 def JSON_schema(self,schema): |
| 30 | 110 """JSON index function""" |
| 111 self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json") | |
| 45 | 112 json.dump(self.getListOfTables(schema), self.REQUEST.RESPONSE) |
| 30 | 113 |
| 44 | 114 def JSON_schema_table(self,schema,table): |
| 30 | 115 """JSON index function""" |
| 116 self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json") | |
| 44 | 117 json.dump(self.getTable(schema, table), self.REQUEST.RESPONSE) |
| 30 | 118 |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
119 |
|
4
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
120 def __init__(self, id, title, connection_id=None): |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
121 """init""" |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
122 self.id = id |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
123 self.title = title |
|
4
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
124 # database connection id |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
125 self.connection_id = connection_id |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
126 # create template folder |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
127 self.manage_addFolder('template') |
| 44 | 128 |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
129 |
| 44 | 130 def getRestDbUrl(self): |
| 131 """returns url to the RestDb instance""" | |
| 132 return self.absolute_url() | |
| 133 | |
| 22 | 134 def getJsonString(self,object): |
| 135 """returns a JSON formatted string from object""" | |
| 136 return json.dumps(object) | |
|
4
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
137 |
| 17 | 138 def getCursor(self,autocommit=True): |
|
4
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
139 """returns fresh DB cursor""" |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
140 conn = getattr(self,"_v_database_connection",None) |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
141 if conn is None: |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
142 # create a new connection object |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
143 try: |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
144 if self.connection_id is None: |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
145 # try to take the first existing ID |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
146 connids = SQLConnectionIDs(self) |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
147 if len(connids) > 0: |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
148 connection_id = connids[0][0] |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
149 self.connection_id = connection_id |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
150 logging.debug("connection_id: %s"%repr(connection_id)) |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
151 |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
152 da = getattr(self, self.connection_id) |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
153 da.connect('') |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
154 # we copy the DAs database connection |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
155 conn = da._v_database_connection |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
156 #conn._register() # register with the Zope transaction system |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
157 self._v_database_connection = conn |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
158 except Exception, e: |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
159 raise IOError("No database connection! (%s)"%str(e)) |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
160 |
| 5 | 161 cursor = conn.getcursor() |
| 17 | 162 if autocommit: |
| 163 # is there a better version to get to the connection? | |
| 164 cursor.connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) | |
| 165 | |
|
4
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
166 return cursor |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
167 |
| 55 | 168 def getFieldNameMap(self,fields): |
| 169 """returns a dict mapping field names to row indexes""" | |
| 170 map = {} | |
| 171 i = 0 | |
| 172 for f in fields: | |
| 173 map[f[0]] = i | |
| 174 i += 1 | |
| 175 | |
| 176 return map | |
| 177 | |
| 17 | 178 def executeSQL(self, query, args=None, hasResult=True, autocommit=True): |
|
4
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
179 """execute query with args on database and return all results. |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
180 result format: {"fields":fields, "rows":data}""" |
| 16 | 181 logging.debug("executeSQL query=%s args=%s"%(query,args)) |
| 17 | 182 cur = self.getCursor(autocommit=autocommit) |
| 53 | 183 if args is not None: |
| 184 # make sure args is a list | |
| 185 if isinstance(args,basestring): | |
| 186 args = (args,) | |
| 187 | |
|
4
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
188 cur.execute(query, args) |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
189 # description of returned fields |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
190 fields = cur.description |
| 17 | 191 if hasResult: |
| 192 # get all data in an array | |
| 193 data = cur.fetchall() | |
| 194 cur.close() | |
| 55 | 195 #logging.debug("fields: %s"%repr(fields)) |
| 58 | 196 #logging.debug("rows: %s"%repr(data)) |
| 17 | 197 return {"fields":fields, "rows":data} |
| 198 else: | |
| 199 cur.close() | |
| 200 return None | |
| 16 | 201 |
| 60 | 202 def isAllowed(self,action,schema,table,user=None): |
| 203 """returns if the requested action on the table is allowed""" | |
| 204 if user is None: | |
| 205 user = self.REQUEST.get('AUTHENTICATED_USER',None) | |
| 206 logging.debug("isAllowed action=%s schema=%s table=%s user=%s"%(action,schema,table,user)) | |
| 207 # no default policy! | |
| 208 return True | |
| 209 | |
| 210 | |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
211 def publishTraverse(self,request,name): |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
212 """change the traversal""" |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
213 # get stored path |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
214 path = request.get('restdb_path', []) |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
215 logging.debug("publishtraverse: name=%s restdb_path=%s"%(name,path)) |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
216 |
| 13 | 217 if name in ("index_html", "PUT"): |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
218 # end of traversal |
| 13 | 219 if request.get("method") == "POST" and request.get("action",None) == "PUT": |
| 220 # fake PUT by POST with action=PUT | |
| 221 name = "PUT" | |
| 222 | |
| 223 return getattr(self, name) | |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
224 #TODO: should we check more? |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
225 else: |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
226 # traverse |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
227 if len(path) == 0: |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
228 # first segment |
| 8 | 229 if name == 'db': |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
230 # virtual path -- continue traversing |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
231 path = [name] |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
232 request['restdb_path'] = path |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
233 else: |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
234 # try real path |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
235 tr = DefaultPublishTraverse(self, request) |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
236 ob = tr.publishTraverse(request, name) |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
237 return ob |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
238 else: |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
239 path.append(name) |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
240 |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
241 # continue traversing |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
242 return self |
| 44 | 243 |
| 244 | |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
245 def index_html(self,REQUEST,RESPONSE): |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
246 """index method""" |
| 8 | 247 # ReST path was stored in request |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
248 path = REQUEST.get('restdb_path',[]) |
| 19 | 249 |
| 8 | 250 # type and format are real parameter |
| 26 | 251 resultFormat = REQUEST.get('format','HTML').upper() |
| 27 | 252 queryType = REQUEST.get('type',None) |
| 19 | 253 |
| 26 | 254 logging.debug("index_html path=%s resultFormat=%s queryType=%s"%(path,resultFormat,queryType)) |
| 8 | 255 |
| 26 | 256 if queryType is not None: |
| 257 # non-empty queryType -- look for template | |
| 258 pt = getattr(self.template, "%s_%s"%(resultFormat,queryType), None) | |
| 8 | 259 if pt is not None: |
| 26 | 260 return pt(format=resultFormat,type=queryType,path=path) |
| 8 | 261 |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
262 if len(path) == 1: |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
263 # list of schemas |
| 61 | 264 return self.showListOfSchemas(format=resultFormat) |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
265 elif len(path) == 2: |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
266 # list of tables |
| 61 | 267 return self.showListOfTables(format=resultFormat,schema=path[1]) |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
268 elif len(path) == 3: |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
269 # table |
| 25 | 270 if REQUEST.get("method") == "POST" and REQUEST.get("create_table_file",None) is not None: |
| 24 | 271 # POST to table to check |
| 61 | 272 return self.checkTable(format=resultFormat,schema=path[1],table=path[2]) |
| 24 | 273 # else show table |
| 61 | 274 return self.showTable(format=resultFormat,schema=path[1],table=path[2]) |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
275 |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
276 # don't know what to do |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
277 return str(REQUEST) |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
278 |
| 12 | 279 def PUT(self, REQUEST, RESPONSE): |
| 280 """ | |
| 281 Implement WebDAV/HTTP PUT/FTP put method for this object. | |
| 282 """ | |
| 13 | 283 logging.debug("RestDbInterface PUT") |
| 284 #logging.debug("req=%s"%REQUEST) | |
| 12 | 285 #self.dav__init(REQUEST, RESPONSE) |
| 286 #self.dav__simpleifhandler(REQUEST, RESPONSE) | |
| 13 | 287 # ReST path was stored in request |
| 288 path = REQUEST.get('restdb_path',[]) | |
| 289 if len(path) == 3: | |
| 290 schema = path[1] | |
| 291 tablename = path[2] | |
| 292 file = REQUEST.get("create_table_file",None) | |
| 293 if file is None: | |
| 294 RESPONSE.setStatus(400) | |
| 295 return | |
|
4
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
296 |
| 30 | 297 fields = None |
| 298 fieldsStr = REQUEST.get("create_table_fields",None) | |
| 299 logging.debug("put with schema=%s table=%s file=%s fields=%s"%(schema,tablename,file,repr(fieldsStr))) | |
| 300 if fieldsStr is not None: | |
| 301 # unpack fields | |
| 302 fields = [{"name":n, "type": t} for (n,t) in [f.split(":") for f in fieldsStr.split(",")]] | |
| 31 | 303 |
| 28 | 304 ret = self.createTableFromXML(schema, tablename, file, fields) |
| 26 | 305 # return the result as JSON |
| 306 format = REQUEST.get("format","JSON") | |
| 307 if format == "JSON": | |
| 20 | 308 RESPONSE.setHeader("Content-Type", "application/json") |
| 309 json.dump(ret, RESPONSE) | |
| 26 | 310 |
| 311 elif format == "JSONHTML": | |
| 20 | 312 RESPONSE.setHeader("Content-Type", "text/html") |
| 313 RESPONSE.write("<html>\n<body>\n<pre>") | |
| 314 json.dump(ret, RESPONSE) | |
| 315 RESPONSE.write("</pre>\n</body>\n</html>") | |
| 13 | 316 |
| 317 else: | |
| 318 # 400 Bad Request | |
| 319 RESPONSE.setStatus(400) | |
| 320 return | |
| 12 | 321 |
| 61 | 322 def showTable(self,format='XML',schema='public',table=None,REQUEST=None,RESPONSE=None): |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
323 """returns PageTemplate with tables""" |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
324 logging.debug("showtable") |
| 38 | 325 if REQUEST is None: |
| 326 REQUEST = self.REQUEST | |
| 327 | |
| 23 | 328 # should be cross-site accessible |
| 25 | 329 if RESPONSE is None: |
| 330 RESPONSE = self.REQUEST.RESPONSE | |
| 38 | 331 |
| 23 | 332 RESPONSE.setHeader('Access-Control-Allow-Origin', '*') |
| 38 | 333 |
| 22 | 334 # everything else has its own template |
| 9 | 335 pt = getattr(self.template, '%s_schema_table'%format, None) |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
336 if pt is None: |
| 9 | 337 return "ERROR!! template %s_schema_table not found"%format |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
338 |
| 44 | 339 #data = self.getTable(schema,table) |
| 340 return pt(schema=schema,table=table) | |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
341 |
| 58 | 342 def getTable(self,schema='public',table=None,sortBy=1,username='guest'): |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
343 """return table data""" |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
344 logging.debug("gettable") |
| 58 | 345 if sortBy: |
| 346 data = self.executeSQL('select * from "%s"."%s" order by %s'%(schema,table,sortBy)) | |
| 347 else: | |
| 348 data = self.executeSQL('select * from "%s"."%s"'%(schema,table)) | |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
349 return data |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
350 |
| 26 | 351 def hasTable(self,schema='public',table=None,username='guest'): |
| 352 """return if table exists""" | |
| 353 logging.debug("hastable") | |
| 354 data = self.executeSQL('select 1 from information_schema.tables where table_schema=%s and table_name=%s',(schema,table)) | |
| 355 ret = bool(data['rows']) | |
| 356 return ret | |
| 357 | |
| 61 | 358 def showListOfTables(self,format='XML',schema='public',REQUEST=None,RESPONSE=None): |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
359 """returns PageTemplate with list of tables""" |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
360 logging.debug("showlistoftables") |
| 23 | 361 # should be cross-site accessible |
| 25 | 362 if RESPONSE is None: |
| 363 RESPONSE = self.REQUEST.RESPONSE | |
| 23 | 364 RESPONSE.setHeader('Access-Control-Allow-Origin', '*') |
| 24 | 365 |
| 9 | 366 pt = getattr(self.template, '%s_schema'%format, None) |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
367 if pt is None: |
| 9 | 368 return "ERROR!! template %s_schema not found"%format |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
369 |
| 45 | 370 #data = self.getListOfTables(schema) |
| 371 return pt(schema=schema) | |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
372 |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
373 def getListOfTables(self,schema='public',username='guest'): |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
374 """return list of tables""" |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
375 logging.debug("getlistoftables") |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
376 # get list of fields and types of db table |
| 61 | 377 #qstr="""SELECT c.relname AS tablename FROM pg_catalog.pg_class c |
| 378 # LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace | |
| 379 # WHERE c.relkind IN ('r','') AND n.nspname NOT IN ('pg_catalog', 'pg_toast') | |
| 380 # AND pg_catalog.pg_table_is_visible(c.oid) | |
| 381 # AND c.relname ORDER BY 1""" | |
| 382 qstr = """SELECT table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE' | |
| 383 AND table_schema = %s ORDER BY 1""" | |
| 384 data=self.executeSQL(qstr,(schema,)) | |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
385 return data |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
386 |
| 61 | 387 def showListOfSchemas(self,format='XML',REQUEST=None,RESPONSE=None): |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
388 """returns PageTemplate with list of schemas""" |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
389 logging.debug("showlistofschemas") |
| 23 | 390 # should be cross-site accessible |
| 25 | 391 if RESPONSE is None: |
| 392 RESPONSE = self.REQUEST.RESPONSE | |
| 23 | 393 RESPONSE.setHeader('Access-Control-Allow-Origin', '*') |
| 24 | 394 |
| 9 | 395 pt = getattr(self.template, '%s_index'%format, None) |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
396 if pt is None: |
| 9 | 397 return "ERROR!! template %s_index not found"%format |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
398 |
| 45 | 399 #data = self.getListOfSchemas() |
| 400 return pt() | |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
401 |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
402 def getListOfSchemas(self,username='guest'): |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
403 """return list of schemas""" |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
404 logging.debug("getlistofschemas") |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
405 # TODO: really look up schemas |
| 9 | 406 data={'fields': (('schemas',),), 'rows': [('public',),]} |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
407 return data |
| 26 | 408 |
| 61 | 409 def checkTable(self,format,schema,table,REQUEST=None,RESPONSE=None): |
| 26 | 410 """check the table. |
| 411 returns valid data fields and table name.""" | |
| 412 if REQUEST is None: | |
| 413 REQUEST = self.REQUEST | |
| 414 RESPONSE = REQUEST.RESPONSE | |
| 74 | 415 |
| 26 | 416 file = REQUEST.get("create_table_file",None) |
| 417 res = self.checkTableFromXML(schema, table, file) | |
| 418 logging.debug("checkTable result=%s"%repr(res)) | |
| 419 # return the result as JSON | |
| 61 | 420 if format == "JSON": |
| 26 | 421 RESPONSE.setHeader("Content-Type", "application/json") |
| 422 json.dump(res, RESPONSE) | |
| 423 | |
| 61 | 424 elif format == "JSONHTML": |
| 26 | 425 RESPONSE.setHeader("Content-Type", "text/html") |
| 426 RESPONSE.write("<html>\n<body>\n<pre>") | |
| 427 json.dump(res, RESPONSE) | |
| 428 RESPONSE.write("</pre>\n</body>\n</html>") | |
| 429 | |
| 430 else: | |
| 61 | 431 return "ERROR: invalid format" |
| 74 | 432 |
| 26 | 433 def checkTableFromXML(self,schema,table,data,REQUEST=None,RESPONSE=None): |
| 434 """check the table with the given XML data. | |
| 435 returns valid data fields and table name.""" | |
| 436 logging.debug("checkTableFromXML schema=%s table=%s"%(schema,table)) | |
| 437 # clean table name | |
| 438 tablename = sqlName(table) | |
| 439 tableExists = self.hasTable(schema, table) | |
| 440 if data is None: | |
| 441 fieldNames = [] | |
| 442 else: | |
| 443 # get list of field names from upload file | |
| 444 fields = self.importExcelXML(schema,tablename,data,fieldsOnly=True) | |
| 14 | 445 |
| 26 | 446 res = {'tablename': tablename, 'table_exists': tableExists} |
| 447 res['fields'] = fields | |
| 448 return res | |
| 19 | 449 |
| 14 | 450 def createEmptyTable(self,schema,table,fields): |
| 451 """create a table with the given fields | |
| 452 returns list of created fields""" | |
| 15 | 453 logging.debug("createEmptyTable") |
| 19 | 454 |
| 14 | 455 sqlFields = [] |
| 456 for f in fields: | |
| 457 if isinstance(f,dict): | |
| 458 # {name: XX, type: YY} | |
| 459 name = sqlName(f['name']) | |
| 460 type = f['type'] | |
| 70 | 461 if hasattr(self, 'toSqlTypeMap'): |
| 462 sqltype = self.toSqlTypeMap[type] | |
| 463 else: | |
| 464 sqltype = 'text' | |
| 14 | 465 |
| 466 else: | |
| 467 # name only | |
| 468 name = sqlName(f) | |
| 469 type = 'text' | |
| 31 | 470 sqltype = 'text' |
| 14 | 471 |
| 31 | 472 sqlFields.append({'name':name, 'type':type, 'sqltype':sqltype}) |
| 14 | 473 |
| 60 | 474 if self.hasTable(schema,table): |
| 475 # TODO: find owner | |
| 476 if not self.isAllowed("update", schema, table): | |
| 477 raise Unauthorized | |
| 478 self.executeSQL('drop table "%s"."%s"'%(schema,table),hasResult=False) | |
| 479 else: | |
| 480 if not self.isAllowed("create", schema, table): | |
| 481 raise Unauthorized | |
| 482 | |
| 43 | 483 fieldString = ", ".join(['"%s" %s'%(f['name'],f['sqltype']) for f in sqlFields]) |
| 484 sqlString = 'create table "%s"."%s" (%s)'%(schema,table,fieldString) | |
| 485 logging.debug("createemptytable: SQL=%s"%sqlString) | |
| 486 self.executeSQL(sqlString,hasResult=False) | |
| 487 self.setTableMetaTypes(schema,table,sqlFields) | |
| 488 return sqlFields | |
| 19 | 489 |
| 28 | 490 def createTableFromXML(self,schema,table,data, fields=None): |
| 13 | 491 """create or replace a table with the given XML data""" |
| 28 | 492 logging.debug("createTableFromXML schema=%s table=%s data=%s fields=%s"%(schema,table,data,fields)) |
| 17 | 493 tablename = sqlName(table) |
| 28 | 494 self.importExcelXML(schema, tablename, data, fields) |
| 17 | 495 return {"tablename": tablename} |
| 76 | 496 |
| 28 | 497 def importExcelXML(self,schema,table,xmldata,fields=None,fieldsOnly=False): |
| 14 | 498 ''' |
| 499 Import XML file in Excel format into the table | |
| 500 @param table: name of the table the xml shall be imported into | |
| 501 ''' | |
| 502 from xml.dom.pulldom import parseString,parse | |
| 503 | |
| 60 | 504 if not (fieldsOnly or self.isAllowed("create", schema, table)): |
| 505 raise Unauthorized | |
| 506 | |
| 14 | 507 namespace = "urn:schemas-microsoft-com:office:spreadsheet" |
| 508 containerTagName = "Table" | |
| 509 rowTagName = "Row" | |
| 16 | 510 colTagName = "Cell" |
| 14 | 511 dataTagName = "Data" |
| 26 | 512 xmlFields = [] |
| 14 | 513 sqlFields = [] |
| 16 | 514 numFields = 0 |
| 14 | 515 sqlInsert = None |
| 516 | |
| 517 logging.debug("import excel xml") | |
| 76 | 518 |
| 14 | 519 ret="" |
| 16 | 520 if isinstance(xmldata, str): |
| 14 | 521 logging.debug("importXML reading string data") |
| 16 | 522 doc=parseString(xmldata) |
| 73 | 523 else: |
| 14 | 524 logging.debug("importXML reading file data") |
| 16 | 525 doc=parse(xmldata) |
| 14 | 526 |
| 527 cnt = 0 | |
| 528 while True: | |
| 529 node=doc.getEvent() | |
| 16 | 530 |
| 14 | 531 if node is None: |
| 532 break | |
| 533 | |
| 534 else: | |
| 16 | 535 #logging.debug("tag=%s"%node[1].localName) |
| 14 | 536 if node[1].localName is not None: |
| 537 tagName = node[1].localName.lower() | |
| 76 | 538 else: |
| 14 | 539 # ignore non-tag nodes |
| 540 continue | |
| 16 | 541 |
| 14 | 542 if tagName == rowTagName.lower(): |
| 543 # start of row | |
| 544 doc.expandNode(node[1]) | |
| 545 cnt += 1 | |
| 546 if cnt == 1: | |
| 547 # first row -- field names | |
| 548 names=node[1].getElementsByTagNameNS(namespace, dataTagName) | |
| 549 for name in names: | |
| 550 fn = getTextFromNode(name) | |
| 26 | 551 xmlFields.append({'name':sqlName(fn),'type':'text'}) |
| 14 | 552 |
| 26 | 553 if fieldsOnly: |
| 14 | 554 # return just field names |
| 26 | 555 return xmlFields |
| 14 | 556 |
| 557 # create table | |
| 28 | 558 if fields is None: |
| 31 | 559 fields = xmlFields |
| 560 | |
| 561 sqlFields = self.createEmptyTable(schema, table, fields) | |
| 16 | 562 numFields = len(sqlFields) |
| 15 | 563 fieldString = ", ".join(['"%s"'%f['name'] for f in sqlFields]) |
| 14 | 564 valString = ", ".join(["%s" for f in sqlFields]) |
| 16 | 565 sqlInsert = 'insert into "%s"."%s" (%s) values (%s)'%(schema,table,fieldString,valString) |
| 26 | 566 #logging.debug("importexcelsql: sqlInsert=%s"%sqlInsert) |
| 14 | 567 |
| 568 else: | |
| 569 # following rows are data | |
| 16 | 570 colNodes=node[1].getElementsByTagNameNS(namespace, colTagName) |
| 14 | 571 data = [] |
| 16 | 572 hasData = False |
| 573 for colNode in colNodes: | |
| 574 dataNodes=colNode.getElementsByTagNameNS(namespace, dataTagName) | |
| 575 if len(dataNodes) > 0: | |
| 576 val = getTextFromNode(dataNodes[0]) | |
| 577 hasData = True | |
| 578 else: | |
| 579 val = None | |
| 19 | 580 |
| 14 | 581 data.append(val) |
| 582 | |
| 16 | 583 if not hasData: |
| 584 # ignore empty rows | |
| 585 continue | |
| 586 | |
| 587 # fix number of data fields | |
| 588 if len(data) > numFields: | |
| 589 del data[numFields:] | |
| 590 elif len(data) < numFields: | |
| 591 missFields = numFields - len(data) | |
| 592 data.extend(missFields * [None,]) | |
| 593 | |
| 594 logging.debug("importexcel sqlinsert=%s data=%s"%(sqlInsert,data)) | |
| 17 | 595 self.executeSQL(sqlInsert, data, hasResult=False) |
| 14 | 596 |
| 16 | 597 return cnt |
| 38 | 598 |
|
4
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
599 def manage_editRestDbInterface(self, title=None, connection_id=None, |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
600 REQUEST=None): |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
601 """Change the object""" |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
602 if title is not None: |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
603 self.title = title |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
604 |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
605 if connection_id is not None: |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
606 self.connection_id = connection_id |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
607 |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
608 #checkPermission=getSecurityManager().checkPermission |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
609 REQUEST.RESPONSE.redirect('manage_main') |
|
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
610 |
|
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
611 |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
612 manage_addRestDbInterfaceForm=PageTemplateFile('zpt/addRestDbInterface',globals()) |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
613 |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
614 def manage_addRestDbInterface(self, id, title='', label='', description='', |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
615 createPublic=0, |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
616 createUserF=0, |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
617 REQUEST=None): |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
618 """Add a new object with id *id*.""" |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
619 |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
620 ob=RestDbInterface(str(id),title) |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
621 self._setObject(id, ob) |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
622 |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
623 #checkPermission=getSecurityManager().checkPermission |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
624 REQUEST.RESPONSE.redirect('manage_main') |
|
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
625 |
| 5 | 626 |
