Skip to content

Commit

Permalink
add publish_event and publish_planning signals (#699)
Browse files Browse the repository at this point in the history
STTNHUB-257
  • Loading branch information
petrjasek authored Dec 13, 2023
1 parent 65dc1ff commit 681516c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
8 changes: 8 additions & 0 deletions newsroom/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ def publish_event(event, orig):
# This can happen when pushing a Planning item before linking to an Event
service.system_update(plan["_id"], {"event_id": _id}, plan)

signals.publish_event.send(app._get_current_object(), item=agenda, is_new=True)
_id = service.post([agenda])[0]
else:
# replace the original document
Expand Down Expand Up @@ -276,6 +277,11 @@ def publish_event(event, orig):
set_agenda_metadata_from_event(updates, event, False)

if updates:
updated = orig.copy()
updated.update(updates)
signals.publish_event.send(
app._get_current_object(), item=updated, updates=updates, orig=orig, is_new=False
)
service.patch(orig["_id"], updates)
updates["_id"] = orig["_id"]
superdesk.get_resource_service("agenda").notify_agenda_update(updates, orig)
Expand Down Expand Up @@ -322,9 +328,11 @@ def publish_planning_item(planning, orig):
# Setting ``_id`` of Agenda to be equal to the Planning item if there's no Event ID
agenda.setdefault("_id", planning["guid"])
agenda.setdefault("guid", planning["guid"])
signals.publish_planning.send(app._get_current_object(), item=agenda, is_new=new_plan)
return service.post([agenda])[0]
else:
# Replace the original
signals.publish_planning.send(app._get_current_object(), item=agenda, is_new=new_plan)
service.patch(agenda["_id"], agenda)
return agenda["_id"]

Expand Down
3 changes: 3 additions & 0 deletions newsroom/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
signals = Namespace()

publish_item = signals.signal("publish-item")
publish_event = signals.signal("publish-event")
publish_planning = signals.signal("publish-planning")


#:
#: ..versionadded:: 2.4
Expand Down
29 changes: 29 additions & 0 deletions tests/core/test_push_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from flask import json
from superdesk import get_resource_service

import newsroom.signals

from newsroom.tests.users import (
ADMIN_USER_ID,
Expand Down Expand Up @@ -1389,3 +1390,31 @@ def test_push_plan_with_date_before_event_start(client, app):
parsed = get_entity_or_404(planning["guid"], "agenda")
assert 1 == len(parsed["planning_items"])
assert 2 == len(parsed["coverages"])


def test_push_planning_signal(client, app):
def on_push_planning(sender, item, is_new, **kwargs):
item["dates"]["all_day"] = True
assert is_new

newsroom.signals.publish_planning.connect(on_push_planning)

planning = deepcopy(test_planning)
client.post("/push", data=json.dumps(planning), content_type="application/json")

parsed = get_entity_or_404(planning["guid"], "agenda")
assert parsed and parsed["dates"]["all_day"]


def test_push_events_signal(client, app):
def on_push_event(sender, item, is_new, **kwargs):
item["dates"]["all_day"] = True
assert is_new

newsroom.signals.publish_event.connect(on_push_event)

event = deepcopy(test_event)
client.post("/push", data=json.dumps(event), content_type="application/json")

parsed = get_entity_or_404(event["guid"], "agenda")
assert parsed and parsed["dates"]["all_day"]

0 comments on commit 681516c

Please sign in to comment.