scaffold.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 import os, sys, string, time, getopt
00004 from log import *
00005 
00006 import neo_cgi, neo_cs, neo_util
00007 import odb, hdfhelp, CSPage
00008 
00009 class Scaffold(CSPage.CSPage):
00010   def setupDetails(self, db, tableName):
00011     self.db = db
00012     self.tableName = tableName
00013 
00014   def display(self, hdf):
00015     pathinfo = hdf.getValue("CGI.PathInfo", "")
00016 
00017     pathparts = []
00018     methodName = "list"
00019     if pathinfo: 
00020       pathparts = string.split(pathinfo, "/")
00021 
00022       if len(pathparts):
00023         methodName = pathparts[0]
00024 
00025     self.activate(hdf, methodName)
00026 
00027   def activate(self, hdf, methodName):
00028     modpath, f = os.path.split(os.path.abspath(__file__) )
00029     templatePath = os.path.join(modpath, "templates")
00030     ##self.clearPaths()
00031     #warn(templatePath)
00032     self.setPaths([templatePath])
00033 
00034     method = getattr(self, methodName)
00035 
00036     self.pagename = methodName
00037 
00038     method(hdf)
00039 
00040   def list(self, hdf):
00041     hdf.setValue("CGI.cur.title", "List %s Table" % self.tableName)
00042 
00043     self.__listTable(hdf, self.tableName, "CGI.cur.table")
00044 
00045 
00046   def __listTable(self, hdf, tableName, prefix):
00047     tbl = self.db[tableName]
00048     tableColumnlist = tbl.getColumnList()
00049 
00050     columnlist = []
00051     joins = []
00052     for col, d, colinfo in tableColumnlist:
00053       if colinfo.has_key("foreign_table"):
00054         foreign_table = colinfo["foreign_table"]
00055         joins.append(col)
00056         columnlist.append(("_" + col + "." + "name", d, colinfo))
00057       else:
00058         columnlist.append((col, d, colinfo))
00059     rows = tbl.fetchRows(join2=joins)
00060 #    rows = tbl.fetchRows()
00061     self.__exportColumnHeaders(columnlist, hdf, prefix)
00062     primaryKeys = tbl.getPrimaryKeyList()
00063 
00064     n = 0
00065     for row in rows:
00066       n = n + 1
00067       aprefix = prefix + ".rows.%s" % n
00068       self.__exportRow(row, columnlist, hdf, aprefix, primaryKeys)
00069 
00070   def __exportColumnHeaders(self, columnlist, hdf, prefix):
00071     for col_name,col_type,options in columnlist:
00072       col_name = col_name.replace(".", "_")
00073       if options.get("no_export", 0): continue
00074       aprefix = prefix + ".fields.%s" % col_name
00075       hdf.setValue(aprefix + ".name", col_name)
00076 
00077       hdf.setValue(aprefix + ".col_type", col_type.odbType())
00078       hdfhelp.hdfExportDict(aprefix + ".options", hdf, options)
00079       
00080 
00081   def __exportRow(self, row, columnlist, hdf, prefix, primaryKeys):
00082 
00083     for col_name,col_type,options in columnlist:
00084       if options.get("no_export", 0): continue
00085 
00086       a_col_name = col_name.replace(".", "_")
00087       nprefix = prefix + ".values." + a_col_name
00088       try:
00089         value = row.get(col_name, "")
00090       except odb.eNoSuchColumn:
00091         value = ""
00092       if value is not None:
00093         hdf.setValue(nprefix, str(value))
00094       else:
00095         hdf.setValue(nprefix, "")
00096 
00097     ## generate key
00098     key = []
00099     for k in primaryKeys:
00100       val = row.get(k, "")
00101       if val:
00102         key.append("%s=%s" % (k, str(val)))
00103     hdf.setValue(prefix + ".key", string.join(key, "&"))
00104 
00105   def __findKeyFromQuery(self, hdf):
00106     fields = {}
00107     node = hdf.getObj("Query")
00108     if node:
00109       node = node.child()
00110       while node:
00111         if node.name() not in ("debug", ):
00112           fields[node.name()] = node.value()
00113         node = node.next()
00114     fields = fields.items()
00115     return fields
00116     
00117   def delete(self, hdf):
00118     fields = self.__findKeyFromQuery(hdf)
00119     tbl = self.db[self.tableName]
00120     row = tbl.fetchRow(fields)
00121     row.delete()
00122     
00123     self.redirectUri("list")
00124 
00125   def edit(self, hdf):
00126     hdf.setValue("CGI.cur.action", "edit")
00127 
00128     self.show(hdf)
00129 
00130     tbl = self.db[self.tableName]
00131     columnlist = tbl.getColumnList()
00132     self.__exportColumnHeaders(columnlist, hdf, "CGI.cur.table")
00133     hdf.setValue("CGI.cur.title", "Edit %s" % self.tableName)
00134 
00135   def Action_new(self, hdf):
00136     tbl = self.db[self.tableName]
00137     hdf.setValue("CGI.cur.action", "new")
00138     self.pagename = "edit"
00139 
00140     row = tbl.newRow()
00141     
00142     node = hdf.getObj("Query.values")
00143     if node:
00144       node = node.child()
00145       while node:
00146         row[node.name()] = node.value()
00147         node = node.next()
00148 
00149     row.save()
00150     self.redirectUri("list")
00151 
00152   def getKeyFromQuery(self, hdf):
00153     key = hdf.getValue("Query.key", "")
00154     if not key: return []
00155     parts = key.split("&")
00156     keylist = []
00157     for part in parts:
00158       kvparts = part.split("=", 1)
00159       keylist.append((kvparts[0], kvparts[1]))
00160     return keylist
00161 
00162   def Action_edit(self, hdf):
00163     hdf.setValue("CGI.cur.action", "edit")
00164     dict = {}
00165     node = hdf.getObj("Query.values")
00166     if node:
00167       node = node.child()
00168       while node:
00169         dict[node.name()] = node.value()
00170         node = node.next()
00171         
00172     tbl = self.db[self.tableName]
00173 
00174     keylist = self.getKeyFromQuery(hdf)
00175     row = tbl.fetchRow(keylist)
00176 
00177     for k,v in dict.items():
00178       row[k] = v
00179     row.save()
00180     self.redirectUri("list")
00181 
00182   def new(self, hdf):
00183     hdf.setValue("CGI.cur.title", "New %s Row" % self.tableName)
00184 
00185     hdf.setValue("CGI.cur.action", "new")
00186     self.pagename = "edit"
00187 
00188     tbl = self.db[self.tableName]
00189     prefix = "CGI.cur.row"
00190     columnlist = tbl.getColumnList()
00191     row = {}
00192     primaryKeys = tbl.getPrimaryKeyList()
00193     self.__exportRow(row, columnlist, hdf, "CGI.cur.row", primaryKeys)
00194 
00195     self.__exportColumnHeaders(columnlist, hdf, "CGI.cur.table")
00196 
00197 
00198   def show(self, hdf):
00199     hdf.setValue("CGI.cur.title", "Show %s" % self.tableName)
00200 
00201     fields = self.__findKeyFromQuery(hdf)
00202 
00203     tbl = self.db[self.tableName]
00204     row = tbl.fetchRow(fields)
00205 
00206     columnlist = tbl.getColumnList()
00207     primaryKeys = tbl.getPrimaryKeyList()
00208     self.__exportRow(row, columnlist, hdf, "CGI.cur.row", primaryKeys)
00209     
00210 
00211     
00212 class DBScaffold(Scaffold):
00213   def setupDetails(self, db):
00214     self.db = db
00215 
00216   def setup(self, hdf):
00217     pathinfo = hdf.getValue("CGI.PathInfo", "")
00218 
00219     pathparts = []
00220     self.tableName = "__db"
00221     self.methodName = "listTables"
00222 
00223     if pathinfo: 
00224       pathparts = string.split(pathinfo, "/")
00225 
00226       self.methodName = "list"
00227       if len(pathparts) >= 1:
00228         self.tableName = pathparts[0]
00229       if len(pathparts) == 2:
00230         self.methodName = pathparts[1]
00231         if not self.methodName:
00232           self.methodName = "list"
00233 
00234   def display(self, hdf):
00235 
00236     if self.tableName == "__db":
00237       if self.methodName == "listTables":
00238         #tables = self.db.listTables()
00239         tables = self.db.getTableList()
00240         tables.sort()
00241         hdfhelp.hdfExportList("CGI.cur.tables", hdf, tables)
00242         Scaffold.display(self, hdf)
00243     else:
00244       s = Scaffold(self.context, self.methodName)
00245       s._path_num = self._path_num
00246       s.ncgi = self.ncgi
00247 
00248       self.pagename = self.methodName
00249       s.setupDetails(self.db, self.tableName)
00250       s.activate(hdf, self.methodName)
00251       self.pagename = s.pagename
00252 
00253 
00254   def list(self, hdf):
00255     hdf.setValue("CGI.cur.title", "List DB Tables")
00256     self.pagename = "tables"
00257     
00258     
00259 
00260 
00261 def test():
00262   pass
00263 
00264 def usage(progname):
00265   print __doc__ % vars()
00266 
00267 def main(argv, stdout, environ):
00268   progname = argv[0]
00269   optlist, args = getopt.getopt(argv[1:], "", ["help", "test", "debug"])
00270 
00271   testflag = 0
00272   if len(args) == 0:
00273     usage(progname)
00274     return
00275   for (field, val) in optlist:
00276     if field == "--help":
00277       usage(progname)
00278       return
00279     elif field == "--debug":
00280       debugfull()
00281     elif field == "--test":
00282       testflag = 1
00283 
00284   if testflag:
00285     test()
00286     return
00287 
00288 
00289 if __name__ == "__main__":
00290   main(sys.argv, sys.stdout, os.environ)


pyclearsilver
Author(s): Scott Hassan/hassan@willowgarage.com
autogenerated on Sat Dec 28 2013 17:47:30