From 1adb7b3c38641c2e9e346d21555214e7b320b836 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Mon, 11 Mar 2013 20:03:17 +0100 Subject: [PATCH] Ported layermapping for autocommit. --- django/contrib/gis/utils/layermapping.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/django/contrib/gis/utils/layermapping.py b/django/contrib/gis/utils/layermapping.py index 51f70f5350..248afb5808 100644 --- a/django/contrib/gis/utils/layermapping.py +++ b/django/contrib/gis/utils/layermapping.py @@ -59,11 +59,6 @@ class LayerMapping(object): models.PositiveSmallIntegerField : (OFTInteger, OFTReal, OFTString), } - # The acceptable transaction modes. - TRANSACTION_MODES = {'autocommit' : transaction.autocommit, - 'commit_on_success' : transaction.commit_on_success, - } - def __init__(self, model, data, mapping, layer=0, source_srs=None, encoding='utf-8', transaction_mode='commit_on_success', @@ -127,9 +122,11 @@ class LayerMapping(object): # Setting the transaction decorator with the function in the # transaction modes dictionary. - if transaction_mode in self.TRANSACTION_MODES: - self.transaction_decorator = self.TRANSACTION_MODES[transaction_mode] - self.transaction_mode = transaction_mode + self.transaction_mode = transaction_mode + if transaction_mode == 'autocommit': + self.transaction_decorator = None + elif transaction_mode == 'commit_on_success': + self.transaction_decorator = transaction.atomic else: raise LayerMapError('Unrecognized transaction mode: %s' % transaction_mode) @@ -501,9 +498,6 @@ class LayerMapping(object): else: progress_interval = progress - # Defining the 'real' save method, utilizing the transaction - # decorator created during initialization. - @self.transaction_decorator def _save(feat_range=default_range, num_feat=0, num_saved=0): if feat_range: layer_iter = self.layer[feat_range] @@ -572,6 +566,9 @@ class LayerMapping(object): # values returned here. return num_saved, num_feat + if self.transaction_decorator is not None: + _save = self.transaction_decorator(_save) + nfeat = self.layer.num_feat if step and isinstance(step, int) and step < nfeat: # Incremental saving is requested at the given interval (step)