Skip to content

Commit

Permalink
[SDESK-7166] fix: Use ingest_id for coverage content when its auto …
Browse files Browse the repository at this point in the history
…published (#1903)

* [SDESK-7166] fix: Use `ingest_id` for coverage content when its auto published

* fix front-end tests

* fix: Don't process coverages on updates if not provided

* fix lint error

* Revert "fix front-end tests"

This reverts commit e6d4c7e.
  • Loading branch information
MarkLark86 authored Jan 24, 2024
1 parent 393bae8 commit 1655909
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
18 changes: 18 additions & 0 deletions server/planning/output_formatters/json_planning.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from superdesk.publish.formatters import Formatter
import superdesk
from apps.archive.common import ARCHIVE
import json
from superdesk.utils import json_serialize_datetime_objectId
from copy import deepcopy
Expand Down Expand Up @@ -170,11 +171,28 @@ def _expand_delivery(self, coverage):
)
deliveries = list(delivery_service.get(req=None, lookup={"coverage_id": coverage.get("coverage_id")}))

# Get the associated article(s) linked to the coverage(s)
query = {"$and": [{"_id": {"$in": [item["item_id"] for item in deliveries]}}]}
articles = {item["_id"]: item for item in get_resource_service(ARCHIVE).get_from_mongo(req=None, lookup=query)}

# Check to see if in this delivery chain, whether the item has been published at least once
item_never_published = True
for delivery in deliveries:
for f in remove_fields:
delivery.pop(f, None)

# TODO: This is a hack, need to find a better way of doing this
# If the linked article was auto-published, then use the ``ingest_id`` for the article ID
# This is required when the article was published using the ``NewsroomNinjsFormatter``
# Otherwise this coverage in Newshub would point to a non-existing wire item
article = articles.get(delivery["item_id"])
if (
article is not None
and article.get("ingest_id")
and (article.get("auto_publish") or (article.get("extra") or {}).get("publish_ingest_id_as_guid"))
):
delivery["item_id"] = article["ingest_id"]

if delivery.get("item_state") == CONTENT_STATE.PUBLISHED:
item_never_published = False

Expand Down
9 changes: 9 additions & 0 deletions server/planning/planning/planning.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,9 @@ def get_all_items_in_relationship(self, item):
return all_items

def remove_coverages(self, updates, original):
if "coverages" not in updates:
return

for coverage in (original or {}).get("coverages") or []:
updated_coverage = next(
(
Expand Down Expand Up @@ -525,6 +528,9 @@ def remove_coverage_entity(self, coverage_entity, original_planning, entity_type
self._create_update_assignment(original_planning, {}, updated_coverage_entity, coverage_entity)

def add_coverages(self, updates, original):
if "coverages" not in updates:
return

planning_date = original.get("planning_date") or updates.get("planning_date")
original_coverage_ids = [
coverage["coverage_id"] for coverage in original.get("coverages") or [] if coverage.get("coverage_id")
Expand Down Expand Up @@ -606,6 +612,9 @@ def update_scheduled_updates(self, updates, original, coverage, original_coverag
self._create_update_assignment(original, updates, s, original_scheduled_update, coverage)

def update_coverages(self, updates, original):
if "coverages" not in updates:
return

for coverage in updates.get("coverages") or []:
coverage_id = coverage.get("coverage_id")
original_coverage = next(
Expand Down
32 changes: 32 additions & 0 deletions server/planning/tests/output_formatters/json_planning_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,35 @@ def test_matching_product_ids(self):
output = formatter.format(item, {"name": "Test Subscriber"})[0]
output_item = json.loads(output[1])
self.assertEqual(output_item["products"], [{"code": "prod-type-planning", "name": "planning-only"}])

def test_expand_delivery_uses_ingest_id(self):
self.app.data.insert("assignments", self.assignment)
self.app.data.insert("delivery", self.delivery)
formatter = JsonPlanningFormatter()
item_id = self.delivery[0]["item_id"]
ingest_id = "urn:newsml:localhost:2024-01-24-ingest-1"
article = {
"_id": item_id,
"type": "text",
"headline": "test headline",
"slugline": "test slugline",
"ingest_id": ingest_id,
}

self.app.data.insert("archive", [article])
deliveries, _ = formatter._expand_delivery(deepcopy(self.item["coverages"][0]))
self.assertNotEqual(deliveries[0]["item_id"], ingest_id)

article = self.app.data.find_one("archive", req=None, _id=item_id)
self.app.data.update("archive", item_id, {"auto_publish": True}, article)
deliveries, _ = formatter._expand_delivery(deepcopy(self.item["coverages"][0]))
self.assertEqual(deliveries[0]["item_id"], ingest_id)

article = self.app.data.find_one("archive", req=None, _id=item_id)
updates = {
"auto_publish": None,
"extra": {"publish_ingest_id_as_guid": True},
}
self.app.data.update("archive", item_id, updates, article)
deliveries, _ = formatter._expand_delivery(deepcopy(self.item["coverages"][0]))
self.assertEqual(deliveries[0]["item_id"], ingest_id)

0 comments on commit 1655909

Please sign in to comment.