Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
petrjasek committed Jul 24, 2024
1 parent 583aac2 commit df86506
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 8 deletions.
86 changes: 85 additions & 1 deletion server/features/events.feature
Original file line number Diff line number Diff line change
Expand Up @@ -1671,4 +1671,88 @@ Feature: Events
"lock_session": "#SESSION_ID#"
}
"""
Then we get OK response
Then we get OK response

@auth
Scenario: Link new Event with many_secondary link method
Given config update
"""
{"PLANNING_EVENT_LINK_METHOD": "many_secondary"}
"""
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": "secondary"}]}
"""

@wip
@auth
Scenario: Link new Event with one_primary link method
Given config update
"""
{"PLANNING_EVENT_LINK_METHOD": "one_primary"}
"""
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"}]}
"""

2 changes: 1 addition & 1 deletion server/features/events_cancel.feature
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ Feature: Events Cancel
Scenario: Cancelling an Event does not cancel Planning item with secondary link
Given config update
"""
{"PLANNING_EVENT_LINK_METHOD": "secondary"}
{"PLANNING_EVENT_LINK_METHOD": "many_secondary"}
"""
Given we have sessions "/sessions"
And "events"
Expand Down
12 changes: 10 additions & 2 deletions server/planning/events/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@
update_ingest_on_patch,
TEMP_ID_PREFIX,
)
from planning.utils import get_related_planning_for_events, get_related_event_ids_for_planning
from planning.utils import (
get_planning_event_link_method,
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 @@ -751,8 +755,12 @@ def _link_to_planning(event: Event):
raise SuperdeskApiError.badRequestError("Planning item not found")

updates = {"related_events": planning_item.get("related_events") or []}
event_link_method = get_planning_event_link_method()
link_type: PLANNING_RELATED_EVENT_LINK_TYPE = (
"primary" if not len(get_related_event_ids_for_planning(planning_item, "primary")) else "secondary"
"primary"
if not len(get_related_event_ids_for_planning(planning_item, "primary"))
and event_link_method in ("one_primary", "one_primary_many_secondary")
else "secondary"
)
related_planning = PlanningRelatedEventLink(_id=event_id, link_type=link_type)
updates["related_events"].append(related_planning)
Expand Down
8 changes: 6 additions & 2 deletions server/planning/planning/planning.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""Superdesk Planning"""

from typing import Dict, Any, Optional, List
from typing_extensions import assert_never
from copy import deepcopy
import logging
from datetime import datetime
Expand Down Expand Up @@ -74,6 +75,7 @@
from planning.signals import planning_created, planning_ingested
from .planning_schema import planning_schema
from planning.utils import (
get_planning_event_link_method,
get_related_planning_for_events,
get_related_event_links_for_planning,
get_related_event_ids_for_planning,
Expand Down Expand Up @@ -361,9 +363,9 @@ def validate_planning(self, updates, original=None):
if next_schedule and next_schedule["planning"]["scheduled"] > scheduled_update["planning"]["scheduled"]:
raise SuperdeskApiError(message="Scheduled updates of a coverage must be after the previous update")

def _validate_events_links(self, updates):
def _validate_events_links(self, updates) -> None:
ONLY_ONE_PRIMARY_LINKED_EVENT_ERROR = "Only 1 primary linked event is allowed"
event_link_method = app.config.get("PLANNING_EVENT_LINK_METHOD", "one_primary")
event_link_method = get_planning_event_link_method()

if updates.get("related_events"):
related_events_links = updates.get("related_events")
Expand All @@ -383,6 +385,8 @@ def _validate_events_links(self, updates):
assert len(primary_links) + len(secondary_links) == len(
related_events_links
), "Missing events link type"
else:
assert_never(event_link_method)

def _set_planning_event_info(self, doc: Planning, planning_type: ContentProfile) -> Optional[Event]:
"""Set the planning event date
Expand Down
1 change: 1 addition & 0 deletions server/planning/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

UPDATE_METHOD = Literal["single", "future", "all"]
PLANNING_RELATED_EVENT_LINK_TYPE = Literal["primary", "secondary"]
PLANNING_EVENT_LINK_METHOD = Literal["one_primary", "many_secondary", "one_primary_many_secondary"]


class StringFieldTranslation(TypedDict):
Expand Down
5 changes: 5 additions & 0 deletions server/planning/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import arrow
import pytz

from planning import types
from superdesk import get_resource_service
from superdesk.json_utils import cast_item

Expand Down Expand Up @@ -213,3 +214,7 @@ def get_first_event_item_for_planning_id(
return None

return get_resource_service("events").find_one(req=None, _id=first_event_id)


def get_planning_event_link_method() -> types.PLANNING_EVENT_LINK_METHOD:
return app.config.get("PLANNING_EVENT_LINK_METHOD", "one_primary")
5 changes: 3 additions & 2 deletions server/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

import os
import json
import planning.types as planning_types

from urllib.parse import urlparse
from typing import Literal


def env(variable, fallback_value=None):
Expand Down Expand Up @@ -191,4 +192,4 @@ def env(variable, fallback_value=None):

PLANNING_DEFAULT_COVERAGE_STATUS_ON_INGEST = "ncostat:int"

PLANNING_EVENT_LINK_METHOD: Literal["one_primary", "many_secondary", "one_primary_many_secondary"] = "one_primary"
PLANNING_EVENT_LINK_METHOD: planning_types.PLANNING_EVENT_LINK_METHOD = "one_primary"

0 comments on commit df86506

Please sign in to comment.