Skip to content

Commit

Permalink
add assigned user/desk info to planning json
Browse files Browse the repository at this point in the history
CPCN-502
  • Loading branch information
petrjasek committed Dec 13, 2023
1 parent 559f72c commit e167c77
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
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"))

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

0 comments on commit e167c77

Please sign in to comment.