|
14 | 14 | FieldValue = t.TypeVar("FieldValue")
|
15 | 15 |
|
16 | 16 |
|
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 | +): |
18 | 21 | # 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. |
21 | 24 |
|
22 | 25 | Args:
|
23 | 26 | 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. |
25 | 28 |
|
26 | 29 | 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. |
29 | 31 | """
|
30 | 32 | # pylint: enable=line-too-long
|
31 | 33 |
|
32 |
| - for field, cls in fields.items(): |
| 34 | + for field, predicate in predicates.items(): |
33 | 35 | previous_value_key = PREVIOUS_VALUE_KEY.format(field=field)
|
34 | 36 |
|
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) |
37 | 39 | ):
|
38 | 40 | return False
|
39 | 41 |
|
|
0 commit comments