dabo Commit
Revision 4923
Date: 2009-01-11 20:19:23 -0800 (Sun, 11 Jan 2009)
Author: Ed
Trac: http://trac.dabodev.com/changeset/4923

Changed:
U   trunk/dabo/biz/dBizobj.py
U   trunk/dabo/db/dCursorMixin.py

Log:
Added the 'cloneRecord()' method to dBizobj. This will make a copy of the 
current record and append it as a new record in the dataset. The copy will 
contain all of the columns of the current record except for PK columns.

Added the 'appendDataSet()' method to dCursorMixin. This assumes that the 
dataset to be appended has the same table structure as the current dataset; if 
not, an error is raised.

Added the 'isRemote()' method to dBizobj. This returns True if the connection 
for the bizobj is a remote web connection.

Implemented remote proxy support in the getFieldNames() method.


Diff:
Modified: trunk/dabo/biz/dBizobj.py
===================================================================
--- trunk/dabo/biz/dBizobj.py   2009-01-12 00:48:10 UTC (rev 4922)
+++ trunk/dabo/biz/dBizobj.py   2009-01-12 04:19:23 UTC (rev 4923)
@@ -775,6 +775,9 @@
 
        def getFieldNames(self):
                """Returns a tuple of all the field names in the cursor."""
+               rp = self._RemoteProxy
+               if rp:
+                       return rp.getFieldNames()
                flds = self._CurrentCursor.getFields()
                # This is a tuple of 3-tuples; we just want the names
                return tuple([ff[0] for ff in flds])
@@ -1153,7 +1156,7 @@
                pass
 
 
-       def _onNew(self):
+       def _onNew(self, setDefaults=True):
                """ Populate the record with any default values.
 
                User subclasses should leave this alone and instead override 
onNew().
@@ -1168,7 +1171,8 @@
                                self.__currentCursorKey = tmpKey
                                del self.__cursors[currKey]
                                self.__cursors[tmpKey] = cursor
-               cursor.setDefaults(self.DefaultValues)
+               if setDefaults:
+                       cursor.setDefaults(self.DefaultValues)
                cursor.setNewFlag()
                # Fill in the link to the parent record
                if self.Parent and self.FillLinkFromParent and self.LinkField:
@@ -1363,7 +1367,10 @@
                                        ch.requery()
                        
                if cursor is not None:
-                       ret = cursor.getFieldVal(fld, row, 
_rowChangeCallback=changeRowNumCallback)
+                       try:
+                               ret = cursor.getFieldVal(fld, row, 
_rowChangeCallback=changeRowNumCallback)
+                       except dException.RowNotFoundException:
+                               return None
 
                if oldRow != self.RowNumber:
                        self._moveToRowNum(oldRow)
@@ -1376,7 +1383,7 @@
                if cursor is not None:
                        try:
                                ret = cursor.setFieldVal(fld, val, row)
-                       except dException.NoRecordsException:
+                       except (dException.NoRecordsException, 
dException.RowNotFoundException):
                                ret = False
                return ret
 
@@ -1484,14 +1491,24 @@
                it is the responsibility of the caller to make sure that they 
match. If invalid data is
                passed, a dException.FieldNotFoundException will be raised.
                """
-               for rec in ds:
-                       self.new()
-                       for col, val in rec.items():
-                               if self.AutoPopulatePK and (col == 
self.KeyField):
-                                       continue
-                               self.setFieldVal(col, val)
+               self._CurrentCursor.appendDataSet(ds)
 
 
+       def cloneRecord(self):
+               """Creates a copy of the current record and adds it to the 
dataset. The KeyField
+               is not copied.
+               """
+               self._CurrentCursor.cloneRecord()
+               self._onNew(setDefaults=False)
+
+
+       def isRemote(self):
+               """Returns True/False, depending on whether this bizobj's 
connection
+               is remote or not.
+               """
+               return self._connection.isRemote()
+
+
        def getDataTypes(self):
                """Returns the field type definitions as set in the cursor."""
                return self._CurrentCursor.getDataTypes()
@@ -1800,7 +1817,8 @@
                crs.AutoQuoteNames = self._autoQuoteNames
                if self._dataStructure is not None:
                        crs.DataStructure = self._dataStructure
-               crs.Table = self._dataSource
+               if not self._RemoteProxy:
+                       crs.Table = self._dataSource
                crs.UserSQL = self._userSQL
                crs.VirtualFields = self._virtualFields
                crs.Encoding = self.Encoding
@@ -2045,7 +2063,7 @@
 
 
        def _getRemoteProxy(self):
-               if self._connection.isRemote():
+               if self.isRemote():
                        try:
                                return self._remoteProxy
                        except AttributeError:

Modified: trunk/dabo/db/dCursorMixin.py
===================================================================
--- trunk/dabo/db/dCursorMixin.py       2009-01-12 00:48:10 UTC (rev 4922)
+++ trunk/dabo/db/dCursorMixin.py       2009-01-12 04:19:23 UTC (rev 4923)
@@ -811,7 +811,11 @@
                if row is None:
                        row = self.RowNumber
 
-               rec = self._records[row]
+               try:
+                       rec = self._records[row]
+               except IndexError:
+                       raise dException.RowNotFoundException(
+                                       _("Row #%s requested, but the data set 
has only %s row(s),") % (row, len(self._records)))
                if isinstance(fld, (tuple, list)):
                        ret = []
                        for xFld in fld:
@@ -894,7 +898,11 @@
                if row is None:
                        row = self.RowNumber
 
-               rec = self._records[row]
+               try:
+                       rec = self._records[row]
+               except IndexError:
+                       raise dException.RowNotFoundException(
+                                       _("Row #%s requested, but the data set 
has only %s row(s),") % (row, len(self._records)))
                valid_pk = self._hasValidKeyField()
                keyField = self.KeyField
                if fld not in rec:
@@ -1104,6 +1112,38 @@
                return dDataSet(ds)
 
 
+       def appendDataSet(self, ds):
+               """Appends the rows in the passed dataset to this cursor's 
dataset. No checking
+               is done on the dataset columns to make sure that they are 
correct for this cursor;
+               it is the responsibility of the caller to make sure that they 
match. If invalid data is
+               passed, a dException.FieldNotFoundException will be raised.
+               """
+               kf = self.KeyField
+               if not isinstance(kf, tuple):
+                       kf = (kf, )
+               for rec in ds:
+                       self.new()
+                       for col, val in rec.items():
+                               if self.AutoPopulatePK and (col in kf):
+                                       continue
+                               self.setFieldVal(col, val)
+
+
+       def cloneRecord(self):
+               """Creates a copy of the current record and adds it to the 
dataset."""
+               if not self.RowCount:
+                       raise dException.NoRecordsException(_("No records in 
the data set."))
+               rec = self._records[self.RowNumber]
+               if self.AutoPopulatePK:
+                       kf = self.KeyField
+                       blank = self._getBlankRecord()
+                       if not isinstance(kf, tuple):
+                               kf = (kf, )
+                       for fld in kf:
+                               rec[fld] = blank[fld]
+               self.appendDataSet((rec, ))
+
+
        def getDataTypes(self):
                """Returns the internal _types dict."""
                return self._types
@@ -1427,11 +1467,18 @@
                return self.BackendObject.pregenPK(self.AuxCursor)
 
 
+       def _getBlankRecord(self):
+               """Returns a record template, with each field set to the 
'blank' value 
+               for that data type.
+               """
+               if not self._blank:
+                       self.__setStructure()
+               return self._blank.copy()
+
+
        def new(self):
                """Add a new record to the data set."""
-               if not self._blank:
-                       self.__setStructure()
-               blank = self._blank.copy()
+               blank = self._getBlankRecord()
                self._records += dDataSet((blank,))
                # Adjust the RowCount and position
                self.RowNumber = self.RowCount - 1




_______________________________________________
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]

Reply via email to