Skip to content

Commit 3100b03

Browse files
committed
add history change reason to allow reasoning for the changes; use changeReason to set values
1 parent 6fd1b65 commit 3100b03

File tree

5 files changed

+13
-2
lines changed

5 files changed

+13
-2
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Authors
2626
- Trey Hunner
2727
- Ulysses Vilela
2828
- vnagendra
29+
- hmit
2930

3031
Background
3132
==========

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ tip (unreleased)
55
----------------
66
- Do NOT delete the history elements when a user is deleted.
77
- Add support for ``latest``
8+
- Allow setting a reason for change. [using option changeReason]
89

910
1.5.3 (2014-11-18)
1011
------------------

simple_history/models.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ def get_instance(self):
159159
return {
160160
'history_id': models.AutoField(primary_key=True),
161161
'history_date': models.DateTimeField(),
162+
'history_change_reason': models.CharField(max_length=100, null=True),
162163
'history_user': models.ForeignKey(
163164
user_model, null=True, related_name=self.user_related_name,
164165
on_delete=models.SET_NULL),
@@ -204,12 +205,13 @@ def post_delete(self, instance, **kwargs):
204205
def create_historical_record(self, instance, history_type):
205206
history_date = getattr(instance, '_history_date', now())
206207
history_user = self.get_history_user(instance)
208+
history_change_reason = getattr(instance, 'changeReason', None)
207209
manager = getattr(instance, self.manager_name)
208210
attrs = {}
209211
for field in instance._meta.fields:
210212
attrs[field.attname] = getattr(instance, field.attname)
211213
manager.create(history_date=history_date, history_type=history_type,
212-
history_user=history_user, **attrs)
214+
history_user=history_user, history_change_reason=history_change_reason, **attrs)
213215

214216
def get_history_user(self, instance):
215217
"""Get the modifying user from instance or middleware."""

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

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

simple_history/tests/tests/test_models.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def assertRecordValues(self, record, klass, values_dict):
5050
self.assertEqual(getattr(record, key), value)
5151
self.assertEqual(record.history_object.__class__, klass)
5252
for key, value in values_dict.items():
53-
if key != 'history_type':
53+
if key not in ['history_type', 'history_change_reason'] :
5454
self.assertEqual(getattr(record.history_object, key), value)
5555

5656
def test_create(self):
@@ -69,37 +69,43 @@ def test_update(self):
6969
Poll.objects.create(question="what's up?", pub_date=today)
7070
p = Poll.objects.get()
7171
p.pub_date = tomorrow
72+
p.changeReason = 'future poll'
7273
p.save()
7374
update_record, create_record = p.history.all()
7475
self.assertRecordValues(create_record, Poll, {
7576
'question': "what's up?",
7677
'pub_date': today,
7778
'id': p.id,
79+
'history_change_reason': None,
7880
'history_type': "+"
7981
})
8082
self.assertRecordValues(update_record, Poll, {
8183
'question': "what's up?",
8284
'pub_date': tomorrow,
8385
'id': p.id,
86+
'history_change_reason': 'future poll',
8487
'history_type': "~"
8588
})
8689
self.assertDatetimesEqual(update_record.history_date, datetime.now())
8790

8891
def test_delete(self):
8992
p = Poll.objects.create(question="what's up?", pub_date=today)
9093
poll_id = p.id
94+
p.changeReason = 'wrongEntry'
9195
p.delete()
9296
delete_record, create_record = Poll.history.all()
9397
self.assertRecordValues(create_record, Poll, {
9498
'question': "what's up?",
9599
'pub_date': today,
96100
'id': poll_id,
101+
'history_change_reason': None,
97102
'history_type': "+"
98103
})
99104
self.assertRecordValues(delete_record, Poll, {
100105
'question': "what's up?",
101106
'pub_date': today,
102107
'id': poll_id,
108+
'history_change_reason': 'wrongEntry',
103109
'history_type': "-"
104110
})
105111

0 commit comments

Comments
 (0)