django-simple-history icon indicating copy to clipboard operation
django-simple-history copied to clipboard

Add some clue from where in the code the update came from

Open PetrDlouhy opened this issue 6 months ago • 0 comments

Problem Statement As I added simple history to existing project I am often dealing with a situation that I see history change but it have blank change_reason and I don't know where it came from. Optimally I would like to see full trace from the save() up.

I tried to add the _change_reason to various places in the code, but it could take quite some time to have it everywhere regardless the burden it would add to the code. I would like to have _change_reason only on places in the code where it brings some additional benefit over just knowing where the change came from.

Describe the solution you'd like I am not sure what the correct solution is. I am sure that for example introducing new field with full trace could be too expensive for many of the current implementations.

This functionality could have been made somehow optional, for example as some parameter to HistoricalRecords.

Also there is possibility to just fill in something to the _change_reason if it is blank. I have found a way to do this as a bit dirty hotfix in my project just by adding this code to /history_register/__init__.py:

import traceback

from simple_history import utils


# Modify get_change_message to include change reason
_original_get_change_reason_from_object = utils.get_change_reason_from_object


def get_change_reason_from_object(obj):
    original_reason = _original_get_change_reason_from_object(obj)
    if not original_reason:
        blenderhub_ts = []
        for t in traceback.format_stack():
            if "/blenderhub" in t and "/history_register/" not in t:
                blenderhub_ts.append(
                    t.split("apps/")[-1].split(", in")[0].replace('", line ', ":").strip()
                )
        return "|".join(blenderhub_ts)[-100:]
    return original_reason


utils.get_change_reason_from_object = get_change_reason_from_object

PetrDlouhy avatar Dec 21 '23 10:12 PetrDlouhy