From 3630cfbdc42caeb32cad8b9c64abd1f13ef591ae Mon Sep 17 00:00:00 2001 From: Petr Jasek Date: Tue, 17 Sep 2024 15:30:49 +0200 Subject: [PATCH] fix error when ingesting cancelled event with assignments it was checking session data which failed during ingest STTNHUB-361 --- server/planning/assignments/assignments.py | 2 +- server/planning/common.py | 3 ++ server/planning/planning/planning.py | 35 ++++++++--------- server/planning/planning/planning_post.py | 2 + .../planning/tests/ingest_cancelled_test.py | 38 +++++++++++++++++++ 5 files changed, 62 insertions(+), 18 deletions(-) create mode 100644 server/planning/tests/ingest_cancelled_test.py diff --git a/server/planning/assignments/assignments.py b/server/planning/assignments/assignments.py index 76790564a..f285f6f41 100644 --- a/server/planning/assignments/assignments.py +++ b/server/planning/assignments/assignments.py @@ -1195,7 +1195,7 @@ def on_delete(self, doc): """ Validate that we have a lock on the Assignment and it's associated Planning item """ - if doc.get("_to_delete") is True: + if doc.get("_to_delete") is True or not request: # Already marked for delete - no validation needed (could be the background job) return diff --git a/server/planning/common.py b/server/planning/common.py index 717dcbdc0..69a2e850e 100644 --- a/server/planning/common.py +++ b/server/planning/common.py @@ -379,9 +379,12 @@ def update_post_item(updates, original): # From item actions pub_status = POST_STATE.USABLE + print("IN", updates, pub_status) + if pub_status is not None: if original.get(ITEM_TYPE): resource_name = "events_post" if original.get(ITEM_TYPE) == "event" else "planning_post" + print("RESOURCE", resource_name) item_post_service = get_resource_service(resource_name) doc = { "etag": updates.get("_etag"), diff --git a/server/planning/planning/planning.py b/server/planning/planning/planning.py index 8712345f6..fac3e8ad2 100644 --- a/server/planning/planning/planning.py +++ b/server/planning/planning/planning.py @@ -15,7 +15,7 @@ import logging from datetime import datetime -from flask import json, current_app as app +from flask import json, current_app as app, request from eve.methods.common import resolve_document_etag import superdesk @@ -1274,23 +1274,24 @@ def delete_assignments_for_coverages(self, coverages, notify=True): if original_assigment: assignment_service.system_update(ObjectId(assign_id), {"_to_delete": True}, original_assigment) - session_id = get_auth().get("_id") - user_id = get_user().get(config.ID_FIELD) - if len(deleted_assignments) > 0: - push_notification( - "assignments:delete", - items=deleted_assignments, - session=session_id, - user=user_id, - ) + if request: + session_id = get_auth().get("_id") + user_id = get_user().get(config.ID_FIELD) + if len(deleted_assignments) > 0: + push_notification( + "assignments:delete", + items=deleted_assignments, + session=session_id, + user=user_id, + ) - if len(failed_assignments) > 0 and notify: - push_notification( - "assignments:delete:fail", - items=failed_assignments, - session=session_id, - user=user_id, - ) + if len(failed_assignments) > 0 and notify: + push_notification( + "assignments:delete:fail", + items=failed_assignments, + session=session_id, + user=user_id, + ) def get_expired_items(self, expiry_datetime, spiked_planning_only=False): """Get the expired items diff --git a/server/planning/planning/planning_post.py b/server/planning/planning/planning_post.py index 6bc81ae47..485839060 100644 --- a/server/planning/planning/planning_post.py +++ b/server/planning/planning/planning_post.py @@ -50,6 +50,7 @@ class PlanningPostResource(PlanningResource): class PlanningPostService(BaseService): def create(self, docs, **kwargs): + print("IN CREATE", docs) ids = [] assignments_to_delete = [] for doc in docs: @@ -72,6 +73,7 @@ def create(self, docs, **kwargs): self.post_planning(plan, doc["pubstatus"], assignments_to_delete, **kwargs) ids.append(doc["planning"]) + print("DELETE", assignments_to_delete) get_resource_service("planning").delete_assignments_for_coverages(assignments_to_delete) return ids diff --git a/server/planning/tests/ingest_cancelled_test.py b/server/planning/tests/ingest_cancelled_test.py new file mode 100644 index 000000000..be33985b8 --- /dev/null +++ b/server/planning/tests/ingest_cancelled_test.py @@ -0,0 +1,38 @@ +from flask import request +from planning.tests import TestCase +from planning.common import update_post_item + + +class IngestCancelledTestCase(TestCase): + BEHAVE = True + + def test_ingest_cancelled_event(self): + assignments = [ + {"planning_item": "p1", "coverage_item": "c1"}, + ] + + self.app.data.insert("assignments", assignments) + planning = { + "_id": "p1", + "name": "planning item", + "type": "planning", + "coverages": [ + { + "coverage_id": "c1", + "planning": {}, + "assigned_to": { + "assignment_id": assignments[0]["_id"], + }, + }, + ], + } + + self.app.data.insert("planning", [planning]) + + assert not request + + with self.app.app_context(): + update_post_item({"pubstatus": "cancelled"}, planning) + + cursor, count = self.app.data.find("assignments", req=None, lookup={}) + assert count == 0