Skip to content

Commit 882ff36

Browse files
lmiguelvargasfRoss Mechanic
authored andcommitted
Support changeReason for bulk history create (#449)
* Support changeReason for bulk_history_create This solves issue #442. * Test bulk_history_create sets change reason * Update docs * Add suggested changes
1 parent 0f975c4 commit 882ff36

File tree

5 files changed

+36
-0
lines changed

5 files changed

+36
-0
lines changed

AUTHORS.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ Authors
4040
- Marty Alchin
4141
- Mauricio de Abreu Antunes
4242
- Micah Denbraver
43+
- Miguel Vargas
44+
- Nianpeng Li
4345
- Phillip Marshall
4446
- Rajesh Pappula
4547
- Roberto Aguilar

CHANGES.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changes
22
=======
33

4+
Unreleased
5+
-----------
6+
- Add `'+'` as the `history_type` for each instance in `bulk_history_create` (gh-449)
7+
- Add support for `history_change_reason` for each instance in `bulk_history_create` (gh-449)
8+
49
2.5.0 (2018-10-18)
510
------------------
611
- Add ability to cascade delete historical records when master record is deleted (gh-440)

docs/reference.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ history:
3131
>>> Poll.history.count()
3232
1000
3333
34+
If you want to specify a change reason for each record in the bulk create, you
35+
can add `changeReason` on each instance:
36+
37+
.. code-block:: pycon
38+
39+
>>> for poll in data:
40+
poll.changeReason = 'reason'
41+
>>> objs = bulk_create_with_history(data, Poll, batch_size=500)
42+
>>> Poll.history.get(id=data[0].id).history_change_reason
43+
'reason'
44+
45+
3446
QuerySet Updates with History
3547
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3648
Unlike with ``bulk_create``, `queryset updates`_ perform an SQL update query on

simple_history/manager.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ def bulk_history_create(self, objs, batch_size=None):
9797
self.model(
9898
history_date=getattr(instance, '_history_date', now()),
9999
history_user=getattr(instance, '_history_user', None),
100+
history_change_reason=getattr(instance, 'changeReason', ''),
101+
history_type='+',
100102
**{
101103
field.attname: getattr(instance, field.attname)
102104
for field in instance._meta.fields

simple_history/tests/tests/test_manager.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,26 @@ def test_simple_bulk_history_create(self):
104104
self.assertQuerysetEqual(Poll.history.order_by('question'), [
105105
'Question 1', 'Question 2', 'Question 3', 'Question 4'
106106
], attrgetter('question'))
107+
self.assertTrue(
108+
all([history.history_type == '+'
109+
for history in Poll.history.all()])
110+
)
107111

108112
created = Poll.history.bulk_create([])
109113
self.assertEqual(created, [])
110114
self.assertEqual(Poll.history.count(), 4)
111115

116+
def test_bulk_history_create_with_change_reason(self):
117+
for poll in self.data:
118+
poll.changeReason = 'reason'
119+
120+
Poll.history.bulk_history_create(self.data)
121+
122+
self.assertTrue(
123+
all([history.history_change_reason == 'reason'
124+
for history in Poll.history.all()])
125+
)
126+
112127
def test_bulk_history_create_on_objs_without_ids(self):
113128
self.data = [
114129
Poll(question='Question 1', pub_date=datetime.now()),

0 commit comments

Comments
 (0)