Changeset 3074

Show
Ignore:
Timestamp:
04/21/07 05:49:42 (2 years ago)
Author:
ed
Message:

Added the 'SaveNewUnchanged?' property to dBizobj. When this is True, new records that have not been modified from their defaults are saved; the default remains to not insert such records. The code was also changed so that cancel() recognizes these records as 'changed' and in need of being canceled.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/dabo/biz/dBizobj.py

    r3070 r3074  
    264264         
    265265        try: 
    266             self.scanChangedRows(self.save, startTransaction=False, topLevel=False) 
     266            self.scanChangedRows(self.save, includeNewUnchanged=self.SaveNewUnchanged, 
     267                    startTransaction=False, topLevel=False) 
    267268        except dException.ConnectionLostException, e: 
    268269            self.RowNumber = current_row 
     
    366367 
    367368    def cancelAll(self, ignoreNoRecords=None): 
    368         """Cancel all changes made to the current dataset, including all children.""" 
    369         self.scanChangedRows(self.cancel, allCursors=False, ignoreNoRecords=ignoreNoRecords) 
     369        """Cancel all changes made to the current dataset, including all children 
     370        and all new, unmodified records. 
     371        """ 
     372        self.scanChangedRows(self.cancel, allCursors=False, includeNewUnchanged=True, 
     373                ignoreNoRecords=ignoreNoRecords) 
    370374 
    371375 
     
    497501 
    498502 
    499     def getChangedRows(self): 
    500         """ Returns a list of row numbers for which isChanged()    returns True. The  
     503    def getChangedRows(self, includeNewUnchanged=False): 
     504        """ Returns a list of row numbers for which isChanged() returns True. The  
    501505        changes may therefore not be in the record itself, but in a dependent child  
    502         record. 
     506        record. If includeNewUnchanged is True, the presence of a new unsaved 
     507        record that has not been modified from its defaults will suffice to mark the 
     508        record as changed. 
    503509        """ 
    504510        if self.__children: 
     
    509515        else: 
    510516            # Can use the much faster cursor.getChangedRows(): 
    511             return self._CurrentCursor.getChangedRows(
     517            return self._CurrentCursor.getChangedRows(includeNewUnchanged
    512518 
    513519 
     
    587593         
    588594 
    589     def scanChangedRows(self, func, allCursors=False, *args, **kwargs): 
     595    def scanChangedRows(self, func, allCursors=False, includeNewUnchanged=False, 
     596            *args, **kwargs): 
    590597        """Move the record pointer to each changed row, and call func. 
    591598 
    592599        If allCursors is True, all other cursors for different parent records will  
    593         be iterated as well.  
     600        be iterated as well. 
     601         
     602        If includeNewUnchanged is True, new unsaved records that have not been 
     603        edited from their default values will be counted as 'changed'. 
    594604 
    595605        If you want to end the scan on the next iteration, set self.exitScan=True. 
     
    614624        for key, cursor in cursors.iteritems(): 
    615625            self._CurrentCursor = key 
    616             changedRows = self.getChangedRows(
     626            changedRows = self.getChangedRows(includeNewUnchanged
    617627            for row in changedRows: 
    618628                self._moveToRowNum(row) 
     
    942952            return False 
    943953         
    944         if cc.isChanged(allRows=True): 
     954        if cc.isChanged(allRows=True, includeNewUnchanged=self.SaveNewUnchanged): 
    945955            return True 
    946956     
     
    972982            # No cursor, no changes. 
    973983            return False 
    974         ret = cc.isChanged(allRows=False
     984        ret = cc.isChanged(allRows=False, includeNewUnchanged=self.SaveNewUnchanged
    975985 
    976986        if not ret: 
     
    17191729 
    17201730 
     1731    def _getSaveNewUnchanged(self): 
     1732        try: 
     1733            ret = self._saveNewUnchanged 
     1734        except AttributeError: 
     1735            ret = self._saveNewUnchanged = False 
     1736        return ret 
     1737 
     1738    def _setSaveNewUnchanged(self, val): 
     1739        self._saveNewUnchanged = val 
     1740 
     1741 
    17211742    def _getSQL(self): 
    17221743        try: 
     
    18701891            _("The current position of the record pointer in the result set. (int)")) 
    18711892 
     1893    SaveNewUnchanged = property(_getSaveNewUnchanged, _setSaveNewUnchanged, None, 
     1894            _("""Normally new, unmodified records are not saved. If you need  
     1895            this behavior, set this to True.  (bool)""")) 
     1896     
    18721897    SQL = property(_getSQL, _setSQL, None, 
    18731898            _("SQL statement used to create the cursor's data. (str)")) 
  • trunk/dabo/db/dCursorMixin.py

    r3066 r3074  
    610610     
    611611 
    612     def isChanged(self, allRows=True): 
     612    def isChanged(self, allRows=True, includeNewUnchanged=False): 
    613613        """Return True if there are any changes to the local field values. 
    614614 
    615615        If allRows is True (the default), all records in the recordset will be  
    616616        considered. Otherwise, only the current record will be checked. 
     617         
     618        If includeNewUnchanged is True, new records that have not been 
     619        modified from their default values, which normally are not  
     620        considered 'changed', will be counted as 'changed'. 
    617621        """ 
    618622        if allRows: 
    619             #return len(self._mementos) > 0 or len(self._newRecords) > 0 
    620             return len(self._mementos) > 0 
     623            if includeNewUnchanged: 
     624                return (len(self._mementos) > 0) or (len(self._newRecords) > 0) 
     625            else: 
     626                return len(self._mementos) > 0 
    621627        else: 
    622628            row = self.RowNumber 
     
    628634                return False 
    629635            recKey = self.pkExpression(rec) 
    630             memento = self._mementos.get(recKey, None) 
    631             return bool(memento) 
     636            modrec = self._mementos.get(recKey, None) 
     637            if not modrec and includeNewUnchanged: 
     638                modrec = self._newRecords.get(recKey, None) 
     639            return bool(modrec) 
    632640 
    633641 
     
    13901398 
    13911399 
    1392     def getChangedRows(self): 
     1400    def getChangedRows(self, includeNewUnchanged=False): 
    13931401        """Returns a list of rows with changes.""" 
    1394         return map(self._getRowByPk, self._mementos.keys()) 
     1402        chKeys = self._mementos.keys() 
     1403        if includeNewUnchanged: 
     1404            # We need to also count all new records 
     1405            chKeys = dict.fromkeys(chKeys + self._newRecords.keys()).keys() 
     1406        return map(self._getRowByPk, chKeys) 
    13951407         
    13961408