Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDESK-6441] Ingest rule to auto post Events or Planning #1691

Merged
merged 3 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions client/planning-extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {IPlanningConfig} from '../../interfaces';
import {getAssignmentService} from './utils';
import {AssignmentsList} from './assignments-overview';
import {IPlanningExtensionConfigurationOptions} from './extension_configuration_options';
import {AutopostIngestRuleEditor} from './ingest_rule_autopost/AutopostIngestRuleEditor';
import {AutopostIngestRulePreview} from './ingest_rule_autopost/AutopostIngestRulePreview';

function onSpike(superdesk: ISuperdesk, item: IArticle) {
const {gettext} = superdesk.localization;
Expand Down Expand Up @@ -117,6 +119,14 @@ const extension: IExtension = {
onRewriteAfter: (item: IArticle) => onArticleRewriteAfter(superdesk, item),
onSendBefore: (items: Array<IArticle>, desk: IDesk) => onSendBefore(superdesk, items, desk),
},
ingest: {
ruleHandlers: {
planning_publish: {
editor: AutopostIngestRuleEditor,
preview: AutopostIngestRulePreview,
},
},
},
},
globalMenuHorizontal: displayTopbarWidget ? [AssignmentsList] : [],
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as React from 'react';
import {IIngestRuleHandlerEditorProps} from 'superdesk-api';
import {Switch} from 'superdesk-ui-framework/react';
import {superdesk} from '../superdesk';

type IProps = IIngestRuleHandlerEditorProps<{autopost: boolean}>;

export class AutopostIngestRuleEditor extends React.PureComponent<IProps> {
constructor(props: IProps) {
super(props);

this.updateAutopostValue = this.updateAutopostValue.bind(this);
}

updateAutopostValue(value: boolean) {
this.props.updateRule({
...this.props.rule,
actions: {
...this.props.rule.actions,
extra: {
...this.props.rule.actions.extra ?? {},
autopost: value,
},
},
});
}

render() {
const {gettext} = superdesk.localization;

return (
<div>
<Switch
label={{text: gettext('Post Items')}}
value={this.props.rule.actions.extra?.autopost === true}
onChange={this.updateAutopostValue}
/>
</div>
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as React from 'react';
import {IIngestRuleHandlerPreviewProps} from 'superdesk-api';
import {superdesk} from '../superdesk';

type IProps = IIngestRuleHandlerPreviewProps<{autopost: boolean}>;

export class AutopostIngestRulePreview extends React.PureComponent<IProps> {
render() {
const {gettext} = superdesk.localization;

return (
<div className="list-row">
<span className="text-label text-label--auto">
{gettext('Post Items')}:
</span>
<span className="list-row__item">
{this.props.rule.actions.extra?.autopost === true ?
gettext('On') :
gettext('Off')
}
</span>
</div>
);
}
}
50 changes: 50 additions & 0 deletions server/features/ingest_rule_handlers.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Feature: Planning Ingest Rule Handlers

@auth
Scenario: Getting list includes Planning Autopost handler
When we get "/ingest_rule_handlers"
Then we get list with 2 items
"""
{"_items": [
{"_id": "desk_fetch_publish", "name": "Desk Fetch/Publish"},
{
"_id": "planning_publish",
"name": "Autopost Planning",
"supported_actions": {
"fetch_to_desk": false,
"publish_from_desk": false
},
"supported_configs": {
"exit": true,
"preserve_desk": false
},
"default_values": {
"name": "",
"filter": null,
"handler": "planning_publish",
"actions": {
"fetch": [],
"publish": [],
"exit": false,
"extra": {
"autopost": true
}
},
"schedule": {
"day_of_week": [
"MON",
"TUE",
"WED",
"THU",
"FRI",
"SAT",
"SUN"
],
"hour_of_day_from": null,
"hour_of_day_to": null,
"_allDay": true
}
}
}
]}
"""
1 change: 1 addition & 0 deletions server/planning/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import planning.feeding_services # noqa
import planning.feed_parsers # noqa
import planning.output_formatters # noqa
import planning.io # noqa
from planning.planning_download import init_app as init_planning_download_app

__version__ = "2.5.0-dev"
Expand Down
11 changes: 11 additions & 0 deletions server/planning/io/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# -*- coding: utf-8; -*-
#
# This file is part of Superdesk.
#
# Copyright 2022 Sourcefabric z.u. and contributors.
#
# 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

from .ingest_rule_handler import PlanningRoutingRuleHandler # noqa
74 changes: 74 additions & 0 deletions server/planning/io/ingest_rule_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# -*- coding: utf-8; -*-
#
# This file is part of Superdesk.
#
# Copyright 2022 Sourcefabric z.u. and contributors.
#
# 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 logging

from eve.utils import config
from flask_babel import lazy_gettext

from superdesk.metadata.item import ITEM_TYPE
from apps.rules.rule_handlers import RoutingRuleHandler, register_routing_rule_handler

from planning.search.eventsplanning_filters import ITEM_TYPES
from planning.common import POST_STATE, update_post_item

logger = logging.getLogger(__name__)


class PlanningRoutingRuleHandler(RoutingRuleHandler):
ID = "planning_publish"
NAME = lazy_gettext("Autopost Planning")
supported_actions = {
"fetch_to_desk": False,
"publish_from_desk": False,
}
supported_configs = {
"exit": True,
"preserve_desk": False,
}
default_values = {
"name": "",
"handler": "planning_publish",
"filter": None,
"actions": {
"fetch": [],
"publish": [],
"exit": False,
"extra": {
"autopost": True,
},
},
"schedule": {
"day_of_week": ["MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"],
"hour_of_day_from": None,
"hour_of_day_to": None,
"_allDay": True,
},
}

def can_handle(self, rule, ingest_item, routing_scheme):
return ingest_item.get(ITEM_TYPE) in [ITEM_TYPES.EVENT, ITEM_TYPES.PLANNING]

def apply_rule(self, rule, ingest_item, routing_scheme):
if rule.get("actions", {}).get("extra", {}).get("autopost"):
logger.info(ingest_item)
item_id = ingest_item.get(config.ID_FIELD)
logger.info(f"Posting item {item_id}")
update_post_item(
{
"pubstatus": ingest_item.get("pubstatus") or POST_STATE.USABLE,
"_etag": ingest_item.get("_etag"),
},
ingest_item,
)
logger.info(f"Posted item {item_id}")


register_routing_rule_handler(PlanningRoutingRuleHandler())