Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add assigned user/desk info to planning json #1888

Merged
merged 2 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,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
Expand Down
26 changes: 22 additions & 4 deletions server/planning/output_formatters/json_planning.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to add this to the README.md file (maybe under a new Publishing heading: https://github.com/superdesk/superdesk-planning?tab=readme-ov-file#config).
That way Planning configs are at least documented somewhere


if (coverage.get("assigned_to") or {}).get("contact"):
expanded_contacts = expand_contact_info([coverage["assigned_to"]["contact"]])
if expanded_contacts:
Expand All @@ -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"),
)
41 changes: 41 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,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": "[email protected]"}],
)

self.app.data.insert("users", [{"_id": user_id, "display_name": "John Doe", "email": "[email protected]"}])

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": "[email protected]",
}
assert coverage["assigned_desk"] == {
"name": "sports",
"email": "[email protected]",
}

# 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"]
Loading