@@ -971,7 +971,7 @@ def _get_form_field_email(form_data: dict, email_field: str) -> str | None:
971971
972972
973973def _collect_notification_recipients (
974- notif , form_data : dict , submission = None
974+ notif , form_data : dict , submission = None , task = None
975975) -> list [str ]:
976976 """Return the deduplicated list of recipient emails for a notification rule.
977977
@@ -983,6 +983,10 @@ def _collect_notification_recipients(
983983 4. notify_stage_assignees → ApprovalTask.assigned_to.email
984984 5. notify_stage_groups → all users in the stage's approval groups
985985 6. notify_groups (M2M) → all users in explicitly-listed groups
986+
987+ When ``use_triggering_stage`` is True and a *task* is provided, the
988+ task's ``workflow_stage`` is used to scope stage-based recipients
989+ instead of ``notif.stage``.
986990 """
987991 recipients : list [str ] = []
988992
@@ -1004,6 +1008,18 @@ def _add(email: str | None) -> None:
10041008 if addr and "@" in addr :
10051009 _add (addr )
10061010
1011+ # Resolve the effective stage for stage-scoped recipient sources.
1012+ # Priority: use_triggering_stage (from task) > explicit stage FK > all stages.
1013+ _triggering_stage_id = None
1014+ if getattr (notif , "use_triggering_stage" , False ) and task is not None :
1015+ _triggering_stage_id = getattr (task , "workflow_stage_id" , None )
1016+
1017+ def _effective_stage_id ():
1018+ """Return the stage ID to scope to, or None for all stages."""
1019+ if _triggering_stage_id :
1020+ return _triggering_stage_id
1021+ return getattr (notif , "stage_id" , None )
1022+
10071023 # 4. Stage assignees (NotificationRule only)
10081024 if getattr (notif , "notify_stage_assignees" , False ) and submission is not None :
10091025 qs = (
@@ -1012,9 +1028,9 @@ def _add(email: str | None) -> None:
10121028 .exclude (workflow_stage__assignee_form_field__isnull = True )
10131029 .exclude (workflow_stage__assignee_form_field = "" )
10141030 )
1015- # If rule is stage-scoped, limit to that stage
1016- if getattr ( notif , "stage_id" , None ) :
1017- qs = qs .filter (workflow_stage_id = notif . stage_id )
1031+ eff_stage = _effective_stage_id ()
1032+ if eff_stage :
1033+ qs = qs .filter (workflow_stage_id = eff_stage )
10181034 for email in qs .values_list ("assigned_to__email" , flat = True ):
10191035 _add (email )
10201036
@@ -1023,9 +1039,9 @@ def _add(email: str | None) -> None:
10231039 from django .contrib .auth import get_user_model
10241040
10251041 user_model = get_user_model ()
1026- # Determine which stages to include
1027- if getattr ( notif , "stage_id" , None ) :
1028- stage_ids = [notif . stage_id ]
1042+ eff_stage = _effective_stage_id ()
1043+ if eff_stage :
1044+ stage_ids = [eff_stage ]
10291045 else :
10301046 # All stages in this workflow
10311047 from .models import WorkflowStage
@@ -1152,15 +1168,29 @@ def send_notification_rules(
11521168 workflow = getattr (submission .form_definition , "workflow" , None )
11531169 hide_approval_history = bool (getattr (workflow , "hide_approval_history" , False ))
11541170
1155- rules = (
1156- NotificationRule .objects .filter (
1157- workflow__form_definition = submission .form_definition ,
1158- event = event ,
1159- )
1171+ # Resolve the task (if any) to scope rules to its workflow
1172+ task = None
1173+ if task_id :
1174+ try :
1175+ task = ApprovalTask .objects .select_related ("workflow_stage" ).get (id = task_id )
1176+ except ApprovalTask .DoesNotExist :
1177+ pass
1178+
1179+ rules_qs = (
1180+ NotificationRule .objects .filter (event = event )
11601181 .select_related ("workflow" , "stage" )
11611182 .prefetch_related ("notify_groups" )
11621183 )
11631184
1185+ # Scope rules to the specific workflow that owns the triggering task,
1186+ # rather than firing rules from every workflow on the form.
1187+ if task and getattr (task , "workflow_stage_id" , None ):
1188+ rules_qs = rules_qs .filter (workflow_id = task .workflow_stage .workflow_id )
1189+ else :
1190+ rules_qs = rules_qs .filter (workflow__form_definition = submission .form_definition )
1191+
1192+ rules = rules_qs
1193+
11641194 default_subject_tpl = _EVENT_DEFAULT_SUBJECTS .get (
11651195 event , "{form_name} (ID {submission_id})"
11661196 )
@@ -1190,7 +1220,7 @@ def send_notification_rules(
11901220
11911221 # Resolve recipients
11921222 recipients = _collect_notification_recipients (
1193- rule , form_data , submission = submission
1223+ rule , form_data , submission = submission , task = task
11941224 )
11951225 if not recipients :
11961226 logger .info (
0 commit comments