Skip to content

Commit ce2eaa8

Browse files
committed
Add option to pass change_reason explicitly
1 parent 20bfd4d commit ce2eaa8

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

simple_history/registry_tests/migration_test_app/migrations/0001_initial.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class Migration(migrations.Migration):
2929
('history_id', models.AutoField(primary_key=True, serialize=False)),
3030
('history_date', models.DateTimeField()),
3131
('history_type', models.CharField(choices=[('+', 'Created'), ('~', 'Changed'), ('-', 'Deleted')], max_length=1)),
32+
('history_change_reason', models.CharField(max_length=100, null=True)),
3233
('history_user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL)),
3334
],
3435
options={

simple_history/tests/tests/test_models.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from django.test import TestCase
1313

1414
from simple_history.models import HistoricalRecords, convert_auto_field
15+
from simple_history.utils import update_change_reason
1516
from ..models import (
1617
AdminProfile, Bookcase, MultiOneToOne, Poll, Choice, Restaurant,
1718
Person, FileModel, Document, Book, HistoricalPoll, Library, State,
@@ -52,7 +53,7 @@ def assertRecordValues(self, record, klass, values_dict):
5253
self.assertEqual(getattr(record, key), value)
5354
self.assertEqual(record.history_object.__class__, klass)
5455
for key, value in values_dict.items():
55-
if key not in ['history_type', 'history_change_reason'] :
56+
if key not in ['history_type', 'history_change_reason']:
5657
self.assertEqual(getattr(record.history_object, key), value)
5758

5859
def test_create(self):
@@ -71,8 +72,8 @@ def test_update(self):
7172
Poll.objects.create(question="what's up?", pub_date=today)
7273
p = Poll.objects.get()
7374
p.pub_date = tomorrow
74-
p.changeReason = 'future poll'
7575
p.save()
76+
update_change_reason(p, 'future poll')
7677
update_record, create_record = p.history.all()
7778
self.assertRecordValues(create_record, Poll, {
7879
'question': "what's up?",
@@ -90,7 +91,7 @@ def test_update(self):
9091
})
9192
self.assertDatetimesEqual(update_record.history_date, datetime.now())
9293

93-
def test_delete(self):
94+
def test_delete_verify_change_reason_implicitly(self):
9495
p = Poll.objects.create(question="what's up?", pub_date=today)
9596
poll_id = p.id
9697
p.changeReason = 'wrongEntry'
@@ -111,6 +112,27 @@ def test_delete(self):
111112
'history_type': "-"
112113
})
113114

115+
def test_delete_verify_change_reason_explicity(self):
116+
p = Poll.objects.create(question="what's up?", pub_date=today)
117+
poll_id = p.id
118+
p.delete()
119+
update_change_reason(p, 'wrongEntry')
120+
delete_record, create_record = Poll.history.all()
121+
self.assertRecordValues(create_record, Poll, {
122+
'question': "what's up?",
123+
'pub_date': today,
124+
'id': poll_id,
125+
'history_change_reason': None,
126+
'history_type': "+"
127+
})
128+
self.assertRecordValues(delete_record, Poll, {
129+
'question': "what's up?",
130+
'pub_date': today,
131+
'id': poll_id,
132+
'history_change_reason': 'wrongEntry',
133+
'history_type': "-"
134+
})
135+
114136
def test_save_without_historical_record(self):
115137
pizza_place = Restaurant.objects.create(name='Pizza Place', rating=3)
116138
pizza_place.rating = 4

simple_history/utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def update_change_reason(instance, reason):
2+
attrs = {}
3+
model = type(instance)
4+
manager = instance if instance.id is not None else model
5+
for field in instance._meta.fields:
6+
value = getattr(instance, field.attname)
7+
if field.primary_key is True:
8+
if value is not None:
9+
attrs[field.attname] = value
10+
else:
11+
attrs[field.attname] = value
12+
record = manager.history.filter(**attrs).order_by('-history_date').first()
13+
record.history_change_reason = reason
14+
record.save()

0 commit comments

Comments
 (0)