From 46516ca358da3044941c9860a848034e6642b8f1 Mon Sep 17 00:00:00 2001 From: amartis Date: Sun, 20 Mar 2022 00:02:49 +0800 Subject: [PATCH 1/3] Make change reason customizable with overriding. Shortcoming of previous architecture: Recording reason for change was only through manually setting to each object or through bulk_history_create. So recording API request as reason automatically was hard to implement with this architecture. Solution: Getting change reason is now done through `HistoricalRecords` method `get_change_reason_for_object`. And thus this method can be overriden in subclass to define custom behavior. --- AUTHORS.rst | 1 + simple_history/models.py | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/AUTHORS.rst b/AUTHORS.rst index 7657f729b..31fa0b60d 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -15,6 +15,7 @@ Authors - Aleksey Kladov - Alexander Anikeev - Amanda Ng (`AmandaCLNg `_) +- Amartis Gladius (`Amartis `_) - Ben Lawson (`blawson `_) - Benjamin Mampaey (`bmampaey `_) - `bradford281 `_ diff --git a/simple_history/models.py b/simple_history/models.py index bee29637a..ab76a81df 100644 --- a/simple_history/models.py +++ b/simple_history/models.py @@ -541,11 +541,18 @@ def post_delete(self, instance, using=None, **kwargs): else: self.create_historical_record(instance, "-", using=using) + def get_change_reason_for_object(self, instance, history_type, using): + """ + Get change reason for object. + Customize this method to automatically fill change reason from context. + """ + return get_change_reason_from_object(instance) + def create_historical_record(self, instance, history_type, using=None): using = using if self.use_base_model_db else None history_date = getattr(instance, "_history_date", timezone.now()) history_user = self.get_history_user(instance) - history_change_reason = get_change_reason_from_object(instance) + history_change_reason = self.get_change_reason_for_object(instance, history_type, using) manager = getattr(instance, self.manager_name) attrs = {} From 7c6774ecde5fa4d84ddfe78e470327a714d32ed6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 19 Mar 2022 16:07:01 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- simple_history/models.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/simple_history/models.py b/simple_history/models.py index ab76a81df..33a1ec487 100644 --- a/simple_history/models.py +++ b/simple_history/models.py @@ -544,7 +544,7 @@ def post_delete(self, instance, using=None, **kwargs): def get_change_reason_for_object(self, instance, history_type, using): """ Get change reason for object. - Customize this method to automatically fill change reason from context. + Customize this method to automatically fill change reason from context. """ return get_change_reason_from_object(instance) @@ -552,7 +552,9 @@ def create_historical_record(self, instance, history_type, using=None): using = using if self.use_base_model_db else None history_date = getattr(instance, "_history_date", timezone.now()) history_user = self.get_history_user(instance) - history_change_reason = self.get_change_reason_for_object(instance, history_type, using) + history_change_reason = self.get_change_reason_for_object( + instance, history_type, using + ) manager = getattr(instance, self.manager_name) attrs = {} From 59b1aecdfdcf1de8a04f55994137705745823542 Mon Sep 17 00:00:00 2001 From: amartis Date: Sun, 20 Mar 2022 09:03:21 +0800 Subject: [PATCH 3/3] Add to CHANGES.rst --- CHANGES.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.rst b/CHANGES.rst index 443ad5594..50984593c 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -31,6 +31,7 @@ Full list of changes: - Fix bug with ``history.diff_against`` with non-editable fields (gh-923) - Dropped support for Django 3.1 (gh-952) - RecordModels now support a ``no_db_index`` setting, to drop indices in historical models, default stays the same (gh-720) +- Support change reason formula feature. Change reason formula can be defined by overriding ``get_change_reason_for_object`` method after subclassing ``HistoricalRecords`` 3.0.0 (2021-04-16) ------------------