annotate RestDbInterface.py @ 16:cbb73d103152

NEW - # 12: create table and upload data https://it-dev.mpiwg-berlin.mpg.de/tracs/GIS/ticket/12 works now!
author casties
date Mon, 16 Aug 2010 21:24:00 +0200
parents 5e3edf980813
children ed997e639cfd
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
15
5e3edf980813 more work on xml import
casties
parents: 14
diff changeset
11 import re
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
12 import psycopg2
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
13
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
14 from zope.interface import implements
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
15 from zope.publisher.interfaces import IPublishTraverse
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
16 from ZPublisher.BaseRequest import DefaultPublishTraverse
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
17 #from zope.publisher.interfaces import NotFound
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
18 #from zope.app import zapi
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
19 #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
20 import Shared.DC.ZRDB.DA
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
21 from Products.ZSQLMethods.SQL import SQLConnectionIDs
2
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
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
24 def getTextFromNode(node):
14
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
25 """get the cdata content of a XML node"""
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
26 if node is None:
14
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
27 return ""
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
28
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
29 if isinstance(node, list):
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
30 nodelist = node
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
31 else:
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
32 nodelist=node.childNodes
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
33
14
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
34 rc = ""
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
35 for node in nodelist:
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
36 if node.nodeType == node.TEXT_NODE:
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
37 rc = rc + node.data
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
38 return rc
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
39
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
40 def sqlName(s,lc=True):
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
41 """returns restricted ASCII-only version of string"""
15
5e3edf980813 more work on xml import
casties
parents: 14
diff changeset
42 if s is None:
5e3edf980813 more work on xml import
casties
parents: 14
diff changeset
43 return ""
5e3edf980813 more work on xml import
casties
parents: 14
diff changeset
44
5e3edf980813 more work on xml import
casties
parents: 14
diff changeset
45 # all else -> "_"
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
46 s = re.sub(r'[^A-Za-z0-9_]','_',s)
15
5e3edf980813 more work on xml import
casties
parents: 14
diff changeset
47 if lc:
5e3edf980813 more work on xml import
casties
parents: 14
diff changeset
48 return s.lower()
5e3edf980813 more work on xml import
casties
parents: 14
diff changeset
49
5e3edf980813 more work on xml import
casties
parents: 14
diff changeset
50 return s
14
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
51
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
52 class RestDbInterface(Folder):
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
53 """Object for RESTful database queries
7
bd52d9445a41 trying to rework
casties
parents: 5
diff changeset
54 path schema: /db/{schema}/{table}/
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
55 omitting table gives a list of schemas
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
56 omitting table and schema gives a list of schemas
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
57 """
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
58 implements(IPublishTraverse)
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 meta_type="RESTdb"
4
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
61 manage_options=Folder.manage_options+(
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
62 {'label':'Config','action':'manage_editRestDbInterfaceForm'},
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
63 )
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
64
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
65 # management templates
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
66 manage_editRestDbInterfaceForm=PageTemplateFile('zpt/editRestDbInterface',globals())
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
67
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
68 # data templates
8
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
69 XML_index = PageTemplateFile('zpt/XML_index', globals())
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
70 XML_schema = PageTemplateFile('zpt/XML_schema', globals())
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
71 XML_schema_table = PageTemplateFile('zpt/XML_schema_table', globals())
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
72 HTML_index = PageTemplateFile('zpt/HTML_index', globals())
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
73 HTML_schema = PageTemplateFile('zpt/HTML_schema', globals())
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
74 HTML_schema_table = PageTemplateFile('zpt/HTML_schema_table', globals())
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
75
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
76
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
77
4
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
78 def __init__(self, id, title, connection_id=None):
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
79 """init"""
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
80 self.id = id
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
81 self.title = title
4
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
82 # database connection id
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
83 self.connection_id = connection_id
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
84 # create template folder
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
85 self.manage_addFolder('template')
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
86
4
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
87
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
88 def getCursor(self):
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
89 """returns fresh DB cursor"""
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
90 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
91 if conn is None:
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
92 # create a new connection object
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
93 try:
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
94 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
95 # 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
96 connids = SQLConnectionIDs(self)
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
97 if len(connids) > 0:
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
98 connection_id = connids[0][0]
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
99 self.connection_id = connection_id
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
100 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
101
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
102 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
103 da.connect('')
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
104 # 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
105 conn = da._v_database_connection
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
106 #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
107 self._v_database_connection = conn
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
108 except Exception, e:
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
109 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
110
5
7539a9e69365 small fix
casties
parents: 4
diff changeset
111 cursor = conn.getcursor()
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
112 cursor.connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
4
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
113 return cursor
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
114
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
115 def executeSQL(self, query, *args):
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
116 """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
117 result format: {"fields":fields, "rows":data}"""
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
118 logging.debug("executeSQL query=%s args=%s"%(query,args))
4
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
119 cur = self.getCursor()
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
120 cur.execute(query, args)
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
121 # description of returned fields
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
122 fields = cur.description
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
123 # get all data in an array
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
124 data = cur.fetchall()
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
125 cur.close()
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
126 return {"fields":fields, "rows":data}
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
127
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
128 def executeOnlySQL(self, query, *args):
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
129 """execute query with args on database that has no results.
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
130 result format: {"fields":fields, "rows":data}"""
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
131 logging.debug("executeOnlySQL query=%s args=%s"%(query,args))
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
132 cur = self.getCursor()
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
133 cur.execute(query, args)
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
134 cur.close()
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
135 return None
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
136
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
137
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
138 def publishTraverse(self,request,name):
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
139 """change the traversal"""
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
140 # get stored path
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
141 path = request.get('restdb_path', [])
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
142 logging.debug("publishtraverse: name=%s restdb_path=%s"%(name,path))
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
143
13
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
144 if name in ("index_html", "PUT"):
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
145 # end of traversal
13
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
146 if request.get("method") == "POST" and request.get("action",None) == "PUT":
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
147 # fake PUT by POST with action=PUT
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
148 name = "PUT"
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
149
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
150 return getattr(self, name)
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
151 #TODO: should we check more?
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
152 else:
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
153 # traverse
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
154 if len(path) == 0:
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
155 # first segment
8
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
156 if name == 'db':
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
157 # virtual path -- continue traversing
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
158 path = [name]
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
159 request['restdb_path'] = path
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 # try real path
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
162 tr = DefaultPublishTraverse(self, request)
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
163 ob = tr.publishTraverse(request, name)
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
164 return ob
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
165 else:
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
166 path.append(name)
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
167
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
168 # continue traversing
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
169 return self
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
170
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
171 def index_html(self,REQUEST,RESPONSE):
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
172 """index method"""
8
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
173 # ReST path was stored in request
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
174 path = REQUEST.get('restdb_path',[])
8
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
175 # type and format are real parameter
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
176 format = REQUEST.get('format','HTML').upper()
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
177 type = REQUEST.get('type',None)
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
178 logging.debug("index_html path=%s format=%s type=%s"%(path,format,type))
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
179
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
180 if type is not None:
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
181 # non-empty type -- look for template
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
182 pt = getattr(self.template, "%s_%s"%(format,type), None)
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
183 if pt is not None:
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
184 return pt(format=format,type=type,path=path)
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
185
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
186 if len(path) == 1:
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
187 # list of schemas
8
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
188 return self.showListOfSchemas(format=format)
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
189 elif len(path) == 2:
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
190 # list of tables
8
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
191 return self.showListOfTables(format=format,schema=path[1])
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
192 elif len(path) == 3:
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
193 # table
8
a9a49f5765c9 reworking templates and schema
casties
parents: 7
diff changeset
194 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
195
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
196 # don't know what to do
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
197 return str(REQUEST)
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
198
12
0fa22c291ff1 NEW - # 12: create table and upload data
casties
parents: 9
diff changeset
199 def PUT(self, REQUEST, RESPONSE):
0fa22c291ff1 NEW - # 12: create table and upload data
casties
parents: 9
diff changeset
200 """
0fa22c291ff1 NEW - # 12: create table and upload data
casties
parents: 9
diff changeset
201 Implement WebDAV/HTTP PUT/FTP put method for this object.
0fa22c291ff1 NEW - # 12: create table and upload data
casties
parents: 9
diff changeset
202 """
13
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
203 logging.debug("RestDbInterface PUT")
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
204 #logging.debug("req=%s"%REQUEST)
12
0fa22c291ff1 NEW - # 12: create table and upload data
casties
parents: 9
diff changeset
205 #self.dav__init(REQUEST, RESPONSE)
0fa22c291ff1 NEW - # 12: create table and upload data
casties
parents: 9
diff changeset
206 #self.dav__simpleifhandler(REQUEST, RESPONSE)
13
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
207 # ReST path was stored in request
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
208 path = REQUEST.get('restdb_path',[])
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
209 if len(path) == 3:
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
210 schema = path[1]
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
211 tablename = path[2]
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
212 file = REQUEST.get("create_table_file",None)
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
213 if file is None:
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
214 RESPONSE.setStatus(400)
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
215 return
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
216
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
217 logging.debug("put with schema=%s table=%s file=%s"%(schema,tablename,file))
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
218 self.createTableFromXML(schema, tablename, file)
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
219
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
220 else:
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
221 # 400 Bad Request
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
222 RESPONSE.setStatus(400)
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
223 return
12
0fa22c291ff1 NEW - # 12: create table and upload data
casties
parents: 9
diff changeset
224
4
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
225
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
226 def showTable(self,format='XML',schema='public',table=None):
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
227 """returns PageTemplate with tables"""
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
228 logging.debug("showtable")
9
76ac7a721273 more rework of templates
casties
parents: 8
diff changeset
229 pt = getattr(self.template, '%s_schema_table'%format, None)
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
230 if pt is None:
9
76ac7a721273 more rework of templates
casties
parents: 8
diff changeset
231 return "ERROR!! template %s_schema_table not found"%format
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
232
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
233 data = self.getTable(schema,table)
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
234 return pt(data=data,tablename=table)
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
235
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
236
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
237 def getTable(self,schema='public',table=None,username='guest'):
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
238 """return table data"""
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
239 logging.debug("gettable")
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
240 data = self.executeSQL('select * from "%s"."%s"'%(schema,table))
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
241 return data
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
242
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
243 def showListOfTables(self,format='XML',schema='public'):
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
244 """returns PageTemplate with list of tables"""
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
245 logging.debug("showlistoftables")
9
76ac7a721273 more rework of templates
casties
parents: 8
diff changeset
246 pt = getattr(self.template, '%s_schema'%format, None)
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
247 if pt is None:
9
76ac7a721273 more rework of templates
casties
parents: 8
diff changeset
248 return "ERROR!! template %s_schema not found"%format
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
249
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
250 data = self.getListOfTables(schema)
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
251 return pt(data=data,schema=schema)
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
252
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
253 def getListOfTables(self,schema='public',username='guest'):
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
254 """return list of tables"""
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
255 logging.debug("getlistoftables")
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
256 # get list of fields and types of db table
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
257 qstr="""select c.relname FROM pg_catalog.pg_class c
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
258 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
259 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
260 AND pg_catalog.pg_table_is_visible(c.oid)"""
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
261 #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
262 data=self.executeSQL(qstr)
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
263 return data
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
264
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
265 def showListOfSchemas(self,format='XML'):
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
266 """returns PageTemplate with list of schemas"""
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
267 logging.debug("showlistofschemas")
9
76ac7a721273 more rework of templates
casties
parents: 8
diff changeset
268 pt = getattr(self.template, '%s_index'%format, None)
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
269 if pt is None:
9
76ac7a721273 more rework of templates
casties
parents: 8
diff changeset
270 return "ERROR!! template %s_index not found"%format
2
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 data = self.getListOfSchemas()
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
273 return pt(data=data)
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
274
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
275 def getListOfSchemas(self,username='guest'):
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
276 """return list of schemas"""
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
277 logging.debug("getlistofschemas")
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
278 # TODO: really look up schemas
9
76ac7a721273 more rework of templates
casties
parents: 8
diff changeset
279 data={'fields': (('schemas',),), 'rows': [('public',),]}
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
280 return data
14
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
281
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
282 def createEmptyTable(self,schema,table,fields):
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
283 """create a table with the given fields
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
284 returns list of created fields"""
15
5e3edf980813 more work on xml import
casties
parents: 14
diff changeset
285 logging.debug("createEmptyTable")
14
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
286 sqlFields = []
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
287 for f in fields:
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
288 if isinstance(f,dict):
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
289 # {name: XX, type: YY}
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
290 name = sqlName(f['name'])
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
291 type = f['type']
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
292
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
293 else:
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
294 # name only
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
295 name = sqlName(f)
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
296 type = 'text'
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
297
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
298 sqlFields.append({'name':name, 'type':type})
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
299
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
300 # drop table if it exists
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
301 try:
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
302 res = self.executeSQL('select * from "%s"."%s" where 1=0'%(schema,table))
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
303 logging.debug("createemptytable: table %s.%s exists"%(schema,table))
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
304 self.executeOnlySQL('drop table "%s"."%s"'%(schema,table))
14
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
305 except:
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
306 pass
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
307
15
5e3edf980813 more work on xml import
casties
parents: 14
diff changeset
308 fieldString = ", ".join(['"%s" %s'%(f['name'],f['type']) for f in sqlFields])
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
309 sqlString = 'create table "%s"."%s" (%s)'%(schema,table,fieldString)
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
310 logging.debug("createemptytable: SQL=%s"%sqlString)
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
311 ret = self.executeOnlySQL(sqlString)
14
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
312
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
313 return sqlFields
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
314
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
315
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
316
13
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
317 def createTableFromXML(self,schema,table,data):
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
318 """create or replace a table with the given XML data"""
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
319 logging.debug("createTableFromXML schema=%s table=%s data=%s"%(schema,table,data))
15
5e3edf980813 more work on xml import
casties
parents: 14
diff changeset
320 self.importExcelXML(schema,table, data)
14
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
321
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
322 def importExcelXML(self,schema,table,xmldata,fieldNamesOnly=False):
14
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
323 '''
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
324 Import XML file in Excel format into the table
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
325 @param table: name of the table the xml shall be imported into
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
326 @param containerTagName: XML-Tag which describes a dataset
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
327 @param data: data to be imported
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
328 @param identify: (optional) field res. tag which identifies a entry uniquely for updating purposes.
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
329 @param RESPONSE: (optional)
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
330 '''
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
331 from xml.dom.pulldom import parseString,parse
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
332
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
333 namespace = "urn:schemas-microsoft-com:office:spreadsheet"
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
334 containerTagName = "Table"
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
335 rowTagName = "Row"
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
336 colTagName = "Cell"
14
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
337 dataTagName = "Data"
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
338 fieldNames = []
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
339 sqlFields = []
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
340 numFields = 0
14
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
341 sqlInsert = None
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
342
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
343 logging.debug("import excel xml")
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
344
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
345 ret=""
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
346 if isinstance(xmldata, str):
14
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
347 logging.debug("importXML reading string data")
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
348 doc=parseString(xmldata)
14
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
349 else:
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
350 logging.debug("importXML reading file data")
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
351 doc=parse(xmldata)
14
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
352
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
353 cnt = 0
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
354 while True:
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
355 node=doc.getEvent()
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
356
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
357 if node is None:
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
358 break
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
359
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
360 else:
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
361 #logging.debug("tag=%s"%node[1].localName)
14
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
362 if node[1].localName is not None:
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
363 tagName = node[1].localName.lower()
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
364 else:
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
365 # ignore non-tag nodes
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
366 continue
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
367
14
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
368 if tagName == rowTagName.lower():
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
369 # start of row
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
370 doc.expandNode(node[1])
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
371 cnt += 1
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
372 if cnt == 1:
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
373 # first row -- field names
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
374 names=node[1].getElementsByTagNameNS(namespace, dataTagName)
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
375 for name in names:
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
376 fn = getTextFromNode(name)
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
377 fieldNames.append(fn)
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
378
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
379 if fieldNamesOnly:
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
380 # return just field names
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
381 return fieldNames
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
382
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
383 # create table
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
384 sqlFields = self.createEmptyTable(schema, table, fieldNames)
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
385 numFields = len(sqlFields)
15
5e3edf980813 more work on xml import
casties
parents: 14
diff changeset
386 fieldString = ", ".join(['"%s"'%f['name'] for f in sqlFields])
14
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
387 valString = ", ".join(["%s" for f in sqlFields])
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
388 sqlInsert = 'insert into "%s"."%s" (%s) values (%s)'%(schema,table,fieldString,valString)
15
5e3edf980813 more work on xml import
casties
parents: 14
diff changeset
389 logging.debug("importexcelsql: sqlInsert=%s"%sqlInsert)
14
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
390
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
391 else:
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
392 # following rows are data
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
393 colNodes=node[1].getElementsByTagNameNS(namespace, colTagName)
14
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
394 data = []
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
395 hasData = False
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
396 for colNode in colNodes:
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
397 dataNodes=colNode.getElementsByTagNameNS(namespace, dataTagName)
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
398 if len(dataNodes) > 0:
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
399 val = getTextFromNode(dataNodes[0])
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
400 hasData = True
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
401 else:
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
402 val = None
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
403
14
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
404 data.append(val)
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
405
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
406 if not hasData:
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
407 # ignore empty rows
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
408 continue
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
409
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
410 # fix number of data fields
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
411 if len(data) > numFields:
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
412 del data[numFields:]
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
413 elif len(data) < numFields:
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
414 missFields = numFields - len(data)
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
415 data.extend(missFields * [None,])
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
416
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
417 logging.debug("importexcel sqlinsert=%s data=%s"%(sqlInsert,data))
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
418 self.executeOnlySQL(sqlInsert, *data)
14
05933707897f NEW - # 12: create table and upload data
casties
parents: 13
diff changeset
419
16
cbb73d103152 NEW - # 12: create table and upload data
casties
parents: 15
diff changeset
420 return cnt
13
e2c73c077533 NEW - # 12: create table and upload data
casties
parents: 12
diff changeset
421
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
422
4
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
423 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
424 REQUEST=None):
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
425 """Change the object"""
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
426 if title is not None:
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
427 self.title = title
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
428
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
429 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
430 self.connection_id = connection_id
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
431
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
432 #checkPermission=getSecurityManager().checkPermission
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
433 REQUEST.RESPONSE.redirect('manage_main')
e3ee1f358fe6 new version that doesn't use ZSQLExtend but the database connection more directly.
casties
parents: 2
diff changeset
434
2
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
435
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
436 manage_addRestDbInterfaceForm=PageTemplateFile('zpt/addRestDbInterface',globals())
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
437
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
438 def manage_addRestDbInterface(self, id, title='', label='', description='',
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
439 createPublic=0,
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
440 createUserF=0,
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
441 REQUEST=None):
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
442 """Add a new object with id *id*."""
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
443
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
444 ob=RestDbInterface(str(id),title)
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
445 self._setObject(id, ob)
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
446
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
447 #checkPermission=getSecurityManager().checkPermission
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
448 REQUEST.RESPONSE.redirect('manage_main')
61a3764cd5fb new version RestDbInterface with working traversal and templates
casties
parents:
diff changeset
449
5
7539a9e69365 small fix
casties
parents: 4
diff changeset
450