mirror of
https://github.com/qgis/QGIS.git
synced 2025-10-08 00:05:09 -04:00
with edit(layer)
raises an error if the commit fails
This commit is contained in:
parent
f64783493e
commit
8ab69e9386
@ -135,6 +135,13 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class QgsEditError(Exception):
|
||||||
|
def __init__(self, value):
|
||||||
|
self.value = value
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return repr(self.value)
|
||||||
|
|
||||||
# Define a `with edit(layer)` statement
|
# Define a `with edit(layer)` statement
|
||||||
|
|
||||||
class edit:
|
class edit:
|
||||||
@ -147,7 +154,8 @@ class edit:
|
|||||||
|
|
||||||
def __exit__(self, ex_type, ex_value, traceback):
|
def __exit__(self, ex_type, ex_value, traceback):
|
||||||
if ex_type is None:
|
if ex_type is None:
|
||||||
assert self.layer.commitChanges()
|
if not self.layer.commitChanges():
|
||||||
|
raise QgsEditError(self.layer.commitErrors())
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
self.layer.rollBack()
|
self.layer.rollBack()
|
||||||
|
@ -21,7 +21,8 @@ from utilities import (unittest,
|
|||||||
from qgis.core import (edit,
|
from qgis.core import (edit,
|
||||||
QgsFeature,
|
QgsFeature,
|
||||||
QgsGeometry,
|
QgsGeometry,
|
||||||
QgsVectorLayer
|
QgsVectorLayer,
|
||||||
|
QgsEditError
|
||||||
)
|
)
|
||||||
|
|
||||||
getQgisTestApp()
|
getQgisTestApp()
|
||||||
@ -72,5 +73,10 @@ class TestSyntacticSugar(TestCase):
|
|||||||
|
|
||||||
assert ml.dataProvider().getFeatures().next()['value']==10
|
assert ml.dataProvider().getFeatures().next()['value']==10
|
||||||
|
|
||||||
|
# Check that we get a QgsEditError exception when the commit fails
|
||||||
|
with self.assertRaises(QgsEditError):
|
||||||
|
with edit(ml) as l:
|
||||||
|
l.rollBack()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user