Skip to content

Commit

Permalink
add assigned user/desk info to planning json (#1888)
Browse files Browse the repository at this point in the history
* add assigned user/desk info to planning json

CPCN-502
  • Loading branch information
petrjasek committed Jan 18, 2024
1 parent ab53049 commit b3077a2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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"))

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 b3077a2

Please sign in to comment.