From 1b9c3a5ee9fe1ab6a93206ec1cf90d9a8a5177f2 Mon Sep 17 00:00:00 2001 From: Brylie Christopher Oxley Date: Fri, 19 Feb 2021 13:13:56 +0200 Subject: [PATCH] Add next and previous record diffs Closes #797 Added next and previous record diffs for consistency --- simple_history/models.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/simple_history/models.py b/simple_history/models.py index 0afb0b21c..9347d3024 100644 --- a/simple_history/models.py +++ b/simple_history/models.py @@ -423,6 +423,18 @@ def get_next_record(self): .first() ) + def get_next_record_diff(self): + """ + Get the difference between this and the next record. `None` of no next record. + """ + + next_record = self.get_prev_record() + + if next_record is not None: + return self.diff_against(previous_record) + + return None + def get_prev_record(self): """ Get the previous history record for the instance. `None` if first. @@ -434,6 +446,18 @@ def get_prev_record(self): .last() ) + def get_prev_record_diff(self): + """ + Get the difference between this and the previous record. `None` if no previous record. + """ + + previous_record = self.get_prev_record() + + if previous_record is not None: + return self.diff_against(previous_record) + + return None + def get_default_history_user(instance): """ Returns the user specified by `get_user` method for manually creating @@ -455,7 +479,9 @@ def get_default_history_user(instance): "instance": property(get_instance), "instance_type": model, "next_record": property(get_next_record), + "next_record_diff": property(get_next_record_diff), "prev_record": property(get_prev_record), + "prev_record_diff": property(get_prev_record_diff), "revert_url": revert_url, "__str__": lambda self: "{} as of {}".format( self.history_object, self.history_date