Skip to content

Commit 4bd742e

Browse files
author
Ross Mechanic
authored
Deprecate changereason (#655)
* Deprecate changeReason and replace with _change_reasom * add missing files
1 parent 2907e49 commit 4bd742e

File tree

9 files changed

+39
-17
lines changed

9 files changed

+39
-17
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Unreleased
55
------------
66
- Added `bulk_update_with_history` utility function (gh-650)
77
- Add default user and default change reason to `bulk_create_with_history` and `bulk_update_with_history`
8+
- Start using `_change_reason` instead of `changeReason` to add change reasons to historical
9+
objects. `changeReason` is deprecated and will be removed in version `3.0.0`
810

911
2.9.0 (2020-04-23)
1012
------------------

docs/common_issues.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@ history:
3333
1000
3434
3535
If you want to specify a change reason or history user for each record in the bulk create,
36-
you can add `changeReason` or `_history_user` on each instance:
36+
you can add `_change_reason` or `_history_user` on each instance:
3737

3838
.. code-block:: pycon
3939
4040
>>> for poll in data:
41-
poll.changeReason = 'reason'
41+
poll._change_reason = 'reason'
4242
poll._history_user = my_user
4343
>>> objs = bulk_create_with_history(data, Poll, batch_size=500)
4444
>>> Poll.history.get(id=data[0].id).history_change_reason
4545
'reason'
4646
4747
You can also specify a default user or default change reason responsible for the change
48-
(`_history_user` and `changeReason` take precedence).
48+
(`_history_user` and `_change_reason` take precedence).
4949

5050
.. code-block:: pycon
5151

docs/historical_model.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@ As an example using the callable mechanism, the below changes the default prefix
151151
opinion = models.CharField(max_length=2000)
152152
153153
register(Opinion, custom_model_name=lambda x:f'Audit{x}')
154-
154+
155155
The resulting history class names would be `AuditPoll` and `AuditOpinion`.
156156
If the app the models are defined in is `yoda` then the corresponding history table names would be `yoda_auditpoll` and `yoda_auditopinion`
157-
157+
158158
IMPORTANT: Setting `custom_model_name` to `lambda x:f'{x}'` is not permitted.
159159
An error will be generated and no history model created if they are the same.
160160

@@ -287,8 +287,8 @@ Change Reason
287287
Change reason is a message to explain why the change was made in the instance. It is stored in the
288288
field ``history_change_reason`` and its default value is ``None``.
289289

290-
By default, the django-simple-history gets the change reason in the field ``changeReason`` of the instance. Also, is possible to pass
291-
the ``changeReason`` explicitly. For this, after a save or delete in an instance, is necessary call the
290+
By default, the django-simple-history gets the change reason in the field ``_change_reason`` of the instance. Also, is possible to pass
291+
the ``_change_reason`` explicitly. For this, after a save or delete in an instance, is necessary call the
292292
function ``utils.update_change_reason``. The first argument of this function is the instance and the second
293293
is the message that represents the change reason.
294294

@@ -308,7 +308,7 @@ You can create an instance with an implicit change reason.
308308
.. code-block:: python
309309
310310
poll = Poll(question='Question 1')
311-
poll.changeReason = 'Add a question'
311+
poll._change_reason = 'Add a question'
312312
poll.save()
313313
314314
Or you can pass the change reason explicitly:

simple_history/manager.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from django.db import models
44
from django.utils import timezone
55

6+
from simple_history.utils import get_change_reason_from_object
7+
68

79
class HistoryDescriptor(object):
810
def __init__(self, model):
@@ -121,9 +123,8 @@ def bulk_history_create(
121123
row = self.model(
122124
history_date=getattr(instance, "_history_date", timezone.now()),
123125
history_user=getattr(instance, "_history_user", default_user),
124-
history_change_reason=getattr(
125-
instance, "changeReason", default_change_reason
126-
),
126+
history_change_reason=get_change_reason_from_object(instance)
127+
or default_change_reason,
127128
history_type=history_type,
128129
**{
129130
field.attname: getattr(instance, field.attname)

simple_history/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from . import exceptions
2525
from .manager import HistoryDescriptor
2626
from .signals import post_create_historical_record, pre_create_historical_record
27+
from .utils import get_change_reason_from_object
2728

2829
if django.VERSION < (2,):
2930
from django.utils.translation import ugettext_lazy as _
@@ -479,7 +480,7 @@ def create_historical_record(self, instance, history_type, using=None):
479480
using = using if self.use_base_model_db else None
480481
history_date = getattr(instance, "_history_date", timezone.now())
481482
history_user = self.get_history_user(instance)
482-
history_change_reason = getattr(instance, "changeReason", None)
483+
history_change_reason = get_change_reason_from_object(instance)
483484
manager = getattr(instance, self.manager_name)
484485

485486
attrs = {}

simple_history/tests/tests/test_admin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def test_history_list(self):
7979
self.assertEqual(model_name, "customuser")
8080
self.login()
8181
poll = Poll(question="why?", pub_date=today)
82-
poll.changeReason = "A random test reason"
82+
poll._change_reason = "A random test reason"
8383
poll._history_user = self.user
8484
poll.save()
8585

simple_history/tests/tests/test_manager.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def test_simple_bulk_history_create(self):
113113

114114
def test_bulk_history_create_with_change_reason(self):
115115
for poll in self.data:
116-
poll.changeReason = "reason"
116+
poll._change_reason = "reason"
117117

118118
Poll.history.bulk_history_create(self.data)
119119

@@ -162,7 +162,7 @@ def test_bulk_history_create_history_user_overrides_default(self):
162162

163163
def test_bulk_history_create_change_reason_overrides_default(self):
164164
for data in self.data:
165-
data.changeReason = "my_reason"
165+
data._change_reason = "my_reason"
166166

167167
Poll.history.bulk_history_create(self.data, default_change_reason="test")
168168

@@ -236,7 +236,7 @@ def test_simple_bulk_history_create(self):
236236

237237
def test_bulk_history_create_with_change_reason(self):
238238
for poll in self.data:
239-
poll.changeReason = "reason"
239+
poll._change_reason = "reason"
240240

241241
Poll.history.bulk_history_create(self.data)
242242

simple_history/tests/tests/test_models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def test_update(self):
171171
def test_delete_verify_change_reason_implicitly(self):
172172
p = Poll.objects.create(question="what's up?", pub_date=today)
173173
poll_id = p.id
174-
p.changeReason = "wrongEntry"
174+
p._change_reason = "wrongEntry"
175175
p.delete()
176176
delete_record, create_record = Poll.history.all()
177177
self.assertRecordValues(

simple_history/utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import warnings
2+
13
import django
24
from django.db import transaction
35
from django.forms.models import model_to_dict
@@ -123,3 +125,19 @@ def bulk_update_with_history(
123125
default_user=default_user,
124126
default_change_reason=default_change_reason,
125127
)
128+
129+
130+
def get_change_reason_from_object(obj):
131+
if hasattr(obj, "_change_reason"):
132+
return getattr(obj, "_change_reason")
133+
134+
if hasattr(obj, "changeReason"):
135+
warning_msg = (
136+
"Using the attr changeReason to populate history_change_reason is"
137+
" deprecated in 2.10.0 and will be removed in 3.0.0. Use "
138+
"_change_reason instead. "
139+
)
140+
warnings.warn(warning_msg, DeprecationWarning)
141+
return getattr(obj, "changeReason")
142+
143+
return None

0 commit comments

Comments
 (0)