Skip to content

Commit 932b928

Browse files
committed
- new modules api. see CHANGES
- new module exceptions,see CHANGES
1 parent f91b7c6 commit 932b928

File tree

4 files changed

+67
-40
lines changed

4 files changed

+67
-40
lines changed

CHANGES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Release 0.4.0 (under development)
22
=================================
33

4+
* start deprecation of ``concurrency.core.VersionChangedError``, ``concurrency.core.RecordModifiedError``,
5+
``concurrency.core.InconsistencyError``,moved in ``concurrency.exceptions``
6+
* start deprecation of ``concurrency.core.apply_concurrency_check``, ``concurrency.core.concurrency_check
7+
moved in ``concurrency.api``
48
* added :ref:`CONCURRECY_SANITY_CHECK` settings entry
59
* signing of version number to avoid tampering (:ref:`concurrentform`)
610
* added :ref:`ConcurrencyTestMixin` to help test on concurrency managed models

concurrency/api.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# -*- coding: utf-8 -*-
2+
from django.core.exceptions import ImproperlyConfigured
3+
4+
from django.utils.translation import gettext as _
5+
from concurrency.core import _wrap_save, _select_lock
6+
7+
__all__ = ['apply_concurrency_check', 'concurrency_check']
8+
9+
10+
def apply_concurrency_check(model, fieldname, versionclass):
11+
"""
12+
Apply concurrency management to existing Models.
13+
14+
:param model: Model class to update
15+
:type model: django.db.Model
16+
17+
:param fieldname: name of the field
18+
:type fieldname: basestring
19+
20+
:param versionclass:
21+
:type versionclass: concurrency.fields.VersionField subclass
22+
"""
23+
if hasattr(model, 'RevisionMetaInfo'):
24+
raise ImproperlyConfigured("%s is already under concurrency management" % model)
25+
26+
ver = versionclass()
27+
ver.contribute_to_class(model, fieldname)
28+
model.RevisionMetaInfo.field = ver
29+
30+
if not model.RevisionMetaInfo.versioned_save:
31+
old_save = getattr(model, 'save')
32+
setattr(model, 'save', _wrap_save(old_save))
33+
model.RevisionMetaInfo.versioned_save = True
34+
35+
36+
def concurrency_check(model_instance, force_insert=False, force_update=False, using=None, **kwargs):
37+
if not force_insert:
38+
_select_lock(model_instance)

concurrency/core.py

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,24 @@
1+
import warnings
12
from functools import update_wrapper
23
from django.conf import settings
3-
from django.core.exceptions import ImproperlyConfigured, ValidationError
44
from django.db import DatabaseError, connections, router
55
from django.utils.translation import ugettext as _
6+
from concurrency.exceptions import VersionChangedError, RecordModifiedError, InconsistencyError
67

78
__all__ = []
89

9-
10-
class VersionChangedError(ValidationError):
11-
pass
12-
13-
14-
class RecordModifiedError(DatabaseError):
15-
def __init__(self, *args, **kwargs):
16-
self.target = kwargs.pop('target')
17-
super(RecordModifiedError, self).__init__(*args, **kwargs)
18-
19-
20-
class InconsistencyError(DatabaseError):
21-
pass
22-
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)
2313

2414
def apply_concurrency_check(model, fieldname, versionclass):
25-
"""
26-
Apply concurrency management to existing Models.
27-
28-
:param model: Model class to update
29-
:type model: django.db.Model
30-
31-
:param fieldname: name of the field
32-
:type fieldname: basestring
33-
34-
:param versionclass:
35-
:type versionclass: concurrency.fields.VersionField subclass
36-
"""
37-
if hasattr(model, 'RevisionMetaInfo'):
38-
raise ImproperlyConfigured("%s is already under concurrency management" % model)
39-
40-
ver = versionclass()
41-
ver.contribute_to_class(model, fieldname)
42-
model.RevisionMetaInfo.field = ver
43-
44-
if not model.RevisionMetaInfo.versioned_save:
45-
old_save = getattr(model, 'save')
46-
setattr(model, 'save', _wrap_save(old_save))
47-
model.RevisionMetaInfo.versioned_save = True
15+
from concurrency.api import apply_concurrency_check as acc
16+
return acc(model, fieldname, versionclass)
4817

4918

5019
def concurrency_check(model_instance, force_insert=False, force_update=False, using=None, **kwargs):
51-
if not force_insert:
52-
_select_lock(model_instance)
20+
from concurrency.api import concurrency_check as cc
21+
return cc(model_instance, force_insert, force_update, using, **kwargs)
5322

5423

5524
def _select_lock(model_instance, version_value=None):

concurrency/exceptions.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from django.core.exceptions import ImproperlyConfigured, ValidationError
2+
from django.db import DatabaseError
3+
4+
5+
class VersionChangedError(ValidationError):
6+
pass
7+
8+
9+
class RecordModifiedError(DatabaseError):
10+
def __init__(self, *args, **kwargs):
11+
self.target = kwargs.pop('target')
12+
super(RecordModifiedError, self).__init__(*args, **kwargs)
13+
14+
15+
class InconsistencyError(DatabaseError):
16+
pass

0 commit comments

Comments
 (0)