11from __future__ import annotations
22
33import operator
4+ from django_lifecycle import types
45from dataclasses import dataclass
56from functools import reduce
67from functools import wraps
@@ -23,6 +24,11 @@ class DjangoLifeCycleException(Exception):
2324@dataclass (order = False )
2425class HookConfig (Validations ):
2526 hook : str
27+ on_commit : bool = False
28+ priority : int = DEFAULT_PRIORITY
29+ condition : Optional [types .Condition ] = None
30+
31+ # Legacy parameters
2632 when : Optional [str ] = None
2733 when_any : Optional [List [str ]] = None
2834 was : Any = "*"
@@ -31,11 +37,28 @@ class HookConfig(Validations):
3137 is_not : Any = NotSet
3238 was_not : Any = NotSet
3339 changes_to : Any = NotSet
34- on_commit : bool = False
35- priority : int = DEFAULT_PRIORITY
3640
37- @property
38- def condition (self ) -> Callable :
41+ def __post_init__ (self ):
42+ super ().__post_init__ ()
43+
44+ if self .condition is None :
45+ self .condition = self ._get_condition_from_legacy_parameters ()
46+
47+ def _legacy_parameters_have_been_passed (self ) -> bool :
48+ return any (
49+ [
50+ self .when is not None ,
51+ self .when_any is not None ,
52+ self .was != "*" ,
53+ self .is_now != "*" ,
54+ self .has_changed is not None ,
55+ self .is_not is not NotSet ,
56+ self .was_not is not NotSet ,
57+ self .changes_to is not NotSet ,
58+ ]
59+ )
60+
61+ def _get_condition_from_legacy_parameters (self ) -> Callable :
3962 if self .when :
4063 return When (
4164 when = self .when ,
@@ -140,9 +163,16 @@ def validate_when_and_when_any(self):
140163 "Can pass either 'when' or 'when_any' but not both"
141164 )
142165
166+ def validate_condition_and_legacy_parameters_are_not_combined (self ):
167+ if self .condition is not None and self ._legacy_parameters_have_been_passed ():
168+ raise DjangoLifeCycleException (
169+ "Legacy parameters (when, when_any, ...) can't be used together with condition"
170+ )
171+
143172 def validate (self ):
144173 self .validate_when_and_when_any ()
145174 self .validate_on_commit_only_for_after_hooks ()
175+ self .validate_condition_and_legacy_parameters_are_not_combined ()
146176
147177 def __lt__ (self , other ):
148178 if not isinstance (other , HookConfig ):
0 commit comments