Skip to content

Commit d028ccd

Browse files
committed
fix check previous values
1 parent 7d7f74b commit d028ccd

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

codeforlife/models/signals/post_save.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,28 @@
1414
FieldValue = t.TypeVar("FieldValue")
1515

1616

17-
def has_previous_values(instance: _.AnyModel, fields: t.Dict[str, t.Type]):
17+
def check_previous_values(
18+
instance: _.AnyModel,
19+
predicates: t.Dict[str, t.Callable[[t.Any], bool]],
20+
):
1821
# pylint: disable=line-too-long
19-
"""Check if the instance has the specified previous values and that the
20-
values are an instance of the specified type.
22+
"""Check if the previous values are as expected. If the previous value's key
23+
is not on the model, this check returns false.
2124
2225
Args:
2326
instance: The current instance.
24-
fields: The fields the instance should have and the type of each value.
27+
predicates: A predicate for each field. The previous value is passed in as an arg and it should return True if the previous value is as expected.
2528
2629
Returns:
27-
If the instance has all the previous values and all the values are of
28-
the expected type.
30+
If all the previous values are as expected.
2931
"""
3032
# pylint: enable=line-too-long
3133

32-
for field, cls in fields.items():
34+
for field, predicate in predicates.items():
3335
previous_value_key = PREVIOUS_VALUE_KEY.format(field=field)
3436

35-
if not hasattr(instance, previous_value_key) or not isinstance(
36-
getattr(instance, previous_value_key), cls
37+
if not hasattr(instance, previous_value_key) or not predicate(
38+
getattr(instance, previous_value_key)
3739
):
3840
return False
3941

codeforlife/models/signals/pre_save.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,15 @@ def get_previous_value(field: str):
4747

4848
def check_previous_values(
4949
instance: _.AnyModel,
50-
predicates: t.Dict[str, t.Callable[[t.Any, t.Any], bool]],
50+
predicates: t.Dict[str, t.Callable[[t.Any], bool]],
5151
):
5252
# pylint: disable=line-too-long
5353
"""Check if the previous values are as expected. If the model has not been
5454
created yet, the previous values are None.
5555
5656
Args:
5757
instance: The current instance.
58-
predicates: A predicate for each field. It accepts the arguments (previous_value, value) and returns True if the values are as expected.
58+
predicates: A predicate for each field. The previous value is passed in as an arg and it should return True if the previous value is as expected.
5959
6060
Returns:
6161
If all the previous values are as expected.
@@ -65,7 +65,7 @@ def check_previous_values(
6565
get_previous_value = _generate_get_previous_value(instance)
6666

6767
return all(
68-
predicate(get_previous_value(field), getattr(instance, field))
68+
predicate(get_previous_value(field))
6969
for field, predicate in predicates.items()
7070
)
7171

0 commit comments

Comments
 (0)