Skip to content

Commit 6d96843

Browse files
committed
add additional on_commit validation
1 parent 756be52 commit 6d96843

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

django_lifecycle/decorators.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from functools import wraps
2-
from typing import List
2+
from typing import List, Optional
33

44
from django_lifecycle import NotSet
55

@@ -10,7 +10,7 @@ class DjangoLifeCycleException(Exception):
1010
pass
1111

1212

13-
def _validate_hook_params(hook, when, when_any, has_changed):
13+
def _validate_hook_params(hook, when, when_any, has_changed, on_commit):
1414
if hook not in VALID_HOOKS:
1515
raise DjangoLifeCycleException(
1616
"%s is not a valid hook; must be one of %s" % (hook, VALID_HOOKS)
@@ -46,6 +46,13 @@ def _validate_hook_params(hook, when, when_any, has_changed):
4646
raise DjangoLifeCycleException(
4747
"Can pass either 'when' or 'when_any' but not both"
4848
)
49+
50+
if on_commit is not None:
51+
if not hook.startswith("after_"):
52+
raise DjangoLifeCycleException("'on_commit' hook param is only valid with AFTER_* hooks")
53+
54+
if not isinstance(on_commit, bool):
55+
raise DjangoLifeCycleException("'on_commit' hook param must be a boolean")
4956

5057

5158
def hook(
@@ -58,8 +65,9 @@ def hook(
5865
is_not=NotSet,
5966
was_not=NotSet,
6067
changes_to=NotSet,
68+
on_commit: Optional[bool] = None
6169
):
62-
_validate_hook_params(hook, when, when_any, has_changed)
70+
_validate_hook_params(hook, when, when_any, has_changed, on_commit)
6371

6472
def decorator(hooked_method):
6573
if not hasattr(hooked_method, "_hooked"):
@@ -83,6 +91,7 @@ def func(*args, **kwargs):
8391
"was": was,
8492
"was_not": was_not,
8593
"changes_to": changes_to,
94+
"on_commit": on_commit
8695
}
8796
)
8897

0 commit comments

Comments
 (0)