dabo Commit
Revision 4057
Date: 2008-05-02 07:21:25 -0700 (Fri, 02 May 2008)
Author: Ed
Trac: http://svn.dabodev.com/trac/dabo/changeset/4057
Changed:
U trunk/dabo/biz/dBizobj.py
Log:
This commit contains many changes related to the way that child bizobjs sync up
with their parent. There were many places in the code where it was assumed that
the link was via the parent's PK; while that is true in the majority of cases,
child bizobjs can be linked via any field, not just the PK. I added a new
method named 'getParentLinkValue()' that will return the value of the field in
the parent record that defines the link to the child.
Please test this with all your code. It is working for all of my apps and the
unit tests, but I don't have anything that links a child by anything other than
the parent PK.
Diff:
Modified: trunk/dabo/biz/dBizobj.py
===================================================================
--- trunk/dabo/biz/dBizobj.py 2008-05-01 17:51:50 UTC (rev 4056)
+++ trunk/dabo/biz/dBizobj.py 2008-05-02 14:21:25 UTC (rev 4057)
@@ -847,16 +847,12 @@
so an empty query is built.
"""
if self.DataSource and self.LinkField and self.Parent:
- if self.Parent.IsAdding or self.Parent.RowCount == 0:
+ if self.Parent.RowCount == 0:
# Parent is new and not yet saved, so we cannot
have child records yet.
self.setWhereClause("")
filtExpr = " 1 = 0 "
else:
- if self.ParentLinkField:
- # The link to the parent is something
other than the PK
- val =
self.escQuote(self.Parent.getFieldVal(self.ParentLinkField))
- else:
- val = self.escQuote(self.getParentPK())
+ val = self.escQuote(self.getParentLinkValue())
linkFieldParts = self.LinkField.split(".")
if len(linkFieldParts) < 2:
dataSource = self.DataSource
@@ -869,6 +865,23 @@
self._CurrentCursor.setChildFilterClause(filtExpr)
+ def getParentLinkValue(self):
+ """Return the value of the parent record on which this bizobj
is dependent. Usually this
+ is the PK of the parent, but can be a non-PK field, if this
bizobj's ParentLinkField is
+ not empty.
+ """
+ ret = None
+ if self.Parent:
+ fld = self.ParentLinkField
+ if not fld:
+ fld = self.Parent.KeyField
+ try:
+ ret = self.Parent.getFieldVal(fld)
+ except dException.NoRecordsException:
+ ret = NO_RECORDS_PK
+ return ret
+
+
def sort(self, col, ord=None, caseSensitive=True):
""" Sort the rows based on values in a specified column.
@@ -970,10 +983,9 @@
"""
self._CurrentCursor.moveToRowNum(rownum)
if updateChildren:
- pk = self.getPK()
for child in self.__children:
- # Let the child know the current dependent PK
- child.setCurrentParent(pk)
+ # Let the child update to the current record.
+ child.setCurrentParent()
def _positionUsingPK(self, pk, updateChildren=True):
@@ -985,8 +997,8 @@
self._CurrentCursor.moveToPK(pk)
if updateChildren:
for child in self.__children:
- # Let the child know the current
dependent PK
- child.setCurrentParent(pk)
+ # Let the child update to the current
record.
+ child.setCurrentParent()
def moveToPK(self, pk):
@@ -1021,15 +1033,16 @@
return ret
- def isAnyChanged(self, parentPK=None):
+ def isAnyChanged(self, useCurrentParent=None):
"""Returns True if any record in the current record set has
been changed."""
- if parentPK is None:
+ if useCurrentParent is None:
try:
cc = self._CurrentCursor
except:
cc = None
else:
- cc = self.__cursors.get(parentPK, None)
+ key = self.getParentLinkValue()
+ cc = self.__cursors.get(key, None)
if cc is None:
# No cursor, no changes.
return False
@@ -1038,14 +1051,8 @@
return True
# Nothing's changed in the top level, so we need to recurse the
children:
- try:
- pk = self.getPK()
- except dException.NoRecordsException:
- # If there are no records, there can be no changes
- return False
-
for child in self.__children:
- if child.isAnyChanged(parentPK=pk):
+ if
child.isAnyChanged(useCurrentParent=useCurrentParent):
return True
# If we made it to here, there are no changes.
return False
@@ -1068,13 +1075,11 @@
if not ret:
# see if any child bizobjs have changed
- try:
- pk = self.getPK()
- except dException.NoRecordsException:
+ if not self.RowCount:
# If there are no records, there can be no
changes
return False
for child in self.__children:
- ret = child.isAnyChanged(parentPK=pk)
+ ret = child.isAnyChanged(useCurrentParent=True)
if ret:
break
return ret
@@ -1148,20 +1153,20 @@
"""
if self.LinkField:
if val is None:
- val = self.getParentPK()
+ val = self.getParentLinkValue()
self.scan(self._setParentFK, val)
def _setParentFK(self, val):
self.setFieldVal(self.LinkField, val)
- def setCurrentParent(self, val=None, fromChildRequery=None):
- """ Lets dependent child bizobjs know the current value of
their parent
+ def setCurrentParent(self, val=None):
+ """ Lets dependent child bizobjs update to the current parent
record.
"""
if self.LinkField:
- if val is None and not fromChildRequery:
- val = self.getParentPK()
+ if val is None:
+ val = self.getParentLinkValue()
# Update the key value for the cursor
self.__currentCursorKey = val
# Make sure there is a cursor object for this key.
@@ -1263,24 +1268,11 @@
if errMsg:
raise dException.BusinessRuleViolation, errMsg
- newAutopop = (self.IsAdding and self.AutoPopulatePK)
- try:
- pk = self.getPK()
- except dException.NoRecordsException:
- # There aren't any records, all children should requery
to 0 records.
- # We can't set the pk to None, because None has special
meaning
- # elsewhere (self.__currentCursorKey).
- pk = NO_RECORDS_PK
-
for child in self.__children:
# Let the child know the current dependent PK
if child.RequeryWithParent:
- child.setCurrentParent(pk,
fromChildRequery=True)
- if newAutopop and (child.RowCount == 0):
- parentPK = None
- else:
- parentPK = pk
- if not child.isAnyChanged(parentPK=parentPK):
+ child.setCurrentParent()
+ if not
child.isAnyChanged(useCurrentParent=True):
child.requery()
self.afterChildRequery()
_______________________________________________
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]