Skip to content

Commit de0c300

Browse files
yetanotherapeRoss Mechanic
authored andcommitted
Fix hardcoded history manager (#542)
* Fix hardcoded history manager. * Add PR changelist requirements.
1 parent 2e7d9d3 commit de0c300

File tree

6 files changed

+37
-4
lines changed

6 files changed

+37
-4
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Authors
2525
- Daniel Gilge
2626
- Daniel Levy
2727
- Daniel Roschka
28+
- Daniil Skrobov (`yetanotherape <https://github.com/yetanotherape>`_)
2829
- David Grochowski (`ThePumpingLemma <https://github.com/ThePumpingLemma>`_)
2930
- David Hite
3031
- Eduardo Cuducos

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Changes
22
=======
33

44
- remove reference to vendored library ``django.utils.six`` in favor of ``six`` (gh-526)
5+
- Fixed hardcoded history manager (gh-540)
56

67
2.7.0 (2019-01-16)
78
------------------

simple_history/admin.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
from django.utils.text import capfirst
1616
from django.utils.translation import ugettext as _
1717

18+
from . import utils
19+
1820
USER_NATURAL_KEY = tuple(key.lower() for key in settings.AUTH_USER_MODEL.split(".", 1))
1921

2022
SIMPLE_HISTORY_EDIT = getattr(settings, "SIMPLE_HISTORY_EDIT", False)
@@ -130,7 +132,8 @@ def history_form_view(self, request, object_id, version_id, extra_context=None):
130132
change_history = False
131133

132134
if "_change_history" in request.POST and SIMPLE_HISTORY_EDIT:
133-
obj = obj.history.get(pk=version_id).instance
135+
history = utils.get_history_manager_for_model(obj)
136+
obj = history.get(pk=version_id).instance
134137

135138
formsets = []
136139
form_class = self.get_form(request, obj)

simple_history/management/commands/clean_duplicate_history.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ def _process(self, to_process, date_back=None, dry_run=True):
7474

7575
def _process_instance(self, instance, model, stop_date=None, dry_run=True):
7676
entries_deleted = 0
77-
o_qs = instance.history.all()
77+
history = utils.get_history_manager_for_model(instance)
78+
o_qs = history.all()
7879
if stop_date:
7980
# to compare last history match
8081
extra_one = o_qs.filter(history_date__lte=stop_date).first()

simple_history/tests/tests/test_commands.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@
77

88
from simple_history import models as sh_models
99
from simple_history.management.commands import populate_history, clean_duplicate_history
10-
from ..models import Book, Poll, PollWithExcludeFields, Restaurant, Place
10+
from ..models import (
11+
Book,
12+
CustomManagerNameModel,
13+
Place,
14+
Poll,
15+
PollWithExcludeFields,
16+
Restaurant,
17+
)
1118

1219

1320
@contextmanager
@@ -355,3 +362,22 @@ def test_auto_cleanup_dated_extra_one(self):
355362
# the "extra_one" (the record before the oldest match)
356363
# is identical to the oldest match, so oldest match is deleted
357364
self.assertEqual(Poll.history.all().count(), 5)
365+
366+
def test_auto_cleanup_custom_history_field(self):
367+
m = CustomManagerNameModel.objects.create(name="John")
368+
self.assertEqual(CustomManagerNameModel.log.all().count(), 1)
369+
m.save()
370+
self.assertEqual(CustomManagerNameModel.log.all().count(), 2)
371+
m.name = "Ivan"
372+
m.save()
373+
self.assertEqual(CustomManagerNameModel.log.all().count(), 3)
374+
out = StringIO()
375+
management.call_command(
376+
self.command_name, auto=True, stdout=out, stderr=StringIO()
377+
)
378+
self.assertEqual(
379+
out.getvalue(),
380+
"Removed 1 historical records for "
381+
"<class 'simple_history.tests.models.CustomManagerNameModel'>\n",
382+
)
383+
self.assertEqual(CustomManagerNameModel.log.all().count(), 2)

simple_history/utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ def update_change_reason(instance, reason):
1414
attrs[field.attname] = value
1515
else:
1616
attrs[field.attname] = value
17-
record = manager.history.filter(**attrs).order_by("-history_date").first()
17+
history = get_history_manager_for_model(manager)
18+
record = history.filter(**attrs).order_by("-history_date").first()
1819
record.history_change_reason = reason
1920
record.save()
2021

0 commit comments

Comments
 (0)