Skip to content

Commit

Permalink
Add next and previous record diffs
Browse files Browse the repository at this point in the history
Closes #797

Added next and previous record diffs for consistency
  • Loading branch information
brylie authored and Brylie Christopher Oxley committed Oct 26, 2021
1 parent 8215eb9 commit 1b9c3a5
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 1b9c3a5

Please sign in to comment.