Skip to content

Commit

Permalink
Migrated create & on_create methods
Browse files Browse the repository at this point in the history
Along with another set of utils functions

SDESK-7442
  • Loading branch information
eos87 committed Dec 6, 2024
1 parent a7c710c commit 4035048
Show file tree
Hide file tree
Showing 15 changed files with 582 additions and 318 deletions.
2 changes: 1 addition & 1 deletion server/planning/commands/delete_spiked_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from superdesk.lock import lock, unlock, remove_locks
from planning.common import WORKFLOW_STATE
from planning.events import EventsAsyncService
from planning.events.utils import get_recurring_timeline
from planning.events.events_utils import get_recurring_timeline
from planning.planning import PlanningAsyncService
from planning.assignments import AssignmentsAsyncService
from .async_cli import planning_cli
Expand Down
51 changes: 34 additions & 17 deletions server/planning/commands/export_to_newsroom_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@
# For the full copyright and license information, please see the
# AUTHORS and LICENSE files distributed with this source code, or
# at https://www.sourcefabric.org/superdesk/license

import mock
from bson import ObjectId
from datetime import timedelta
from .export_to_newsroom import ExportToNewsroom
from superdesk import get_resource_service

from planning.types.event import EventResourceModel

from superdesk.flask import g
from superdesk.utc import utcnow
from superdesk import get_resource_service

from planning.tests import TestCase
from planning.events.events_service import EventsAsyncService

from .export_to_newsroom import ExportToNewsroom


class MockTransmitter:
Expand All @@ -27,17 +36,24 @@ def transmit(self, queue_item):


class ExportToNewsroomTest(TestCase):
def setUp(self):
super().setUp()
async def asyncSetUp(self):
await super().asyncSetUp()

self.event_service = get_resource_service("events")
self.event_service = EventsAsyncService()
self.planning_service = get_resource_service("planning")

def setUp_data(self):
def setup_user(self):
user = {"_id": ObjectId()}
self.app.data.insert("users", [user])
g.user = user

async def setup_data(self):
utc_now = utcnow()
self.setup_user()

events = [
{
"_id": "draft",
"id": "draft",
"dates": {
"start": utc_now,
"end": utc_now + timedelta(days=1),
Expand All @@ -48,7 +64,7 @@ def setUp_data(self):
"type": "event",
},
{
"_id": "scheduled",
"id": "scheduled",
"dates": {
"start": utc_now,
"end": utc_now + timedelta(days=1),
Expand All @@ -60,7 +76,7 @@ def setUp_data(self):
"type": "event",
},
{
"_id": "postponed",
"id": "postponed",
"dates": {
"start": utc_now,
"end": utc_now + timedelta(days=1),
Expand All @@ -72,7 +88,7 @@ def setUp_data(self):
"type": "event",
},
{
"_id": "rescheduled",
"id": "rescheduled",
"dates": {
"start": utc_now,
"end": utc_now + timedelta(days=1),
Expand All @@ -84,7 +100,7 @@ def setUp_data(self):
"type": "event",
},
{
"_id": "cancelled",
"id": "cancelled",
"dates": {
"start": utc_now,
"end": utc_now + timedelta(days=1),
Expand All @@ -96,7 +112,7 @@ def setUp_data(self):
"type": "event",
},
{
"_id": "killed",
"id": "killed",
"dates": {
"start": utc_now,
"end": utc_now + timedelta(days=1),
Expand All @@ -108,7 +124,7 @@ def setUp_data(self):
"type": "event",
},
{
"_id": "postponed-not-published",
"id": "postponed-not-published",
"dates": {
"start": utc_now,
"end": utc_now + timedelta(days=1),
Expand All @@ -119,7 +135,7 @@ def setUp_data(self):
"type": "event",
},
{
"_id": "rescheduled-not-published",
"id": "rescheduled-not-published",
"dates": {
"start": utc_now,
"end": utc_now + timedelta(days=1),
Expand All @@ -130,7 +146,7 @@ def setUp_data(self):
"type": "event",
},
{
"_id": "cancelled-not-published",
"id": "cancelled-not-published",
"dates": {
"start": utc_now,
"end": utc_now + timedelta(days=1),
Expand All @@ -141,6 +157,7 @@ def setUp_data(self):
"type": "event",
},
]
events = [EventResourceModel.from_dict(ev) for ev in events]

planning = [
{
Expand Down Expand Up @@ -213,13 +230,13 @@ def setUp_data(self):
},
]

self.event_service.create(events)
await self.event_service.create(events)
self.planning_service.create(planning)

@mock.patch("planning.commands.export_to_newsroom.NewsroomHTTPTransmitter")
async def test_events_events_planning(self, mock_transmitter):
async with self.app.app_context():
self.setUp_data()
await self.setup_data()

mock_transmitter.return_value = MockTransmitter()
ExportToNewsroom().run(assets_url="foo", resource_url="bar")
Expand Down
2 changes: 1 addition & 1 deletion server/planning/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
)
from planning.autosave import AutosaveService

from .service import EventsAsyncService
from .events_service import EventsAsyncService
from .module import events_resource_config

__all__ = [
Expand Down
19 changes: 1 addition & 18 deletions server/planning/events/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
from planning.types import (
Event,
EmbeddedPlanning,
EmbeddedCoverageItem,
PlanningRelatedEventLink,
PLANNING_RELATED_EVENT_LINK_TYPE,
)
Expand All @@ -74,7 +73,6 @@
set_ingest_version_datetime,
is_new_version,
update_ingest_on_patch,
TEMP_ID_PREFIX,
)
from planning.utils import (
get_planning_event_link_method,
Expand All @@ -84,6 +82,7 @@
from .events_base_service import EventsBaseService
from .events_schema import events_schema
from .events_sync import sync_event_metadata_with_planning_items
from .events_utils import get_events_embedded_planning

logger = logging.getLogger(__name__)

Expand All @@ -99,22 +98,6 @@
}


def get_events_embedded_planning(event: Event) -> List[EmbeddedPlanning]:
def get_coverage_id(coverage: EmbeddedCoverageItem) -> str:
if not coverage.get("coverage_id"):
coverage["coverage_id"] = TEMP_ID_PREFIX + "-" + generate_guid(type=GUID_NEWSML)
return coverage["coverage_id"]

return [
EmbeddedPlanning(
planning_id=planning.get("planning_id"),
update_method=planning.get("update_method") or "single",
coverages={get_coverage_id(coverage): coverage for coverage in planning.get("coverages") or []},
)
for planning in event.pop("embedded_planning", [])
]


def get_subject_str(subject: Dict[str, str]) -> str:
return ":".join(
[
Expand Down
Loading

0 comments on commit 4035048

Please sign in to comment.