11from functools import wraps
2- from typing import List
2+ from typing import List , Optional
33
44from 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
5158def 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