scaffold.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 import os, sys, string, time, getopt
4 from log import *
5 
6 import neo_cgi, neo_cs, neo_util
7 import odb, hdfhelp, CSPage
8 
10  def setupDetails(self, db, tableName):
11  self.db = db
12  self.tableName = tableName
13 
14  def display(self, hdf):
15  pathinfo = hdf.getValue("CGI.PathInfo", "")
16 
17  pathparts = []
18  methodName = "list"
19  if pathinfo:
20  pathparts = string.split(pathinfo, "/")
21 
22  if len(pathparts):
23  methodName = pathparts[0]
24 
25  self.activate(hdf, methodName)
26 
27  def activate(self, hdf, methodName):
28  modpath, f = os.path.split(os.path.abspath(__file__) )
29  templatePath = os.path.join(modpath, "templates")
30  ##self.clearPaths()
31  #warn(templatePath)
32  self.setPaths([templatePath])
33 
34  method = getattr(self, methodName)
35 
36  self.pagename = methodName
37 
38  method(hdf)
39 
40  def list(self, hdf):
41  hdf.setValue("CGI.cur.title", "List %s Table" % self.tableName)
42 
43  self.__listTable(hdf, self.tableName, "CGI.cur.table")
44 
45 
46  def __listTable(self, hdf, tableName, prefix):
47  tbl = self.db[tableName]
48  tableColumnlist = tbl.getColumnList()
49 
50  columnlist = []
51  joins = []
52  for col, d, colinfo in tableColumnlist:
53  if colinfo.has_key("foreign_table"):
54  foreign_table = colinfo["foreign_table"]
55  joins.append(col)
56  columnlist.append(("_" + col + "." + "name", d, colinfo))
57  else:
58  columnlist.append((col, d, colinfo))
59  rows = tbl.fetchRows(join2=joins)
60 # rows = tbl.fetchRows()
61  self.__exportColumnHeaders(columnlist, hdf, prefix)
62  primaryKeys = tbl.getPrimaryKeyList()
63 
64  n = 0
65  for row in rows:
66  n = n + 1
67  aprefix = prefix + ".rows.%s" % n
68  self.__exportRow(row, columnlist, hdf, aprefix, primaryKeys)
69 
70  def __exportColumnHeaders(self, columnlist, hdf, prefix):
71  for col_name,col_type,options in columnlist:
72  col_name = col_name.replace(".", "_")
73  if options.get("no_export", 0): continue
74  aprefix = prefix + ".fields.%s" % col_name
75  hdf.setValue(aprefix + ".name", col_name)
76 
77  hdf.setValue(aprefix + ".col_type", col_type.odbType())
78  hdfhelp.hdfExportDict(aprefix + ".options", hdf, options)
79 
80 
81  def __exportRow(self, row, columnlist, hdf, prefix, primaryKeys):
82 
83  for col_name,col_type,options in columnlist:
84  if options.get("no_export", 0): continue
85 
86  a_col_name = col_name.replace(".", "_")
87  nprefix = prefix + ".values." + a_col_name
88  try:
89  value = row.get(col_name, "")
90  except odb.eNoSuchColumn:
91  value = ""
92  if value is not None:
93  hdf.setValue(nprefix, str(value))
94  else:
95  hdf.setValue(nprefix, "")
96 
97  ## generate key
98  key = []
99  for k in primaryKeys:
100  val = row.get(k, "")
101  if val:
102  key.append("%s=%s" % (k, str(val)))
103  hdf.setValue(prefix + ".key", string.join(key, "&"))
104 
105  def __findKeyFromQuery(self, hdf):
106  fields = {}
107  node = hdf.getObj("Query")
108  if node:
109  node = node.child()
110  while node:
111  if node.name() not in ("debug", ):
112  fields[node.name()] = node.value()
113  node = node.next()
114  fields = fields.items()
115  return fields
116 
117  def delete(self, hdf):
118  fields = self.__findKeyFromQuery(hdf)
119  tbl = self.db[self.tableName]
120  row = tbl.fetchRow(fields)
121  row.delete()
122 
123  self.redirectUri("list")
124 
125  def edit(self, hdf):
126  hdf.setValue("CGI.cur.action", "edit")
127 
128  self.show(hdf)
129 
130  tbl = self.db[self.tableName]
131  columnlist = tbl.getColumnList()
132  self.__exportColumnHeaders(columnlist, hdf, "CGI.cur.table")
133  hdf.setValue("CGI.cur.title", "Edit %s" % self.tableName)
134 
135  def Action_new(self, hdf):
136  tbl = self.db[self.tableName]
137  hdf.setValue("CGI.cur.action", "new")
138  self.pagename = "edit"
139 
140  row = tbl.newRow()
141 
142  node = hdf.getObj("Query.values")
143  if node:
144  node = node.child()
145  while node:
146  row[node.name()] = node.value()
147  node = node.next()
148 
149  row.save()
150  self.redirectUri("list")
151 
152  def getKeyFromQuery(self, hdf):
153  key = hdf.getValue("Query.key", "")
154  if not key: return []
155  parts = key.split("&")
156  keylist = []
157  for part in parts:
158  kvparts = part.split("=", 1)
159  keylist.append((kvparts[0], kvparts[1]))
160  return keylist
161 
162  def Action_edit(self, hdf):
163  hdf.setValue("CGI.cur.action", "edit")
164  dict = {}
165  node = hdf.getObj("Query.values")
166  if node:
167  node = node.child()
168  while node:
169  dict[node.name()] = node.value()
170  node = node.next()
171 
172  tbl = self.db[self.tableName]
173 
174  keylist = self.getKeyFromQuery(hdf)
175  row = tbl.fetchRow(keylist)
176 
177  for k,v in dict.items():
178  row[k] = v
179  row.save()
180  self.redirectUri("list")
181 
182  def new(self, hdf):
183  hdf.setValue("CGI.cur.title", "New %s Row" % self.tableName)
184 
185  hdf.setValue("CGI.cur.action", "new")
186  self.pagename = "edit"
187 
188  tbl = self.db[self.tableName]
189  prefix = "CGI.cur.row"
190  columnlist = tbl.getColumnList()
191  row = {}
192  primaryKeys = tbl.getPrimaryKeyList()
193  self.__exportRow(row, columnlist, hdf, "CGI.cur.row", primaryKeys)
194 
195  self.__exportColumnHeaders(columnlist, hdf, "CGI.cur.table")
196 
197 
198  def show(self, hdf):
199  hdf.setValue("CGI.cur.title", "Show %s" % self.tableName)
200 
201  fields = self.__findKeyFromQuery(hdf)
202 
203  tbl = self.db[self.tableName]
204  row = tbl.fetchRow(fields)
205 
206  columnlist = tbl.getColumnList()
207  primaryKeys = tbl.getPrimaryKeyList()
208  self.__exportRow(row, columnlist, hdf, "CGI.cur.row", primaryKeys)
209 
210 
211 
213  def setupDetails(self, db):
214  self.db = db
215 
216  def setup(self, hdf):
217  pathinfo = hdf.getValue("CGI.PathInfo", "")
218 
219  pathparts = []
220  self.tableName = "__db"
221  self.methodName = "listTables"
222 
223  if pathinfo:
224  pathparts = string.split(pathinfo, "/")
225 
226  self.methodName = "list"
227  if len(pathparts) >= 1:
228  self.tableName = pathparts[0]
229  if len(pathparts) == 2:
230  self.methodName = pathparts[1]
231  if not self.methodName:
232  self.methodName = "list"
233 
234  def display(self, hdf):
235 
236  if self.tableName == "__db":
237  if self.methodName == "listTables":
238  #tables = self.db.listTables()
239  tables = self.db.getTableList()
240  tables.sort()
241  hdfhelp.hdfExportList("CGI.cur.tables", hdf, tables)
242  Scaffold.display(self, hdf)
243  else:
244  s = Scaffold(self.context, self.methodName)
245  s._path_num = self._path_num
246  s.ncgi = self.ncgi
247 
248  self.pagename = self.methodName
249  s.setupDetails(self.db, self.tableName)
250  s.activate(hdf, self.methodName)
251  self.pagename = s.pagename
252 
253 
254  def list(self, hdf):
255  hdf.setValue("CGI.cur.title", "List DB Tables")
256  self.pagename = "tables"
257 
258 
259 
260 
261 def test():
262  pass
263 
264 def usage(progname):
265  print __doc__ % vars()
266 
267 def main(argv, stdout, environ):
268  progname = argv[0]
269  optlist, args = getopt.getopt(argv[1:], "", ["help", "test", "debug"])
270 
271  testflag = 0
272  if len(args) == 0:
273  usage(progname)
274  return
275  for (field, val) in optlist:
276  if field == "--help":
277  usage(progname)
278  return
279  elif field == "--debug":
280  debugfull()
281  elif field == "--test":
282  testflag = 1
283 
284  if testflag:
285  test()
286  return
287 
288 
289 if __name__ == "__main__":
290  main(sys.argv, sys.stdout, os.environ)
def redirectUri(self, redirectTo)
Definition: CSPage.py:112
def setPaths(self, paths)
Definition: CSPage.py:107
def usage(progname)
Definition: scaffold.py:264
def setupDetails(self, db, tableName)
Definition: scaffold.py:10
def __listTable(self, hdf, tableName, prefix)
Definition: scaffold.py:46
def __exportColumnHeaders(self, columnlist, hdf, prefix)
Definition: scaffold.py:70
def __findKeyFromQuery(self, hdf)
Definition: scaffold.py:105
def debugfull()
Definition: log.py:120
def main(argv, stdout, environ)
Definition: scaffold.py:267
def __exportRow(self, row, columnlist, hdf, prefix, primaryKeys)
Definition: scaffold.py:81
def activate(self, hdf, methodName)
Definition: scaffold.py:27
def getKeyFromQuery(self, hdf)
Definition: scaffold.py:152


pyclearsilver
Author(s): Scott Noob Hassan
autogenerated on Mon Jun 10 2019 15:51:13