|
| 1 | +import warnings |
1 | 2 | from functools import update_wrapper |
2 | 3 | from django.conf import settings |
3 | | -from django.core.exceptions import ImproperlyConfigured, ValidationError |
4 | 4 | from django.db import DatabaseError, connections, router |
5 | 5 | from django.utils.translation import ugettext as _ |
| 6 | +from concurrency.exceptions import VersionChangedError, RecordModifiedError, InconsistencyError |
6 | 7 |
|
7 | 8 | __all__ = [] |
8 | 9 |
|
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) |
23 | 13 |
|
24 | 14 | 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) |
48 | 17 |
|
49 | 18 |
|
50 | 19 | 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) |
53 | 22 |
|
54 | 23 |
|
55 | 24 | def _select_lock(model_instance, version_value=None): |
|
0 commit comments