From b3077a2e6cc0caa20aa99fbdb5c27e08c59ac18d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Ja=C5=A1ek?= Date: Tue, 19 Dec 2023 09:56:14 +0100 Subject: [PATCH] add assigned user/desk info to planning json (#1888) * add assigned user/desk info to planning json CPCN-502 --- README.md | 3 ++ .../output_formatters/json_planning.py | 26 ++++++++++-- .../output_formatters/json_planning_test.py | 41 +++++++++++++++++++ 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5667606e5..27572b8e6 100644 --- a/README.md +++ b/README.md @@ -176,6 +176,9 @@ Below sections include the config options that can be defined in settings.py. * PLANNING_SEND_NOTIFICATION_FOR_SELF_ASSIGNMENT * Defaults to false * If true, sends a notification to a user on creating an assignment that is assigned to themselves +* PLANNING_JSON_ASSIGNED_INFO_EXTENDED + * Defaults to `false` + * If `true`, it will add to planning JSON output additional info for coverages like assigned desk name/email and assigned user name/email. ### Authoring Config * PLANNING_CHECK_FOR_ASSIGNMENT_ON_PUBLISH diff --git a/server/planning/output_formatters/json_planning.py b/server/planning/output_formatters/json_planning.py index 479c55545..3f4627af9 100644 --- a/server/planning/output_formatters/json_planning.py +++ b/server/planning/output_formatters/json_planning.py @@ -9,6 +9,7 @@ # at https://www.sourcefabric.org/superdesk/license +from flask import current_app as app from superdesk.publish.formatters import Formatter import superdesk import json @@ -178,12 +179,11 @@ def _expand_delivery(self, coverage): if delivery.get("item_state") == CONTENT_STATE.PUBLISHED: item_never_published = False - if item_never_published: - deliveries = [] - return deliveries, assignment.get("assigned_to").get("state") def _expand_coverage_contacts(self, coverage): + EXTENDED_INFO = bool(app.config.get("PLANNING_JSON_ASSIGNED_INFO_EXTENDED")) + if (coverage.get("assigned_to") or {}).get("contact"): expanded_contacts = expand_contact_info([coverage["assigned_to"]["contact"]]) if expanded_contacts: @@ -194,8 +194,26 @@ def _expand_coverage_contacts(self, coverage): if (coverage.get("assigned_to") or {}).get("user"): user = get_resource_service("users").find_one(req=None, _id=coverage["assigned_to"]["user"]) - if user: + if user and not user.get("private"): coverage["assigned_user"] = { "first_name": user.get("first_name"), "last_name": user.get("last_name"), + "display_name": user.get("display_name"), + } + + if EXTENDED_INFO: + coverage["assigned_user"].update( + email=user.get("email"), + ) + + if (coverage.get("assigned_to") or {}).get("desk"): + desk = get_resource_service("desks").find_one(req=None, _id=coverage["assigned_to"]["desk"]) + if desk: + coverage["assigned_desk"] = { + "name": desk.get("name"), } + + if EXTENDED_INFO: + coverage["assigned_desk"].update( + email=desk.get("email"), + ) diff --git a/server/planning/tests/output_formatters/json_planning_test.py b/server/planning/tests/output_formatters/json_planning_test.py index 3ff9aa94f..d4365786b 100644 --- a/server/planning/tests/output_formatters/json_planning_test.py +++ b/server/planning/tests/output_formatters/json_planning_test.py @@ -318,3 +318,44 @@ 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_assigned_desk_user(self): + with self.app.app_context(): + item = deepcopy(self.item) + desk_id = ObjectId() + user_id = ObjectId() + + item["coverages"][0]["assigned_to"].update( + desk=desk_id, + user=user_id, + ) + + self.app.data.insert( + "desks", + [{"_id": desk_id, "name": "sports", "email": "sports@example.com"}], + ) + + self.app.data.insert("users", [{"_id": user_id, "display_name": "John Doe", "email": "john@example.com"}]) + + formatter = JsonPlanningFormatter() + with mock.patch.dict(self.app.config, {"PLANNING_JSON_ASSIGNED_INFO_EXTENDED": True}): + output = formatter.format(item, {"name": "Test Subscriber"})[0] + output_item = json.loads(output[1]) + coverage = output_item["coverages"][0] + assert coverage["assigned_user"] == { + "first_name": None, + "last_name": None, + "display_name": "John Doe", + "email": "john@example.com", + } + assert coverage["assigned_desk"] == { + "name": "sports", + "email": "sports@example.com", + } + + # without config + output = formatter.format(item, {"name": "Test Subscriber"})[0] + output_item = json.loads(output[1]) + coverage = output_item["coverages"][0] + assert "email" not in coverage["assigned_user"] + assert "email" not in coverage["assigned_desk"]