Skip to content

Commit

Permalink
get previous value
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Jun 20, 2024
1 parent f021d88 commit 7d7f74b
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions codeforlife/models/signals/post_save.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from . import general as _
from .pre_save import PREVIOUS_VALUE_KEY

FieldValue = t.TypeVar("FieldValue")


def has_previous_values(instance: _.AnyModel, fields: t.Dict[str, t.Type]):
# pylint: disable=line-too-long
Expand All @@ -36,3 +38,27 @@ def has_previous_values(instance: _.AnyModel, fields: t.Dict[str, t.Type]):
return False

return True


def get_previous_value(
instance: _.AnyModel, field: str, cls: t.Type[FieldValue]
):
# pylint: disable=line-too-long
"""Get a previous value from the instance and assert the value is of the
expected type.
Args:
instance: The current instance.
field: The field to get the previous value for.
cls: The expected type of the value.
Returns:
The previous value of the field.
"""
# pylint: enable=line-too-long

previous_value = getattr(instance, PREVIOUS_VALUE_KEY.format(field=field))

assert isinstance(previous_value, cls)

return previous_value

0 comments on commit 7d7f74b

Please sign in to comment.