Skip to content
Permalink
Browse files

Fix QgsVectorLayer::updateFeature returns true when errors occur

Refs #17678
  • Loading branch information
nyalldawson committed Jan 3, 2018
1 parent c94d26a commit 5ecb5603df41be3c2678f72abbdaec4163727ebd
Showing with 40 additions and 8 deletions.
  1. +15 −8 src/core/qgsvectorlayer.cpp
  2. +25 −0 tests/src/python/test_qgsvectorlayer.py
@@ -974,13 +974,16 @@ bool QgsVectorLayer::addFeature( QgsFeature &feature, Flags )

bool QgsVectorLayer::updateFeature( const QgsFeature &updatedFeature, bool skipDefaultValues )
{
bool hasChanged = false;
bool hasError = false;
if ( !mEditBuffer || !mDataProvider )
{
return false;
}

QgsFeature currentFeature = getFeature( updatedFeature.id() );
if ( currentFeature.isValid() )
{
QgsDebugMsgLevel( QStringLiteral( "feature %1 could not be retrieved" ).arg( updatedFeature.id() ), 3 );
bool hasChanged = false;
bool hasError = false;

if ( updatedFeature.hasGeometry() && currentFeature.hasGeometry() && !updatedFeature.geometry().isGeosEqual( currentFeature.geometry() ) )
{
@@ -1012,12 +1015,16 @@ bool QgsVectorLayer::updateFeature( const QgsFeature &updatedFeature, bool skipD
}
}
}
}

if ( hasChanged && !mDefaultValueOnUpdateFields.isEmpty() && !skipDefaultValues )
updateDefaultValues( updatedFeature.id(), updatedFeature );
if ( hasChanged && !mDefaultValueOnUpdateFields.isEmpty() && !skipDefaultValues )
updateDefaultValues( updatedFeature.id(), updatedFeature );

return !hasError;
return !hasError;
}
else
{
QgsDebugMsgLevel( QStringLiteral( "feature %1 could not be retrieved" ).arg( updatedFeature.id() ), 3 );
return false;
}
}


@@ -721,6 +721,31 @@ def checkBefore():
# print "COMMIT ERRORS:"
# for item in list(layer.commitErrors()): print item

# updateFeature

def testUpdateFeature(self):
layer = createLayerWithFivePoints()
features = [f for f in layer.getFeatures()]

# try to change feature without editing mode
self.assertFalse(layer.updateFeature(features[0]))

layer.startEditing()

# no matching feature
f = QgsFeature(1123)
self.assertFalse(layer.updateFeature(f))

# change geometry and attributes
f = features[0]
f.setAttributes(['new',321])
f.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(-200, -200)))
self.assertTrue(layer.updateFeature(f))

new_feature = next(layer.getFeatures(QgsFeatureRequest(f.id())))
self.assertEqual(new_feature.attributes(), ['new',321])
self.assertEqual(new_feature.geometry().asPoint(), QgsPointXY(-200, -200))

# ADD ATTRIBUTE

def test_AddAttribute(self):

0 comments on commit 5ecb560

Please sign in to comment.
You can’t perform that action at this time.