From cd3335d49a2acf0f77baa8bc6302f971c2cf746e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Ja=C5=A1ek?= Date: Wed, 22 Jun 2022 06:35:23 +0200 Subject: [PATCH] fix issue with recursive publishing (#2358) when using internal destination auto publish macro as incoming rule on a stage SDESK-6526 --- apps/tasks.py | 17 +++++++++++------ superdesk/internal_destinations.py | 11 ++++++++--- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/apps/tasks.py b/apps/tasks.py index 6851388843..9a4a76fef3 100644 --- a/apps/tasks.py +++ b/apps/tasks.py @@ -19,7 +19,7 @@ from superdesk.users.services import current_user_has_privilege from superdesk.resource import Resource -from superdesk.errors import SuperdeskApiError, InvalidStateTransitionError +from superdesk.errors import StopDuplication, SuperdeskApiError, InvalidStateTransitionError from superdesk.notification import push_notification from superdesk.utc import utcnow from superdesk.metadata.utils import item_url @@ -115,11 +115,6 @@ def send_to(doc, update=None, desk_id=None, stage_id=None, user_id=None, default task["desk"] = destination_stage["desk"] task["stage"] = stage_id - if destination_stage: - apply_stage_rule(doc, update, destination_stage, MACRO_INCOMING, desk=desk, task=task) - if destination_stage.get("task_status"): - task["status"] = destination_stage["task_status"] - if update: update.setdefault("task", {}) update["task"].update(task) @@ -128,6 +123,14 @@ def send_to(doc, update=None, desk_id=None, stage_id=None, user_id=None, default doc["task"].update(task) doc["expiry"] = get_item_expiry(desk=desk, stage=destination_stage) + if destination_stage: + apply_stage_rule(doc, update, destination_stage, MACRO_INCOMING, desk=desk, task=task) + if destination_stage.get("task_status"): + if update: + update["task"]["status"] = destination_stage["task_status"] + else: + doc["task"]["status"] = destination_stage["task_status"] + def apply_stage_rule(doc, update, stage, rule_type, desk=None, task=None): macro_type = "{}_macro".format(rule_type) @@ -144,6 +147,8 @@ def apply_stage_rule(doc, update, stage, rule_type, desk=None, task=None): modified = compare_dictionaries(original_doc, doc) for i in modified: update[i] = doc[i] + except StopDuplication: + raise except Exception as ex: message = _("Error:{exception} in {rule_type} rule:{rule} for stage:{stage}").format( exception=str(ex), rule_type=rule_type, rule=macro.get("label"), stage=stage.get("name") diff --git a/superdesk/internal_destinations.py b/superdesk/internal_destinations.py index f7773d75bc..1290c6955e 100644 --- a/superdesk/internal_destinations.py +++ b/superdesk/internal_destinations.py @@ -45,14 +45,15 @@ class InternalDestinationsService(Service): pass -def handle_item_published(sender, item, **extra): +def handle_item_published(sender, item, desk=None, **extra): macros_service = get_resource_service("macros") archive_service = get_resource_service("archive") filters_service = get_resource_service("content_filters") destinations_service = get_resource_service(NAME) for dest in destinations_service.get(req=None, lookup={"is_active": True}): - if dest.get("desk") == item.get("task").get("desk"): + item_desk = desk["_id"] if desk is not None else item.get("task").get("desk") + if dest.get("desk") == item_desk: # item desk and internal destination are same then don't execute continue @@ -72,7 +73,11 @@ def handle_item_published(sender, item, **extra): item[SCHEDULE_SETTINGS] = {} new_item = deepcopy(item) - send_to(new_item, desk_id=dest["desk"], stage_id=dest.get("stage")) + + try: + send_to(new_item, desk_id=dest["desk"], stage_id=dest.get("stage")) + except StopDuplication: + continue if dest.get("macro"): macro = macros_service.get_macro_by_name(dest["macro"])