Mercurial > hg > ChinaGisRestApi
annotate RestDbInterface.py @ 28:9e4f9cfd1edc
start adding field structure to xml upload
author | casties |
---|---|
date | Fri, 27 Aug 2010 19:19:38 +0200 |
parents | a2e4ca3f1cff |
children | 0b9f8cca6744 |
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 |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
9 from Products.ZSQLExtend import ZSQLExtend |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
10 import logging |
15 | 11 import re |
16 | 12 import psycopg2 |
18 | 13 import json |
19 | 14 import time |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
15 |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
16 from zope.interface import implements |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
17 from zope.publisher.interfaces import IPublishTraverse |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
18 from ZPublisher.BaseRequest import DefaultPublishTraverse |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
19 #from zope.publisher.interfaces import NotFound |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
20 #from zope.app import zapi |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
21 #from zope.component import queryMultiAdapter |
4
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
22 import Shared.DC.ZRDB.DA |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
23 from Products.ZSQLMethods.SQL import SQLConnectionIDs |
2
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 |
16 | 26 def getTextFromNode(node): |
14 | 27 """get the cdata content of a XML node""" |
16 | 28 if node is None: |
14 | 29 return "" |
16 | 30 |
31 if isinstance(node, list): | |
32 nodelist = node | |
33 else: | |
34 nodelist=node.childNodes | |
35 | |
14 | 36 rc = "" |
37 for node in nodelist: | |
38 if node.nodeType == node.TEXT_NODE: | |
39 rc = rc + node.data | |
40 return rc | |
41 | |
16 | 42 def sqlName(s,lc=True): |
43 """returns restricted ASCII-only version of string""" | |
15 | 44 if s is None: |
45 return "" | |
46 | |
47 # all else -> "_" | |
16 | 48 s = re.sub(r'[^A-Za-z0-9_]','_',s) |
15 | 49 if lc: |
50 return s.lower() | |
51 | |
52 return s | |
14 | 53 |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
54 class RestDbInterface(Folder): |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
55 """Object for RESTful database queries |
7 | 56 path schema: /db/{schema}/{table}/ |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
57 omitting table gives a list of schemas |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
58 omitting table and schema gives a list of schemas |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
59 """ |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
60 implements(IPublishTraverse) |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
61 |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
62 meta_type="RESTdb" |
4
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
63 manage_options=Folder.manage_options+( |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
64 {'label':'Config','action':'manage_editRestDbInterfaceForm'}, |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
65 ) |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
66 |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
67 # management templates |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
68 manage_editRestDbInterfaceForm=PageTemplateFile('zpt/editRestDbInterface',globals()) |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
69 |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
70 # data templates |
8 | 71 XML_index = PageTemplateFile('zpt/XML_index', globals()) |
72 XML_schema = PageTemplateFile('zpt/XML_schema', globals()) | |
73 XML_schema_table = PageTemplateFile('zpt/XML_schema_table', globals()) | |
74 HTML_index = PageTemplateFile('zpt/HTML_index', globals()) | |
75 HTML_schema = PageTemplateFile('zpt/HTML_schema', globals()) | |
76 HTML_schema_table = PageTemplateFile('zpt/HTML_schema_table', globals()) | |
22 | 77 JSONHTML_index = PageTemplateFile('zpt/JSONHTML_index', globals()) |
78 JSONHTML_schema = PageTemplateFile('zpt/JSONHTML_schema', globals()) | |
79 JSONHTML_schema_table = PageTemplateFile('zpt/JSONHTML_schema_table', globals()) | |
24 | 80 # JSON_* templates are scripts |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
81 |
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 |
4
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
84 def __init__(self, id, title, connection_id=None): |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
85 """init""" |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
86 self.id = id |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
87 self.title = title |
4
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
88 # database connection id |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
89 self.connection_id = connection_id |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
90 # create template folder |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
91 self.manage_addFolder('template') |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
92 |
4
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
93 |
22 | 94 def getJsonString(self,object): |
95 """returns a JSON formatted string from object""" | |
96 return json.dumps(object) | |
97 | |
17 | 98 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
|
99 """returns fresh DB cursor""" |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
100 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
|
101 if conn is None: |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
102 # create a new connection object |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
103 try: |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
104 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
|
105 # 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
|
106 connids = SQLConnectionIDs(self) |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
107 if len(connids) > 0: |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
108 connection_id = connids[0][0] |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
109 self.connection_id = connection_id |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
110 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
|
111 |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
112 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
|
113 da.connect('') |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
114 # 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
|
115 conn = da._v_database_connection |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
116 #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
|
117 self._v_database_connection = conn |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
118 except Exception, e: |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
119 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
|
120 |
5 | 121 cursor = conn.getcursor() |
17 | 122 if autocommit: |
123 # is there a better version to get to the connection? | |
124 cursor.connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) | |
125 | |
4
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
126 return cursor |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
127 |
17 | 128 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
|
129 """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
|
130 result format: {"fields":fields, "rows":data}""" |
16 | 131 logging.debug("executeSQL query=%s args=%s"%(query,args)) |
17 | 132 cur = self.getCursor(autocommit=autocommit) |
4
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
133 cur.execute(query, args) |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
134 # description of returned fields |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
135 fields = cur.description |
17 | 136 if hasResult: |
137 # get all data in an array | |
138 data = cur.fetchall() | |
139 cur.close() | |
140 return {"fields":fields, "rows":data} | |
141 else: | |
142 cur.close() | |
143 return None | |
16 | 144 |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
145 |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
146 def publishTraverse(self,request,name): |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
147 """change the traversal""" |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
148 # get stored path |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
149 path = request.get('restdb_path', []) |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
150 logging.debug("publishtraverse: name=%s restdb_path=%s"%(name,path)) |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
151 |
13 | 152 if name in ("index_html", "PUT"): |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
153 # end of traversal |
13 | 154 if request.get("method") == "POST" and request.get("action",None) == "PUT": |
155 # fake PUT by POST with action=PUT | |
156 name = "PUT" | |
157 | |
158 return getattr(self, name) | |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
159 #TODO: should we check more? |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
160 else: |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
161 # traverse |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
162 if len(path) == 0: |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
163 # first segment |
8 | 164 if name == 'db': |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
165 # virtual path -- continue traversing |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
166 path = [name] |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
167 request['restdb_path'] = path |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
168 else: |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
169 # try real path |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
170 tr = DefaultPublishTraverse(self, request) |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
171 ob = tr.publishTraverse(request, name) |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
172 return ob |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
173 else: |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
174 path.append(name) |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
175 |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
176 # continue traversing |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
177 return self |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
178 |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
179 def index_html(self,REQUEST,RESPONSE): |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
180 """index method""" |
8 | 181 # ReST path was stored in request |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
182 path = REQUEST.get('restdb_path',[]) |
19 | 183 |
27 | 184 # type and format are real parameter |
26 | 185 resultFormat = REQUEST.get('format','HTML').upper() |
27 | 186 queryType = REQUEST.get('type',None) |
19 | 187 |
26 | 188 logging.debug("index_html path=%s resultFormat=%s queryType=%s"%(path,resultFormat,queryType)) |
8 | 189 |
26 | 190 if queryType is not None: |
191 # non-empty queryType -- look for template | |
192 pt = getattr(self.template, "%s_%s"%(resultFormat,queryType), None) | |
8 | 193 if pt is not None: |
26 | 194 return pt(format=resultFormat,type=queryType,path=path) |
8 | 195 |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
196 if len(path) == 1: |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
197 # list of schemas |
26 | 198 return self.showListOfSchemas(resultFormat=resultFormat) |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
199 elif len(path) == 2: |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
200 # list of tables |
26 | 201 return self.showListOfTables(resultFormat=resultFormat,schema=path[1]) |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
202 elif len(path) == 3: |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
203 # table |
25 | 204 if REQUEST.get("method") == "POST" and REQUEST.get("create_table_file",None) is not None: |
24 | 205 # POST to table to check |
26 | 206 return self.checkTable(resultFormat=resultFormat,schema=path[1],table=path[2]) |
24 | 207 # else show table |
26 | 208 return self.showTable(resultFormat=resultFormat,schema=path[1],table=path[2]) |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
209 |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
210 # don't know what to do |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
211 return str(REQUEST) |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
212 |
12 | 213 def PUT(self, REQUEST, RESPONSE): |
214 """ | |
215 Implement WebDAV/HTTP PUT/FTP put method for this object. | |
216 """ | |
13 | 217 logging.debug("RestDbInterface PUT") |
218 #logging.debug("req=%s"%REQUEST) | |
12 | 219 #self.dav__init(REQUEST, RESPONSE) |
220 #self.dav__simpleifhandler(REQUEST, RESPONSE) | |
13 | 221 # ReST path was stored in request |
222 path = REQUEST.get('restdb_path',[]) | |
223 if len(path) == 3: | |
224 schema = path[1] | |
225 tablename = path[2] | |
226 file = REQUEST.get("create_table_file",None) | |
227 if file is None: | |
228 RESPONSE.setStatus(400) | |
229 return | |
230 | |
28 | 231 fields = REQUEST.get("fields",None) |
232 logging.debug("put with schema=%s table=%s file=%s fields=%s"%(schema,tablename,file,repr(fields))) | |
233 ret = self.createTableFromXML(schema, tablename, file, fields) | |
26 | 234 # return the result as JSON |
235 format = REQUEST.get("format","JSON") | |
236 if format == "JSON": | |
20 | 237 RESPONSE.setHeader("Content-Type", "application/json") |
238 json.dump(ret, RESPONSE) | |
26 | 239 |
240 elif format == "JSONHTML": | |
20 | 241 RESPONSE.setHeader("Content-Type", "text/html") |
242 RESPONSE.write("<html>\n<body>\n<pre>") | |
243 json.dump(ret, RESPONSE) | |
244 RESPONSE.write("</pre>\n</body>\n</html>") | |
13 | 245 |
246 else: | |
247 # 400 Bad Request | |
248 RESPONSE.setStatus(400) | |
249 return | |
12 | 250 |
26 | 251 def showTable(self,resultFormat='XML',schema='public',table=None,REQUEST=None,RESPONSE=None): |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
252 """returns PageTemplate with tables""" |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
253 logging.debug("showtable") |
23 | 254 # should be cross-site accessible |
25 | 255 if RESPONSE is None: |
256 RESPONSE = self.REQUEST.RESPONSE | |
23 | 257 RESPONSE.setHeader('Access-Control-Allow-Origin', '*') |
22 | 258 # GIS gets special treatment |
26 | 259 if resultFormat=="GIS": |
23 | 260 id = REQUEST.get('id',[]) |
261 doc = REQUEST.get('doc',None) | |
22 | 262 return self.showGoogleMap(schema=path[1],table=path[2],id=id,doc=doc) |
263 | |
264 # everything else has its own template | |
26 | 265 pt = getattr(self.template, '%s_schema_table'%resultFormat, None) |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
266 if pt is None: |
26 | 267 return "ERROR!! template %s_schema_table not found"%resultFormat |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
268 |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
269 data = self.getTable(schema,table) |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
270 return pt(data=data,tablename=table) |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
271 |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
272 def getTable(self,schema='public',table=None,username='guest'): |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
273 """return table data""" |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
274 logging.debug("gettable") |
16 | 275 data = self.executeSQL('select * from "%s"."%s"'%(schema,table)) |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
276 return data |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
277 |
26 | 278 def hasTable(self,schema='public',table=None,username='guest'): |
279 """return if table exists""" | |
280 logging.debug("hastable") | |
281 data = self.executeSQL('select 1 from information_schema.tables where table_schema=%s and table_name=%s',(schema,table)) | |
282 ret = bool(data['rows']) | |
283 return ret | |
284 | |
285 def showListOfTables(self,resultFormat='XML',schema='public',REQUEST=None,RESPONSE=None): | |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
286 """returns PageTemplate with list of tables""" |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
287 logging.debug("showlistoftables") |
23 | 288 # should be cross-site accessible |
25 | 289 if RESPONSE is None: |
290 RESPONSE = self.REQUEST.RESPONSE | |
23 | 291 RESPONSE.setHeader('Access-Control-Allow-Origin', '*') |
24 | 292 |
26 | 293 pt = getattr(self.template, '%s_schema'%resultFormat, None) |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
294 if pt is None: |
26 | 295 return "ERROR!! template %s_schema not found"%resultFormat |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
296 |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
297 data = self.getListOfTables(schema) |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
298 return pt(data=data,schema=schema) |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
299 |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
300 def getListOfTables(self,schema='public',username='guest'): |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
301 """return list of tables""" |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
302 logging.debug("getlistoftables") |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
303 # get list of fields and types of db table |
22 | 304 qstr="""SELECT c.relname AS tablename FROM pg_catalog.pg_class c |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
305 LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
306 WHERE c.relkind IN ('r','') AND n.nspname NOT IN ('pg_catalog', 'pg_toast') |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
307 AND pg_catalog.pg_table_is_visible(c.oid)""" |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
308 #qstr="select attname, format_type(pg_attribute.atttypid, pg_attribute.atttypmod) from pg_attribute, pg_class where attrelid = pg_class.oid and pg_attribute.attnum > 0" |
4
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
309 data=self.executeSQL(qstr) |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
310 return data |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
311 |
26 | 312 def showListOfSchemas(self,resultFormat='XML',REQUEST=None,RESPONSE=None): |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
313 """returns PageTemplate with list of schemas""" |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
314 logging.debug("showlistofschemas") |
23 | 315 # should be cross-site accessible |
25 | 316 if RESPONSE is None: |
317 RESPONSE = self.REQUEST.RESPONSE | |
23 | 318 RESPONSE.setHeader('Access-Control-Allow-Origin', '*') |
24 | 319 |
26 | 320 pt = getattr(self.template, '%s_index'%resultFormat, None) |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
321 if pt is None: |
26 | 322 return "ERROR!! template %s_index not found"%resultFormat |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
323 |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
324 data = self.getListOfSchemas() |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
325 return pt(data=data) |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
326 |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
327 def getListOfSchemas(self,username='guest'): |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
328 """return list of schemas""" |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
329 logging.debug("getlistofschemas") |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
330 # TODO: really look up schemas |
9 | 331 data={'fields': (('schemas',),), 'rows': [('public',),]} |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
332 return data |
14 | 333 |
24 | 334 def JSON_index(self,data): |
335 """JSON index function""" | |
336 self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json") | |
337 json.dump(data, self.REQUEST.RESPONSE) | |
338 | |
339 def JSON_schema(self,data,schema): | |
340 """JSON index function""" | |
341 self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json") | |
342 json.dump(data, self.REQUEST.RESPONSE) | |
343 | |
344 def JSON_schema_table(self,data,tablename): | |
345 """JSON index function""" | |
346 self.REQUEST.RESPONSE.setHeader("Content-Type", "application/json") | |
347 json.dump(data, self.REQUEST.RESPONSE) | |
348 | |
26 | 349 def checkTable(self,resultFormat,schema,table,REQUEST=None,RESPONSE=None): |
350 """check the table. | |
351 returns valid data fields and table name.""" | |
352 if REQUEST is None: | |
353 REQUEST = self.REQUEST | |
354 RESPONSE = REQUEST.RESPONSE | |
355 | |
356 file = REQUEST.get("create_table_file",None) | |
357 res = self.checkTableFromXML(schema, table, file) | |
358 logging.debug("checkTable result=%s"%repr(res)) | |
359 # return the result as JSON | |
360 if resultFormat == "JSON": | |
361 RESPONSE.setHeader("Content-Type", "application/json") | |
362 json.dump(res, RESPONSE) | |
363 | |
364 elif resultFormat == "JSONHTML": | |
365 RESPONSE.setHeader("Content-Type", "text/html") | |
366 RESPONSE.write("<html>\n<body>\n<pre>") | |
367 json.dump(res, RESPONSE) | |
368 RESPONSE.write("</pre>\n</body>\n</html>") | |
369 | |
370 else: | |
371 return "ERROR: invalid resultFormat" | |
372 | |
373 def checkTableFromXML(self,schema,table,data,REQUEST=None,RESPONSE=None): | |
374 """check the table with the given XML data. | |
375 returns valid data fields and table name.""" | |
376 logging.debug("checkTableFromXML schema=%s table=%s"%(schema,table)) | |
377 # clean table name | |
378 tablename = sqlName(table) | |
379 tableExists = self.hasTable(schema, table) | |
380 if data is None: | |
381 fieldNames = [] | |
382 else: | |
383 # get list of field names from upload file | |
384 fields = self.importExcelXML(schema,tablename,data,fieldsOnly=True) | |
385 | |
386 res = {'tablename': tablename, 'table_exists': tableExists} | |
387 res['fields'] = fields | |
388 return res | |
389 | |
14 | 390 def createEmptyTable(self,schema,table,fields): |
391 """create a table with the given fields | |
392 returns list of created fields""" | |
15 | 393 logging.debug("createEmptyTable") |
14 | 394 sqlFields = [] |
395 for f in fields: | |
396 if isinstance(f,dict): | |
397 # {name: XX, type: YY} | |
398 name = sqlName(f['name']) | |
399 type = f['type'] | |
400 | |
401 else: | |
402 # name only | |
403 name = sqlName(f) | |
404 type = 'text' | |
405 | |
406 sqlFields.append({'name':name, 'type':type}) | |
407 | |
408 # drop table if it exists | |
409 try: | |
16 | 410 res = self.executeSQL('select * from "%s"."%s" where 1=0'%(schema,table)) |
411 logging.debug("createemptytable: table %s.%s exists"%(schema,table)) | |
17 | 412 self.executeSQL('drop table "%s"."%s"'%(schema,table),hasResult=False) |
14 | 413 except: |
414 pass | |
415 | |
15 | 416 fieldString = ", ".join(['"%s" %s'%(f['name'],f['type']) for f in sqlFields]) |
16 | 417 sqlString = 'create table "%s"."%s" (%s)'%(schema,table,fieldString) |
418 logging.debug("createemptytable: SQL=%s"%sqlString) | |
26 | 419 self.executeSQL(sqlString,hasResult=False) |
14 | 420 return sqlFields |
26 | 421 |
28 | 422 def createTableFromXML(self,schema,table,data, fields=None): |
13 | 423 """create or replace a table with the given XML data""" |
28 | 424 logging.debug("createTableFromXML schema=%s table=%s data=%s fields=%s"%(schema,table,data,fields)) |
17 | 425 tablename = sqlName(table) |
28 | 426 self.importExcelXML(schema, tablename, data, fields) |
17 | 427 return {"tablename": tablename} |
14 | 428 |
28 | 429 def importExcelXML(self,schema,table,xmldata,fields=None,fieldsOnly=False): |
14 | 430 ''' |
431 Import XML file in Excel format into the table | |
432 @param table: name of the table the xml shall be imported into | |
433 ''' | |
434 from xml.dom.pulldom import parseString,parse | |
435 | |
436 namespace = "urn:schemas-microsoft-com:office:spreadsheet" | |
437 containerTagName = "Table" | |
438 rowTagName = "Row" | |
16 | 439 colTagName = "Cell" |
14 | 440 dataTagName = "Data" |
26 | 441 xmlFields = [] |
14 | 442 sqlFields = [] |
16 | 443 numFields = 0 |
14 | 444 sqlInsert = None |
445 | |
446 logging.debug("import excel xml") | |
447 | |
448 ret="" | |
16 | 449 if isinstance(xmldata, str): |
14 | 450 logging.debug("importXML reading string data") |
16 | 451 doc=parseString(xmldata) |
14 | 452 else: |
453 logging.debug("importXML reading file data") | |
16 | 454 doc=parse(xmldata) |
14 | 455 |
456 cnt = 0 | |
457 while True: | |
458 node=doc.getEvent() | |
459 | |
460 if node is None: | |
461 break | |
462 | |
463 else: | |
16 | 464 #logging.debug("tag=%s"%node[1].localName) |
14 | 465 if node[1].localName is not None: |
466 tagName = node[1].localName.lower() | |
467 else: | |
468 # ignore non-tag nodes | |
469 continue | |
16 | 470 |
14 | 471 if tagName == rowTagName.lower(): |
472 # start of row | |
473 doc.expandNode(node[1]) | |
474 cnt += 1 | |
475 if cnt == 1: | |
476 # first row -- field names | |
477 names=node[1].getElementsByTagNameNS(namespace, dataTagName) | |
478 for name in names: | |
479 fn = getTextFromNode(name) | |
26 | 480 xmlFields.append({'name':sqlName(fn),'type':'text'}) |
14 | 481 |
26 | 482 if fieldsOnly: |
14 | 483 # return just field names |
26 | 484 return xmlFields |
14 | 485 |
486 # create table | |
28 | 487 if fields is None: |
488 sqlFields = self.createEmptyTable(schema, table, xmlFields) | |
16 | 489 numFields = len(sqlFields) |
15 | 490 fieldString = ", ".join(['"%s"'%f['name'] for f in sqlFields]) |
14 | 491 valString = ", ".join(["%s" for f in sqlFields]) |
16 | 492 sqlInsert = 'insert into "%s"."%s" (%s) values (%s)'%(schema,table,fieldString,valString) |
26 | 493 #logging.debug("importexcelsql: sqlInsert=%s"%sqlInsert) |
14 | 494 |
495 else: | |
496 # following rows are data | |
16 | 497 colNodes=node[1].getElementsByTagNameNS(namespace, colTagName) |
14 | 498 data = [] |
16 | 499 hasData = False |
500 for colNode in colNodes: | |
501 dataNodes=colNode.getElementsByTagNameNS(namespace, dataTagName) | |
502 if len(dataNodes) > 0: | |
503 val = getTextFromNode(dataNodes[0]) | |
504 hasData = True | |
505 else: | |
506 val = None | |
507 | |
14 | 508 data.append(val) |
509 | |
16 | 510 if not hasData: |
511 # ignore empty rows | |
512 continue | |
513 | |
514 # fix number of data fields | |
515 if len(data) > numFields: | |
516 del data[numFields:] | |
517 elif len(data) < numFields: | |
518 missFields = numFields - len(data) | |
519 data.extend(missFields * [None,]) | |
520 | |
521 logging.debug("importexcel sqlinsert=%s data=%s"%(sqlInsert,data)) | |
17 | 522 self.executeSQL(sqlInsert, data, hasResult=False) |
14 | 523 |
16 | 524 return cnt |
21
a67b7c1f7ec5
Merge with Falks GIS stuff 78e70dfa7ad6b27d10d490f9ae8820306e4fe5d4
casties
diff
changeset
|
525 |
19 | 526 # Methods for GoogleMaps creation |
527 def showGoogleMap(self,schema='chgis',table='mpdl',id=[],doc=None): | |
528 logging.debug("showGoogleMap") | |
529 data = self.getDataForGoogleMap(schema,table,id,doc) | |
530 kml=self.getKMLname(data=data,table=table) | |
531 googleMap_page=self.htmlHead()+str(self.getGoogleMapString(kml=kml)) | |
532 return googleMap_page | |
533 | |
534 def getDataForGoogleMap(self,schema='chgis',table='mpdl',id=[],doc=None): | |
535 logging.debug("getDataForGoogleMap") | |
536 qstr="SELECT * FROM "+schema+"."+table | |
537 try: | |
538 if id!=[]: | |
539 qstr=qstr+" WHERE " | |
540 for id_item in id.split(","): | |
541 if table=='mpdl': | |
542 qstr=qstr+" mpdl_xmlsource_id = '"+id_item+ "' OR" | |
543 else: | |
544 qstr=qstr+" cast(id as text) LIKE '"+id_item+ "' OR" | |
545 qstr=str(qstr).rsplit(" ",1)[0] #to remove last " and " | |
546 data=self.ZSQLSimpleSearch(qstr) | |
547 return data | |
548 except: | |
549 return qstr | |
550 | |
551 def getKMLname(self,data=[],table=""): | |
552 logging.debug("getKMLname") | |
553 #session=context.REQUEST.SESSION | |
554 kml4Marker="<kml xmlns=\'http://www.opengis.net/kml/2.2\'><Document><Style id=\'marker_icon\'><IconStyle><scale>15</scale><Icon><href>http://chinagis.mpiwg-berlin.mpg.de/chinagis/images/dot_red.png</href></Icon></IconStyle></Style>\n" | |
555 initializeStringForGoogleMaps="" | |
556 #doLine=container.getVar('doLine') | |
557 # Mapping a set of points from table-based SQL-query: | |
558 if data!=None: | |
559 try: | |
560 SQL="""SELECT \"attribute with gis_id\" FROM public.metadata WHERE tablename LIKE '"""+table+"""'""" | |
561 gisIDattribute=self.ZSQLSimpleSearch(SQL) | |
562 except: | |
563 return "table not registered within metadata" | |
564 for dataset in data: | |
565 try: | |
566 xCoord=getattr(dataset,'longitude') | |
567 yCoord=getattr(dataset,'latitude') | |
568 except: | |
569 try: | |
570 xCoord=getattr(dataset,'x_coord') | |
571 yCoord=getattr(dataset,'y_coord') | |
572 except: | |
573 # try: | |
574 gisID=getattr(dataset,getattr(gisIDattribute[0],'attribute with gis_id')) | |
575 coords=self.getPoint4GISid(gisID) | |
576 if coords!=None: | |
577 xCoord=coords[0] | |
578 yCoord=coords[1] | |
579 # except: | |
580 # return "no coordinates found" | |
581 if float(xCoord)!=0: | |
582 if float(yCoord)!=0: | |
583 kml4Marker=kml4Marker+"<Placemark>" | |
584 kml4Marker=kml4Marker+"<description> <![CDATA[<b>" | |
585 for values in dataset: | |
586 if values != (None, None): | |
587 if str(values).find('name')>-1: | |
588 kml4Marker=kml4Marker+"<name>"+str(values[1])+"</name>\n" | |
589 continue | |
590 elif str(values).find('place')>-1: | |
591 kml4Marker=kml4Marker+"<name>"+str(values[1])+"</name>\n" | |
592 continue | |
14 | 593 |
19 | 594 kml4Marker=kml4Marker+str(values)+": " |
595 attribute_string=str(values).replace("'","__Apostroph__") | |
596 attribute_string=str(attribute_string).replace('"','__DoubleApostroph__') | |
597 attribute_string=str(attribute_string).replace(';','__$$__') | |
598 attribute_string=str(attribute_string).replace('&','&') | |
599 if str(attribute_string).find('http')>-1: | |
600 attribute_string='<A HREF=' + str(attribute_string) + ' target=_blank>' + str(attribute_string) + '</A>' | |
601 kml4Marker=kml4Marker+attribute_string+"</a><br>\n" | |
602 | |
603 kml4Marker=kml4Marker+"]]></description>\n" | |
604 kml4Marker=kml4Marker+"<styleURL>#marker_icon</styleURL>\n" | |
605 kml4Marker=kml4Marker+"<Point>" | |
606 | |
607 kml4Marker=kml4Marker+"<coordinates>"+str(xCoord)+","+str(yCoord)+",0</coordinates>\n" | |
608 kml4Marker=kml4Marker+"</Point>\n" | |
609 kml4Marker=kml4Marker+"</Placemark>\n" | |
14 | 610 |
19 | 611 kml4Marker=kml4Marker+"</Document>\n</kml>" |
612 kmlFileName="marker"+str(time.time())+".kml" | |
14 | 613 |
19 | 614 # kml4Marker=str(kml4Marker).replace('&','$$') |
615 # kml4Marker=str(kml4Marker).replace(';','__$$__') | |
616 # kml4Marker=str(kml4Marker).replace('#','__SHARP__') | |
617 initializeStringForGoogleMaps="""onload=\"initialize(\'http://chinagis.mpiwg-berlin.mpg.de/chinagis/REST/daten/"""+kmlFileName+"""\')\""""#+str(data) | |
618 initializeStringForGoogleMaps=initializeStringForGoogleMaps.replace("None","0") | |
619 isLoadReady='false' | |
620 while isLoadReady=='false': | |
621 isLoadReady=self.RESTwrite2File(self.daten,kmlFileName,kml4Marker) | |
622 | |
623 return initializeStringForGoogleMaps | |
14 | 624 |
19 | 625 def getGoogleMapString(self,kml): |
626 logging.debug("getGoogleMapString") | |
627 printed= '<body %s> '%kml +"""\n <div id="map_canvas" style="width: 98%; height: 95%"> </div> \n </body>" \n </html>""" | |
628 return printed | |
629 | |
630 def getPoint4GISid(self,gis_id): | |
631 j=0 | |
632 while (True): | |
633 j=j+1 | |
634 if (j>100): # FJK: just to prevent endless loops | |
635 break | |
636 if (gis_id.isdigit()): # FJK: regular exit from while-loop | |
637 break | |
638 else: | |
639 gis_id=gis_id.strip('abcdefghijklmnopqrstuvwxyz_') # FJK: to strip all letters | |
640 gis_id=gis_id.strip() # FJK: to strip all whitespaces | |
641 resultpoint = [0,0] | |
642 results = None | |
643 try: | |
644 if int(gis_id)>0: | |
645 SQL="SELECT x_coord,y_coord FROM chgis.chgis_coords WHERE gis_id LIKE cast("+ str(gis_id) +" as text);" | |
646 results=self.ZSQLSimpleSearch(SQL) | |
647 #print results | |
648 if results != None: | |
649 for result in results: | |
650 resultpoint=[getattr(result,str('x_coord')),getattr(result,str('y_coord'))] | |
651 if resultpoint !=[0,0]: | |
652 return resultpoint | |
14 | 653 else: |
19 | 654 coords=self.getCoordsFromREST_gisID(joinid) |
655 SQL="INSERT INTO chgis.chgis_coords (gis_id,x_coord,y_coord) VALUES (" +gis_id+ "," +coords[0][1]+ "," +coords[0][0]+ "); ANALYZE chgis.chgis_coords;" | |
656 returnstring=self.ZSQLSimpleSearch(SQL) | |
657 return coords[0] | |
658 except: | |
659 error="gis_id not to interpretable:"+str(gis_id) | |
16 | 660 |
19 | 661 def getCoordsFromREST_gisID(self,gis_id): |
662 coordlist=[] | |
663 i=0 | |
664 while (i<5 and coordlist==[]): | |
13 | 665 |
19 | 666 urlresponse=container.urlFunctions.zUrlopenParseString(container.urlFunctions.zUrlopenRead("http://chgis.hmdc.harvard.edu/xml/id/"+gis_id)) |
667 baseDocElement=container.urlFunctions.zUrlopenDocumentElement(urlresponse) | |
668 childnodes=container.urlFunctions.zUrlopenChildNodes(baseDocElement) | |
669 itemnodes=container.urlFunctions.zUrlopenGetElementsByTagName(baseDocElement,'item') | |
670 | |
671 for i in range(0,container.urlFunctions.zUrlopenLength(itemnodes)): | |
672 itemnode=container.urlFunctions.zUrlopenGetItem(itemnodes,i) | |
673 itemspatialnodes=container.urlFunctions.zUrlopenGetElementsByTagName(itemnode,'spatial') | |
674 for j in range(0,container.urlFunctions.zUrlopenLength(itemspatialnodes)): | |
675 coord=[] | |
676 itemspatialnode= container.urlFunctions.zUrlopenGetItem(itemspatialnodes,j) | |
677 itemspatiallatnodes=container.urlFunctions.zUrlopenGetElementsByTagName(itemspatialnode,'degrees_latitude') | |
678 for k in range(0,container.urlFunctions.zUrlopenLength(itemspatiallatnodes)): | |
679 itemspatiallatnode= container.urlFunctions.zUrlopenGetItem(itemspatiallatnodes,k) | |
680 coord.append(container.urlFunctions.zUrlopenGetTextData(itemspatiallatnode)) | |
681 itemspatiallngnodes=container.urlFunctions.zUrlopenGetElementsByTagName(itemspatialnode,'degrees_longitude') | |
682 for k in range(0,container.urlFunctions.zUrlopenLength(itemspatiallngnodes)): | |
683 itemspatiallngnode= container.urlFunctions.zUrlopenGetItem(itemspatiallngnodes,k) | |
684 coord.append(container.urlFunctions.zUrlopenGetTextData(itemspatiallngnode)) | |
685 coordlist.append(coord) | |
686 gis_id= "_"+gis_id | |
687 return coordlist | |
688 | |
689 # End for GoogleMaps creation | |
690 | |
691 def RESTwrite2File(self,datadir, name,text): | |
692 # try: | |
693 fileid=name | |
694 if fileid in datadir.objectIds(): | |
695 datadir.manage_delObjects(fileid) | |
696 newfile=open(name,'w') | |
697 newfile.write(text) | |
698 newfile.close() | |
699 file4Read=open(name,'r') | |
700 fileInZope=datadir.manage_addFile(id=fileid,file=file4Read) | |
701 return "Write successful" | |
702 # except: | |
703 # return "Could not write" | |
13 | 704 |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
705 |
4
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
706 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
|
707 REQUEST=None): |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
708 """Change the object""" |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
709 if title is not None: |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
710 self.title = title |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
711 |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
712 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
|
713 self.connection_id = connection_id |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
714 |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
715 #checkPermission=getSecurityManager().checkPermission |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
716 REQUEST.RESPONSE.redirect('manage_main') |
e3ee1f358fe6
new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents:
2
diff
changeset
|
717 |
2
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
718 |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
719 manage_addRestDbInterfaceForm=PageTemplateFile('zpt/addRestDbInterface',globals()) |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
720 |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
721 def manage_addRestDbInterface(self, id, title='', label='', description='', |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
722 createPublic=0, |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
723 createUserF=0, |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
724 REQUEST=None): |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
725 """Add a new object with id *id*.""" |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
726 |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
727 ob=RestDbInterface(str(id),title) |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
728 self._setObject(id, ob) |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
729 |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
730 #checkPermission=getSecurityManager().checkPermission |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
731 REQUEST.RESPONSE.redirect('manage_main') |
61a3764cd5fb
new version RestDbInterface with working traversal and templates
casties
parents:
diff
changeset
|
732 |
5 | 733 |