mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-11-04 03:27:12 -05:00 
			
		
		
		
	Fix: ensure only matched scheduled workflows are applied (#9580)
This commit is contained in:
		
							parent
							
								
									0d5a2b4382
								
							
						
					
					
						commit
						358db10fe3
					
				@ -649,11 +649,12 @@ def send_webhook(
 | 
				
			|||||||
def run_workflows(
 | 
					def run_workflows(
 | 
				
			||||||
    trigger_type: WorkflowTrigger.WorkflowTriggerType,
 | 
					    trigger_type: WorkflowTrigger.WorkflowTriggerType,
 | 
				
			||||||
    document: Document | ConsumableDocument,
 | 
					    document: Document | ConsumableDocument,
 | 
				
			||||||
 | 
					    workflow_to_run: Workflow | None = None,
 | 
				
			||||||
    logging_group=None,
 | 
					    logging_group=None,
 | 
				
			||||||
    overrides: DocumentMetadataOverrides | None = None,
 | 
					    overrides: DocumentMetadataOverrides | None = None,
 | 
				
			||||||
    original_file: Path | None = None,
 | 
					    original_file: Path | None = None,
 | 
				
			||||||
) -> tuple[DocumentMetadataOverrides, str] | None:
 | 
					) -> tuple[DocumentMetadataOverrides, str] | None:
 | 
				
			||||||
    """Run workflows which match a Document (or ConsumableDocument) for a specific trigger type.
 | 
					    """Run workflows which match a Document (or ConsumableDocument) for a specific trigger type or a single workflow if given.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Assignment or removal actions are either applied directly to the document or an overrides object. If an overrides
 | 
					    Assignment or removal actions are either applied directly to the document or an overrides object. If an overrides
 | 
				
			||||||
    object is provided, the function returns the object with the applied changes or None if no actions were applied and a string
 | 
					    object is provided, the function returns the object with the applied changes or None if no actions were applied and a string
 | 
				
			||||||
@ -1192,24 +1193,28 @@ def run_workflows(
 | 
				
			|||||||
    messages = []
 | 
					    messages = []
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    workflows = (
 | 
					    workflows = (
 | 
				
			||||||
        Workflow.objects.filter(enabled=True, triggers__type=trigger_type)
 | 
					        (
 | 
				
			||||||
        .prefetch_related(
 | 
					            Workflow.objects.filter(enabled=True, triggers__type=trigger_type)
 | 
				
			||||||
            "actions",
 | 
					            .prefetch_related(
 | 
				
			||||||
            "actions__assign_view_users",
 | 
					                "actions",
 | 
				
			||||||
            "actions__assign_view_groups",
 | 
					                "actions__assign_view_users",
 | 
				
			||||||
            "actions__assign_change_users",
 | 
					                "actions__assign_view_groups",
 | 
				
			||||||
            "actions__assign_change_groups",
 | 
					                "actions__assign_change_users",
 | 
				
			||||||
            "actions__assign_custom_fields",
 | 
					                "actions__assign_change_groups",
 | 
				
			||||||
            "actions__remove_tags",
 | 
					                "actions__assign_custom_fields",
 | 
				
			||||||
            "actions__remove_correspondents",
 | 
					                "actions__remove_tags",
 | 
				
			||||||
            "actions__remove_document_types",
 | 
					                "actions__remove_correspondents",
 | 
				
			||||||
            "actions__remove_storage_paths",
 | 
					                "actions__remove_document_types",
 | 
				
			||||||
            "actions__remove_custom_fields",
 | 
					                "actions__remove_storage_paths",
 | 
				
			||||||
            "actions__remove_owners",
 | 
					                "actions__remove_custom_fields",
 | 
				
			||||||
            "triggers",
 | 
					                "actions__remove_owners",
 | 
				
			||||||
 | 
					                "triggers",
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					            .order_by("order")
 | 
				
			||||||
 | 
					            .distinct()
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
        .order_by("order")
 | 
					        if workflow_to_run is None
 | 
				
			||||||
        .distinct()
 | 
					        else [workflow_to_run]
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for workflow in workflows:
 | 
					    for workflow in workflows:
 | 
				
			||||||
@ -1220,7 +1225,14 @@ def run_workflows(
 | 
				
			|||||||
            document.refresh_from_db()
 | 
					            document.refresh_from_db()
 | 
				
			||||||
            doc_tag_ids = list(document.tags.values_list("pk", flat=True))
 | 
					            doc_tag_ids = list(document.tags.values_list("pk", flat=True))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if matching.document_matches_workflow(document, workflow, trigger_type):
 | 
					        # If a workflow is supplied, we don't need to check if it matches
 | 
				
			||||||
 | 
					        matches = (
 | 
				
			||||||
 | 
					            matching.document_matches_workflow(document, workflow, trigger_type)
 | 
				
			||||||
 | 
					            if workflow_to_run is None
 | 
				
			||||||
 | 
					            else True
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if matches:
 | 
				
			||||||
            action: WorkflowAction
 | 
					            action: WorkflowAction
 | 
				
			||||||
            for action in workflow.actions.all():
 | 
					            for action in workflow.actions.all():
 | 
				
			||||||
                message = f"Applying {action} from {workflow}"
 | 
					                message = f"Applying {action} from {workflow}"
 | 
				
			||||||
 | 
				
			|||||||
@ -461,6 +461,7 @@ def check_scheduled_workflows():
 | 
				
			|||||||
                            )
 | 
					                            )
 | 
				
			||||||
                            continue
 | 
					                            continue
 | 
				
			||||||
                        run_workflows(
 | 
					                        run_workflows(
 | 
				
			||||||
                            WorkflowTrigger.WorkflowTriggerType.SCHEDULED,
 | 
					                            trigger_type=WorkflowTrigger.WorkflowTriggerType.SCHEDULED,
 | 
				
			||||||
                            document,
 | 
					                            workflow_to_run=workflow,
 | 
				
			||||||
 | 
					                            document=document,
 | 
				
			||||||
                        )
 | 
					                        )
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user