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

Make change reason customizable with overriding. #962

Merged
merged 4 commits into from
Mar 22, 2022
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 @@ -15,6 +15,7 @@ Authors
- Aleksey Kladov
- Alexander Anikeev
- Amanda Ng (`AmandaCLNg <https://github.com/AmandaCLNg>`_)
- Amartis Gladius (`Amartis <https://github.com/amartis>`_)
- Ben Lawson (`blawson <https://github.com/blawson>`_)
- Benjamin Mampaey (`bmampaey <https://github.com/bmampaey>`_)
- `bradford281 <https://github.com/bradford281>`_
Expand Down
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
------------------
Expand Down
11 changes: 10 additions & 1 deletion simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,11 +541,20 @@ 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 = {}
Expand Down