1- import warnings
2- from functools import update_wrapper
1+ import logging
2+ from functools import update_wrapper , wraps
33from django .conf import settings
4- from django .db import DatabaseError , connections , router
4+ from django .db import connections , router
55from django .utils .translation import ugettext as _
6+
7+ logger = logging .getLogger ('concurrency' )
8+
69from concurrency .exceptions import VersionChangedError , RecordModifiedError , InconsistencyError
10+ from concurrency .utils import deprecated
11+
712
813__all__ = []
914
10- def deprecate (target , subst , version ):
11- warnings .warn ("`{0}` will be removed in version `{2}`. Please use `{1}`" .format (target , subst , version ),
12- category = DeprecationWarning )
1315
16+ @deprecated ('concurrency.api.apply_concurrency_check' , '0.5' )
1417def apply_concurrency_check (model , fieldname , versionclass ):
1518 from concurrency .api import apply_concurrency_check as acc
1619 return acc (model , fieldname , versionclass )
1720
1821
22+ @deprecated ('concurrency.api.apply_concurrency_check' , '0.5' )
1923def concurrency_check (model_instance , force_insert = False , force_update = False , using = None , ** kwargs ):
2024 from concurrency .api import concurrency_check as cc
2125 return cc (model_instance , force_insert , force_update , using , ** kwargs )
@@ -36,6 +40,14 @@ def _select_lock(model_instance, version_value=None):
3640 raise InconsistencyError (_ ('Version field is set (%s) but record has not `pk`.' % value ))
3741
3842
43+ def _wrap_model_save (model , force = False ):
44+ if force or not model .RevisionMetaInfo .versioned_save :
45+ logger .debug ('Wrapping save method of %s' % model )
46+ old_save = getattr (model , 'save' )
47+ setattr (model , 'save' , _wrap_save (old_save ))
48+ model .RevisionMetaInfo .versioned_save = True
49+
50+
3951def _wrap_save (func ):
4052 def inner (self , force_insert = False , force_update = False , using = None , ** kwargs ):
4153 concurrency_check (self , force_insert , force_update , using , ** kwargs )
@@ -47,18 +59,11 @@ def inner(self, force_insert=False, force_update=False, using=None, **kwargs):
4759def _versioned_save (self , force_insert = False , force_update = False , using = None ):
4860 if force_insert and force_update :
4961 raise ValueError ("Cannot force both insert and updating in model saving." )
50- concurrency_check (self , force_insert , force_update , using )
62+ if not force_insert :
63+ _select_lock (self )
5164 self .save_base (using = using , force_insert = force_insert , force_update = force_update )
5265
5366
54- def class_prepared_concurrency_handler (sender , ** kwargs ):
55- if hasattr (sender , 'RevisionMetaInfo' ) and not (sender .RevisionMetaInfo .manually or
56- sender .RevisionMetaInfo .versioned_save ):
57- old_save = getattr (sender , 'save' )
58- setattr (sender , 'save' , _wrap_save (old_save ))
59- sender .RevisionMetaInfo .versioned_save = True
60-
61-
6267class RevisionMetaInfo :
6368 field = None
6469 versioned_save = False
0 commit comments