annotate RestDbInterface.py @ 73:695b6612d4c6

getKmlUrl eingebaut
author fknauft
date Wed, 01 Sep 2010 17:42:13 +0200
parents a67b7c1f7ec5
children 6e19a9af00e6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
19
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
11 import time
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
12
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
13 from zope.interface import implements
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
14 from zope.publisher.interfaces import IPublishTraverse
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
15 from ZPublisher.BaseRequest import DefaultPublishTraverse
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
16 #from zope.publisher.interfaces import NotFound
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
17 #from zope.app import zapi
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
18 #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
19 import Shared.DC.ZRDB.DA
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
20 from Products.ZSQLMethods.SQL import SQLConnectionIDs
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
21
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
22
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
23 class RestDbInterface(Folder):
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
24 """Object for RESTful database queries
7
bd52d9445a41 trying to rework
casties
parents: 5
diff changeset
25 path schema: /db/{schema}/{table}/
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
26 omitting table gives a list of schemas
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
27 omitting table and schema gives a list of schemas
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
28 """
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
29 implements(IPublishTraverse)
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
30
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
31 meta_type="RESTdb"
4
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
32 manage_options=Folder.manage_options+(
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
33 {'label':'Config','action':'manage_editRestDbInterfaceForm'},
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
34 )
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
35
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
36 # management templates
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
37 manage_editRestDbInterfaceForm=PageTemplateFile('zpt/editRestDbInterface',globals())
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
38
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
39 # data templates
8
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
40 XML_index = PageTemplateFile('zpt/XML_index', globals())
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
41 XML_schema = PageTemplateFile('zpt/XML_schema', globals())
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
42 XML_schema_table = PageTemplateFile('zpt/XML_schema_table', globals())
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
43 HTML_index = PageTemplateFile('zpt/HTML_index', globals())
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
44 HTML_schema = PageTemplateFile('zpt/HTML_schema', globals())
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
45 HTML_schema_table = PageTemplateFile('zpt/HTML_schema_table', globals())
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
46
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
47
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
48
4
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
49 def __init__(self, id, title, connection_id=None):
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
50 """init"""
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
51 self.id = id
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
52 self.title = title
4
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
53 # database connection id
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
54 self.connection_id = connection_id
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
55 # create template folder
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
56 self.manage_addFolder('template')
73
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
57 # create data folder
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
58 #self.manage_addFolder('daten')
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
59
4
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
60
73
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
61 def getCursor(self):
4
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
62 """returns fresh DB cursor"""
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
63 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
64 if conn is None:
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
65 # create a new connection object
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
66 try:
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
67 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
68 # 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
69 connids = SQLConnectionIDs(self)
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
70 if len(connids) > 0:
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
71 connection_id = connids[0][0]
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
72 self.connection_id = connection_id
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
73 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
74
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
75 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
76 da.connect('')
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
77 # 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
78 conn = da._v_database_connection
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
79 #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
80 self._v_database_connection = conn
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
81 except Exception, e:
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
82 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
83
5
7539a9e69365 small fix
casties
parents: 4
diff changeset
84 cursor = conn.getcursor()
4
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
85 return cursor
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
86
73
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
87 def executeSQL(self, query, *args):
4
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
88 """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
89 result format: {"fields":fields, "rows":data}"""
73
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
90 cur = self.getCursor()
4
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
91 cur.execute(query, args)
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
92 # description of returned fields
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
93 fields = cur.description
73
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
94 # get all data in an array
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
95 data = cur.fetchall()
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
96 cur.close()
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
97 return {"fields":fields, "rows":data}
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
98
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
99
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
100 def publishTraverse(self,request,name):
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
101 """change the traversal"""
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
102 # get stored path
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
103 path = request.get('restdb_path', [])
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
104 logging.debug("publishtraverse: name=%s restdb_path=%s"%(name,path))
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
105
73
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
106 if name == "index_html":
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
107 # end of traversal
73
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
108 return self.index_html
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
109 #TODO: should we check more?
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
110 else:
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
111 # traverse
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
112 if len(path) == 0:
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
113 # first segment
8
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
114 if name == 'db':
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
115 # virtual path -- continue traversing
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
116 path = [name]
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
117 request['restdb_path'] = path
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
118 else:
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
119 # try real path
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
120 tr = DefaultPublishTraverse(self, request)
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
121 ob = tr.publishTraverse(request, name)
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
122 return ob
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
123 else:
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
124 path.append(name)
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
125
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
126 # continue traversing
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
127 return self
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
128
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
129 def index_html(self,REQUEST,RESPONSE):
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
130 """index method"""
8
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
131 # ReST path was stored in request
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
132 path = REQUEST.get('restdb_path',[])
19
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
133
8
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
134 # type and format are real parameter
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
135 format = REQUEST.get('format','HTML').upper()
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
136 type = REQUEST.get('type',None)
19
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
137
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
138 # id and doc are used for GoogleMaps content
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
139 id = REQUEST.get('id',[])
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
140 doc = REQUEST.get('doc',None)
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
141
8
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
142 logging.debug("index_html path=%s format=%s type=%s"%(path,format,type))
73
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
143 RESPONSE.setHeader('Access-Control-Allow-Origin', '*')
8
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
144
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
145 if type is not None:
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
146 # non-empty type -- look for template
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
147 pt = getattr(self.template, "%s_%s"%(format,type), None)
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
148 if pt is not None:
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
149 return pt(format=format,type=type,path=path)
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
150
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
151 if len(path) == 1:
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
152 # list of schemas
8
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
153 return self.showListOfSchemas(format=format)
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
154 elif len(path) == 2:
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
155 # list of tables
8
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
156 return self.showListOfTables(format=format,schema=path[1])
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
157 elif len(path) == 3:
19
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
158 # GIS
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
159 if format=="GIS":
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
160 return self.showGoogleMap(schema=path[1],table=path[2],id=id,doc=doc)
73
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
161 if format=="KML_URL":
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
162 return self.getKmlUrl(schema=path[1],table=path[2],id=id,doc=doc)
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
163 # table
8
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
164 return self.showTable(format=format,schema=path[1],table=path[2])
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
165
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
166 # don't know what to do
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
167 return str(REQUEST)
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
168
4
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
169
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
170 def showTable(self,format='XML',schema='public',table=None):
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
171 """returns PageTemplate with tables"""
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
172 logging.debug("showtable")
9
76ac7a721273 more rework of templates
casties
parents: 8
diff changeset
173 pt = getattr(self.template, '%s_schema_table'%format, None)
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
174 if pt is None:
9
76ac7a721273 more rework of templates
casties
parents: 8
diff changeset
175 return "ERROR!! template %s_schema_table not found"%format
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
176
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
177 data = self.getTable(schema,table)
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
178 return pt(data=data,tablename=table)
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
179
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
180
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
181 def getTable(self,schema='public',table=None,username='guest'):
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
182 """return table data"""
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
183 logging.debug("gettable")
73
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
184 data = self.executeSQL("select * from %s"%table)
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
185 return data
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
186
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
187 def showListOfTables(self,format='XML',schema='public'):
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
188 """returns PageTemplate with list of tables"""
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
189 logging.debug("showlistoftables")
9
76ac7a721273 more rework of templates
casties
parents: 8
diff changeset
190 pt = getattr(self.template, '%s_schema'%format, None)
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
191 if pt is None:
9
76ac7a721273 more rework of templates
casties
parents: 8
diff changeset
192 return "ERROR!! template %s_schema not found"%format
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
193
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
194 data = self.getListOfTables(schema)
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
195 return pt(data=data,schema=schema)
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
196
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
197 def getListOfTables(self,schema='public',username='guest'):
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
198 """return list of tables"""
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
199 logging.debug("getlistoftables")
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
200 # get list of fields and types of db table
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
201 qstr="""select c.relname FROM pg_catalog.pg_class c
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
202 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
203 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
204 AND pg_catalog.pg_table_is_visible(c.oid)"""
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
205 #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
206 data=self.executeSQL(qstr)
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
207 return data
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
208
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
209 def showListOfSchemas(self,format='XML'):
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
210 """returns PageTemplate with list of schemas"""
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
211 logging.debug("showlistofschemas")
9
76ac7a721273 more rework of templates
casties
parents: 8
diff changeset
212 pt = getattr(self.template, '%s_index'%format, None)
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
213 if pt is None:
9
76ac7a721273 more rework of templates
casties
parents: 8
diff changeset
214 return "ERROR!! template %s_index not found"%format
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
215
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
216 data = self.getListOfSchemas()
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
217 return pt(data=data)
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
218
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
219 def getListOfSchemas(self,username='guest'):
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
220 """return list of schemas"""
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
221 logging.debug("getlistofschemas")
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
222 # TODO: really look up schemas
9
76ac7a721273 more rework of templates
casties
parents: 8
diff changeset
223 data={'fields': (('schemas',),), 'rows': [('public',),]}
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
224 return data
19
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
225
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
226 # Methods for GoogleMaps creation
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
227 def showGoogleMap(self,schema='chgis',table='mpdl',id=[],doc=None):
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
228 logging.debug("showGoogleMap")
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
229 data = self.getDataForGoogleMap(schema,table,id,doc)
73
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
230 kmlFileName=self.getKMLname(data=data,table=table)
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
231 initializeStringForGoogleMaps="""onload=\"initialize(\'http://chinagis.mpiwg-berlin.mpg.de/chinagis/REST/daten/"""+kmlFileName+"""\')\""""#+str(data)
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
232 initializeStringForGoogleMaps=initializeStringForGoogleMaps.replace("None","0")
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
233 googleMap_page=self.htmlHead()+str(self.getGoogleMapString(kml=initializeStringForGoogleMaps))
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
234 return googleMap_page
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
235
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
236 def getKmlUrl(self,schema='chgis',table='mpdl',id=[],doc=None):
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
237 logging.debug("getKmlUrl")
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
238 data = self.getDataForGoogleMap(schema,table,id,doc)
19
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
239 kml=self.getKMLname(data=data,table=table)
73
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
240 return """http://chinagis.mpiwg-berlin.mpg.de/chinagis/REST/daten/"""+kml
19
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
241
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
242 def getDataForGoogleMap(self,schema='chgis',table='mpdl',id=[],doc=None):
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
243 logging.debug("getDataForGoogleMap")
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
244 qstr="SELECT * FROM "+schema+"."+table
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
245 try:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
246 if id!=[]:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
247 qstr=qstr+" WHERE "
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
248 for id_item in id.split(","):
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
249 if table=='mpdl':
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
250 qstr=qstr+" mpdl_xmlsource_id = '"+id_item+ "' OR"
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
251 else:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
252 qstr=qstr+" cast(id as text) LIKE '"+id_item+ "' OR"
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
253 qstr=str(qstr).rsplit(" ",1)[0] #to remove last " and "
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
254 data=self.ZSQLSimpleSearch(qstr)
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
255 return data
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
256 except:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
257 return qstr
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
258
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
259 def getKMLname(self,data=[],table=""):
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
260 logging.debug("getKMLname")
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
261 #session=context.REQUEST.SESSION
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
262 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"
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
263 initializeStringForGoogleMaps=""
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
264 #doLine=container.getVar('doLine')
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
265 # Mapping a set of points from table-based SQL-query:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
266 if data!=None:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
267 try:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
268 SQL="""SELECT \"attribute with gis_id\" FROM public.metadata WHERE tablename LIKE '"""+table+"""'"""
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
269 gisIDattribute=self.ZSQLSimpleSearch(SQL)
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
270 except:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
271 return "table not registered within metadata"
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
272 for dataset in data:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
273 try:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
274 xCoord=getattr(dataset,'longitude')
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
275 yCoord=getattr(dataset,'latitude')
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
276 except:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
277 try:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
278 xCoord=getattr(dataset,'x_coord')
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
279 yCoord=getattr(dataset,'y_coord')
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
280 except:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
281 # try:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
282 gisID=getattr(dataset,getattr(gisIDattribute[0],'attribute with gis_id'))
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
283 coords=self.getPoint4GISid(gisID)
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
284 if coords!=None:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
285 xCoord=coords[0]
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
286 yCoord=coords[1]
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
287 # except:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
288 # return "no coordinates found"
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
289 if float(xCoord)!=0:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
290 if float(yCoord)!=0:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
291 kml4Marker=kml4Marker+"<Placemark>"
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
292 kml4Marker=kml4Marker+"<description> <![CDATA[<b>"
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
293 for values in dataset:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
294 if values != (None, None):
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
295 if str(values).find('name')>-1:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
296 kml4Marker=kml4Marker+"<name>"+str(values[1])+"</name>\n"
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
297 continue
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
298 elif str(values).find('place')>-1:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
299 kml4Marker=kml4Marker+"<name>"+str(values[1])+"</name>\n"
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
300 continue
14
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
301
19
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
302 kml4Marker=kml4Marker+str(values)+": "
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
303 attribute_string=str(values).replace("'","__Apostroph__")
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
304 attribute_string=str(attribute_string).replace('"','__DoubleApostroph__')
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
305 attribute_string=str(attribute_string).replace(';','__$$__')
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
306 attribute_string=str(attribute_string).replace('&','&amp;')
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
307 if str(attribute_string).find('http')>-1:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
308 attribute_string='<A HREF=' + str(attribute_string) + ' target=_blank>' + str(attribute_string) + '</A>'
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
309 kml4Marker=kml4Marker+attribute_string+"</a><br>\n"
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
310
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
311 kml4Marker=kml4Marker+"]]></description>\n"
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
312 kml4Marker=kml4Marker+"<styleURL>#marker_icon</styleURL>\n"
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
313 kml4Marker=kml4Marker+"<Point>"
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
314
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
315 kml4Marker=kml4Marker+"<coordinates>"+str(xCoord)+","+str(yCoord)+",0</coordinates>\n"
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
316 kml4Marker=kml4Marker+"</Point>\n"
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
317 kml4Marker=kml4Marker+"</Placemark>\n"
14
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
318
19
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
319 kml4Marker=kml4Marker+"</Document>\n</kml>"
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
320 kmlFileName="marker"+str(time.time())+".kml"
14
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
321
19
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
322 # kml4Marker=str(kml4Marker).replace('&','$$')
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
323 # kml4Marker=str(kml4Marker).replace(';','__$$__')
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
324 # kml4Marker=str(kml4Marker).replace('#','__SHARP__')
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
325 isLoadReady='false'
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
326 while isLoadReady=='false':
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
327 isLoadReady=self.RESTwrite2File(self.daten,kmlFileName,kml4Marker)
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
328
73
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
329 return kmlFileName
14
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
330
19
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
331 def getGoogleMapString(self,kml):
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
332 logging.debug("getGoogleMapString")
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
333 printed= '<body %s> '%kml +"""\n <div id="map_canvas" style="width: 98%; height: 95%"> </div> \n </body>" \n </html>"""
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
334 return printed
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
335
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
336 def getPoint4GISid(self,gis_id):
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
337 j=0
73
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
338 coords=(0,0)
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
339 if gis_id != None:
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
340 while (True):
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
341 j=j+1
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
342 if (j>100): # FJK: just to prevent endless loops
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
343 break
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
344 if (gis_id.isdigit()): # FJK: regular exit from while-loop
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
345 break
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
346 else:
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
347 gis_id=gis_id.strip('abcdefghijklmnopqrstuvwxyz_') # FJK: to strip all letters
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
348 gis_id=gis_id.strip() # FJK: to strip all whitespaces
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
349 resultpoint = [0,0]
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
350 results = None
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
351 try:
19
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
352 if int(gis_id)>0:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
353 SQL="SELECT x_coord,y_coord FROM chgis.chgis_coords WHERE gis_id LIKE cast("+ str(gis_id) +" as text);"
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
354 results=self.ZSQLSimpleSearch(SQL)
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
355 #print results
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
356 if results != None:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
357 for result in results:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
358 resultpoint=[getattr(result,str('x_coord')),getattr(result,str('y_coord'))]
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
359 if resultpoint !=[0,0]:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
360 return resultpoint
14
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
361 else:
19
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
362 coords=self.getCoordsFromREST_gisID(joinid)
73
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
363 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;"
19
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
364 returnstring=self.ZSQLSimpleSearch(SQL)
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
365 return coords[0]
73
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
366 except:
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
367 return "gis_id not to interpretable:"+str(gis_id)
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
368 else:
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
369 return coords[0]
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
370
19
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
371 def getCoordsFromREST_gisID(self,gis_id):
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
372 coordlist=[]
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
373 i=0
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
374 while (i<5 and coordlist==[]):
13
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
375
19
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
376 urlresponse=container.urlFunctions.zUrlopenParseString(container.urlFunctions.zUrlopenRead("http://chgis.hmdc.harvard.edu/xml/id/"+gis_id))
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
377 baseDocElement=container.urlFunctions.zUrlopenDocumentElement(urlresponse)
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
378 childnodes=container.urlFunctions.zUrlopenChildNodes(baseDocElement)
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
379 itemnodes=container.urlFunctions.zUrlopenGetElementsByTagName(baseDocElement,'item')
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
380
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
381 for i in range(0,container.urlFunctions.zUrlopenLength(itemnodes)):
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
382 itemnode=container.urlFunctions.zUrlopenGetItem(itemnodes,i)
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
383 itemspatialnodes=container.urlFunctions.zUrlopenGetElementsByTagName(itemnode,'spatial')
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
384 for j in range(0,container.urlFunctions.zUrlopenLength(itemspatialnodes)):
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
385 coord=[]
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
386 itemspatialnode= container.urlFunctions.zUrlopenGetItem(itemspatialnodes,j)
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
387 itemspatiallatnodes=container.urlFunctions.zUrlopenGetElementsByTagName(itemspatialnode,'degrees_latitude')
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
388 for k in range(0,container.urlFunctions.zUrlopenLength(itemspatiallatnodes)):
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
389 itemspatiallatnode= container.urlFunctions.zUrlopenGetItem(itemspatiallatnodes,k)
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
390 coord.append(container.urlFunctions.zUrlopenGetTextData(itemspatiallatnode))
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
391 itemspatiallngnodes=container.urlFunctions.zUrlopenGetElementsByTagName(itemspatialnode,'degrees_longitude')
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
392 for k in range(0,container.urlFunctions.zUrlopenLength(itemspatiallngnodes)):
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
393 itemspatiallngnode= container.urlFunctions.zUrlopenGetItem(itemspatiallngnodes,k)
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
394 coord.append(container.urlFunctions.zUrlopenGetTextData(itemspatiallngnode))
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
395 coordlist.append(coord)
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
396 gis_id= "_"+gis_id
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
397 return coordlist
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
398
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
399 # End for GoogleMaps creation
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
400
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
401 def RESTwrite2File(self,datadir, name,text):
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
402 # try:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
403 fileid=name
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
404 if fileid in datadir.objectIds():
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
405 datadir.manage_delObjects(fileid)
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
406 newfile=open(name,'w')
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
407 newfile.write(text)
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
408 newfile.close()
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
409 file4Read=open(name,'r')
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
410 fileInZope=datadir.manage_addFile(id=fileid,file=file4Read)
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
411 return "Write successful"
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
412 # except:
78e70dfa7ad6 GoogleMaps related functions
fknauft
parents: 18
diff changeset
413 # return "Could not write"
73
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
414
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
415
4
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
416 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
417 REQUEST=None):
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
418 """Change the object"""
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
419 if title is not None:
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
420 self.title = title
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
421
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
422 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
423 self.connection_id = connection_id
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
424
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
425 #checkPermission=getSecurityManager().checkPermission
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
426 REQUEST.RESPONSE.redirect('manage_main')
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
427
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
428
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
429 manage_addRestDbInterfaceForm=PageTemplateFile('zpt/addRestDbInterface',globals())
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
430
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
431 def manage_addRestDbInterface(self, id, title='', label='', description='',
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
432 createPublic=0,
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
433 createUserF=0,
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
434 REQUEST=None):
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
435 """Add a new object with id *id*."""
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
436
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
437 ob=RestDbInterface(str(id),title)
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
438 self._setObject(id, ob)
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
439
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
440 #checkPermission=getSecurityManager().checkPermission
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
441 REQUEST.RESPONSE.redirect('manage_main')
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
442
5
7539a9e69365 small fix
casties
parents: 4
diff changeset
443
73
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
444
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
445 # constructors = (
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
446 # REST_test.manage_addRESTclassForm,
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
447 # REST_test.manage_addRESTclass
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
448 # )
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
449
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
450
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
451 context.registerClass(
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
452 RestDbInterface.RestDbInterface,
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
453 constructors = (
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
454 RestDbInterface.manage_addRestDbInterfaceForm,
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
455 RestDbInterface.manage_addRestDbInterface
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
456 )
695b6612d4c6 getKmlUrl eingebaut
fknauft
parents: 21
diff changeset
457 )