Skip to content

Commit

Permalink
[STT-15] backend: Support AddAsEvent when Planning already has an Eve…
Browse files Browse the repository at this point in the history
…nt linked (superdesk#2015)
  • Loading branch information
MarkLark86 authored Jul 7, 2024
1 parent 7df17ff commit 9c9a101
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 2 deletions.
78 changes: 78 additions & 0 deletions server/features/events.feature
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,84 @@ Feature: Events
}
"""

@auth
Scenario: Link new Event as secondary to a Planning item
Given "planning"
"""
[{
"_id": "plan1",
"guid": "plan1",
"slugline": "TestEvent",
"state": "draft",
"lock_user": "#CONTEXT_USER_ID#",
"lock_session": "#SESSION_ID#",
"lock_action": "add_as_event",
"lock_time": "#DATE#",
"planning_date": "2016-01-02"
}]
"""
When we post to "events"
"""
{
"guid": "event_1",
"name": "Primary Event 1",
"slugline": "event-1",
"_planning_item": "plan1",
"dates": {
"start": "2029-11-21T12:00:00.000Z",
"end": "2029-11-21T14:00:00.000Z",
"tz": "Australia/Sydney"
}
}
"""
Then we get OK response
When we get "/planning/plan1"
Then we get existing resource
"""
{"related_events": [{"_id": "#events._id#", "link_type": "primary"}]}
"""
When we post to "events"
"""
{
"guid": "event_2",
"name": "Primary Event 2",
"slugline": "event-2",
"_planning_item": "plan1",
"dates": {
"start": "2029-11-21T12:00:00.000Z",
"end": "2029-11-21T14:00:00.000Z",
"tz": "Australia/Sydney"
}
}
"""
Then we get OK response
When we get "/planning/plan1"
Then we get existing resource
"""
{"related_events": [
{"_id": "event_1", "link_type": "primary"},
{"_id": "event_2", "link_type": "secondary"}
]}
"""

@auth
Scenario: Attemps to link Event to non existing Planning fails
When we post to "events"
"""
{
"guid": "event_1",
"name": "Primary Event 1",
"slugline": "event-1",
"_planning_item": "plan1",
"dates": {
"start": "2029-11-21T12:00:00.000Z",
"end": "2029-11-21T14:00:00.000Z",
"tz": "Australia/Sydney"
}
}
"""
Then we get error 400

@auth
Scenario: Fails to link a new Event to a Planning Item if another use holds the Planning lock
Given we have sessions "/sessions"
Expand Down
40 changes: 40 additions & 0 deletions server/features/planning.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4323,3 +4323,43 @@ Feature: Planning
}
"""
Then we get OK response

@auth
Scenario: Planning can only have 1 primary linked event
Given "events"
"""
[{
"guid": "event_1",
"name": "Primary Event 1",
"slugline": "event-1",
"dates": {
"start": "2029-11-21T12:00:00.000Z",
"end": "2029-11-21T14:00:00.000Z",
"tz": "Australia/Sydney"
}
}, {
"guid": "event_2",
"name": "Primary Event 2",
"slugline": "event-2",
"dates": {
"start": "2029-11-21T12:00:00.000Z",
"end": "2029-11-21T14:00:00.000Z",
"tz": "Australia/Sydney"
}
}]
"""
When we post to "planning"
"""
{
"_id": "plan1",
"guid": "plan1",
"slugline": "TestEvent",
"state": "draft",
"planning_date": "2016-01-02".
"related_events": [
{"_id": "event_1", "link_type": "primary"},
{"_id": "event_2", "link_type": "primary"}
]
}
"""
Then we get error 400
10 changes: 8 additions & 2 deletions server/planning/events/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
update_ingest_on_patch,
TEMP_ID_PREFIX,
)
from planning.utils import get_related_planning_for_events
from planning.utils import get_related_planning_for_events, get_related_event_ids_for_planning
from .events_base_service import EventsBaseService
from .events_schema import events_schema
from .events_sync import sync_event_metadata_with_planning_items
Expand Down Expand Up @@ -735,7 +735,7 @@ def get_recurring_timeline(self, selected, spiked=False):
return events_base_service.get_recurring_timeline(selected, postponed=True, spiked=spiked)

@staticmethod
def _link_to_planning(event: Event, link_type: PLANNING_RELATED_EVENT_LINK_TYPE = "primary"):
def _link_to_planning(event: Event):
"""
Links an Event to an existing Planning Item
Expand All @@ -747,7 +747,13 @@ def _link_to_planning(event: Event, link_type: PLANNING_RELATED_EVENT_LINK_TYPE
event_id = event[config.ID_FIELD]
planning_item = planning_service.find_one(req=None, _id=plan_id)

if not planning_item:
raise SuperdeskApiError.badRequestError("Planning item not found")

updates = {"related_events": planning_item.get("related_events") or []}
link_type: PLANNING_RELATED_EVENT_LINK_TYPE = (
"primary" if not len(get_related_event_ids_for_planning(planning_item, "primary")) else "secondary"
)
related_planning = PlanningRelatedEventLink(_id=event_id, link_type=link_type)
updates["related_events"].append(related_planning)

Expand Down
1 change: 1 addition & 0 deletions server/planning/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ def _remove_unwanted_fields(self, update):
update_copy.pop(field, None)

return update_copy
return update

def _save_history(self, item, update, operation):
raise NotImplementedError()
3 changes: 3 additions & 0 deletions server/planning/planning/planning.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ def validate_planning(self, updates, original=None):

sanitize_input_data(updates)

if len(get_related_event_ids_for_planning(updates, "primary")) > 1:
raise SuperdeskApiError.badRequestError("Only 1 primary linked event is allowed")

# Validate if agendas being added are enabled agendas
agenda_service = get_resource_service("agenda")
for agenda_id in updates.get("agendas", []):
Expand Down

0 comments on commit 9c9a101

Please sign in to comment.