Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hardcoded history manager #542

Merged
merged 2 commits into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Authors
- Daniel Gilge
- Daniel Levy
- Daniel Roschka
- Daniil Skrobov (`yetanotherape <https://github.com/yetanotherape>`_)
- David Grochowski (`ThePumpingLemma <https://github.com/ThePumpingLemma>`_)
- David Hite
- Eduardo Cuducos
Expand Down
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Changes
=======

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

2.7.0 (2019-01-16)
------------------
Expand Down
5 changes: 4 additions & 1 deletion simple_history/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from django.utils.text import capfirst
from django.utils.translation import ugettext as _

from . import utils

USER_NATURAL_KEY = tuple(key.lower() for key in settings.AUTH_USER_MODEL.split(".", 1))

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

if "_change_history" in request.POST and SIMPLE_HISTORY_EDIT:
obj = obj.history.get(pk=version_id).instance
history = utils.get_history_manager_for_model(obj)
obj = history.get(pk=version_id).instance

formsets = []
form_class = self.get_form(request, obj)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ def _process(self, to_process, date_back=None, dry_run=True):

def _process_instance(self, instance, model, stop_date=None, dry_run=True):
entries_deleted = 0
o_qs = instance.history.all()
history = utils.get_history_manager_for_model(instance)
o_qs = history.all()
if stop_date:
# to compare last history match
extra_one = o_qs.filter(history_date__lte=stop_date).first()
Expand Down
28 changes: 27 additions & 1 deletion simple_history/tests/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@

from simple_history import models as sh_models
from simple_history.management.commands import populate_history, clean_duplicate_history
from ..models import Book, Poll, PollWithExcludeFields, Restaurant, Place
from ..models import (
Book,
CustomManagerNameModel,
Place,
Poll,
PollWithExcludeFields,
Restaurant,
)


@contextmanager
Expand Down Expand Up @@ -355,3 +362,22 @@ def test_auto_cleanup_dated_extra_one(self):
# the "extra_one" (the record before the oldest match)
# is identical to the oldest match, so oldest match is deleted
self.assertEqual(Poll.history.all().count(), 5)

def test_auto_cleanup_custom_history_field(self):
m = CustomManagerNameModel.objects.create(name="John")
self.assertEqual(CustomManagerNameModel.log.all().count(), 1)
m.save()
self.assertEqual(CustomManagerNameModel.log.all().count(), 2)
m.name = "Ivan"
m.save()
self.assertEqual(CustomManagerNameModel.log.all().count(), 3)
out = StringIO()
management.call_command(
self.command_name, auto=True, stdout=out, stderr=StringIO()
)
self.assertEqual(
out.getvalue(),
"Removed 1 historical records for "
"<class 'simple_history.tests.models.CustomManagerNameModel'>\n",
)
self.assertEqual(CustomManagerNameModel.log.all().count(), 2)
3 changes: 2 additions & 1 deletion simple_history/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def update_change_reason(instance, reason):
attrs[field.attname] = value
else:
attrs[field.attname] = value
record = manager.history.filter(**attrs).order_by("-history_date").first()
history = get_history_manager_for_model(manager)
record = history.filter(**attrs).order_by("-history_date").first()
record.history_change_reason = reason
record.save()

Expand Down