dabo Commit
Revision 5433
Date: 2009-09-26 15:48:41 -0700 (Sat, 26 Sep 2009)
Author: Ed
Trac: http://trac.dabodev.com/changeset/5433
Changed:
U trunk/dabo/biz/dBizobj.py
U trunk/dabo/db/dCursorMixin.py
U trunk/dabo/db/dDataSet.py
U trunk/dabo/ui/__init__.py
Log:
Overhauled the replace() functionality. There were several problems, with the
most glaring being how slow it ran. I improved that, but then discovered
something: after a replace(), neither cancel() nor save() touched those
changes, as they were not recorded in the memento framework. So I modified it
again, this time working with bizobj.setFieldVal() so that modifications create
the proper mementos. It's not as fast as my first change, but it's still much
faster than it was before.
Diff:
Modified: trunk/dabo/biz/dBizobj.py
===================================================================
--- trunk/dabo/biz/dBizobj.py 2009-09-26 17:42:02 UTC (rev 5432)
+++ trunk/dabo/biz/dBizobj.py 2009-09-26 22:48:41 UTC (rev 5433)
@@ -37,10 +37,9 @@
# now the DefaultValues property (used to be self.defaultValues
attribute)
self._defaultValues = {}
- # PKId's of rows to be filtered out when
- # filtering Virtual fields
+ # PKs of rows to be filtered out when filtering Virtual fields
self.__filterPKVirtual = []
-
+
self._beforeInit()
self.setConnection(conn)
# We need to make sure the cursor is created *before* the call
to
@@ -808,6 +807,23 @@
return tuple([ff[0] for ff in flds])
+ def _fldReplace(self, expr):
+ """Takes a user-defined, SQL-like expression, and substitutes
any
+ field name with the reference for that value in the bizobj.
+ Example (assuming 'price' is a column in the data):
+ self._fldReplace("price > 50")
+ => returns "self.Record.price > 50"
+ """
+ patTemplate = r"(.*\b)%s(\b.*)"
+ ret = expr
+ for fld in self.getFieldNames():
+ pat = patTemplate % fld
+ mtch = re.match(pat, ret)
+ if mtch:
+ ret = mtch.groups()[0] + "self.Record.%s" % fld
+ mtch.groups()[1]
+ return ret
+
+
def replace(self, field, valOrExpr, scope=None):
"""Replaces the value of the specified field with the given
value
or expression. All records matching the scope are affected; if
@@ -817,9 +833,31 @@
with an equals sign. All expressions will therefore be a string
beginning with '='. Literals can be of any type.
"""
- self._CurrentCursor.replace(field, valOrExpr, scope=scope)
+ self.scan(self._replace, field, valOrExpr, scope)
+ def _replace(self, field, valOrExpr, scope):
+ """Called once for each record in the bizobj when the replace()
method
+ is invoked.
+ """
+ if scope is not None:
+ scope = self._fldReplace(scope)
+ if not eval(scope):
+ return
+ literal = True
+ try:
+ if valOrExpr.startswith("="):
+ literal = False
+ valOrExpr = valOrExpr.strip()[1:]
+ valOrExpr = self._fldReplace(valOrExpr)
+ print "VOEX", valOrExpr
+ valOrExpr = eval(valOrExpr)
+ except AttributeError:
+ # Not a string expression; no worries
+ pass
+ self.setFieldVal(field, valOrExpr)
+
+
def new(self):
""" Create a new record and populate it with default values.
Default
values are specified in the DefaultValues dictionary.
Modified: trunk/dabo/db/dCursorMixin.py
===================================================================
--- trunk/dabo/db/dCursorMixin.py 2009-09-26 17:42:02 UTC (rev 5432)
+++ trunk/dabo/db/dCursorMixin.py 2009-09-26 22:48:41 UTC (rev 5433)
@@ -1211,6 +1211,11 @@
'valOrExpr' will be treated as a literal value, unless it is
prefixed
with an equals sign. All expressions will therefore be a string
beginning with '='. Literals can be of any type.
+
+ NOTE: this does NOT work with the memento framework for
+ determining modified records. It is strongly recommended that
+ instead of calling this directly that the bizobj.replace()
method
+ be used in any programming.
"""
# Make sure that the data set object has any necessary
references
self._records.Cursor = self
Modified: trunk/dabo/db/dDataSet.py
===================================================================
--- trunk/dabo/db/dDataSet.py 2009-09-26 17:42:02 UTC (rev 5432)
+++ trunk/dabo/db/dDataSet.py 2009-09-26 22:48:41 UTC (rev 5433)
@@ -105,9 +105,7 @@
Scope is a boolean expression.
"""
- if scope is None:
- scope = "True"
- else:
+ if scope is not None:
scope = self._fldReplace(scope, "rec")
literal = True
@@ -116,11 +114,17 @@
literal = False
valOrExpr = valOrExpr.replace("=", "", 1)
valOrExpr = self._fldReplace(valOrExpr, "rec")
- for rec in self:
- if eval(scope):
- if literal:
- rec[field] = valOrExpr
- else:
+ if literal:
+ upDict = {field: valOrExpr}
+ if scope is None:
+ [rec.update(upDict) for rec in self]
+ else:
+ [rec.update(upDict) for rec in self
+ if eval(scope)]
+ else:
+ # Need to go record-by-record so that the expression
evaluates correctly
+ for rec in self:
+ if eval(scope):
rec[field] = eval(valOrExpr)
Modified: trunk/dabo/ui/__init__.py
===================================================================
--- trunk/dabo/ui/__init__.py 2009-09-26 17:42:02 UTC (rev 5432)
+++ trunk/dabo/ui/__init__.py 2009-09-26 22:48:41 UTC (rev 5433)
@@ -57,10 +57,9 @@
try:
exec("from %s import *" % mods[typ], globals())
retVal = True
- except Exception, e:
+ except Exception:
retVal = False
# Record the actual problem
- #dabo.errorLog.write("Error Loading UI: %s" % e)
traceback.print_exc()
else:
if currType == typ:
_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-dev
Searchable Archives: http://leafe.com/archives/search/dabo-dev
This message:
http://leafe.com/archives/byMID/[email protected]