-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move NTB related code from planning component (#344)
* Add NTBEventXMLFeedParser with test cases. * Add NTB related feeding service test cases. * NTB Event formatter. * Behave tests for NTB events. * Formatter and file feed service test cases. * Update NTBEventFormatter with latest changes. * Flake8. * Separate parser implementation from feeding service. * SDNTB-604 Typo in test feature name.
- Loading branch information
Showing
29 changed files
with
1,361 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
server/features/ingest_events.feature → ...er/features/ingest_ntb_events_api.feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
Feature: Events Ingest | ||
@auth @events_ingest | ||
Scenario: Ingest NTB Event file | ||
Given empty "events" | ||
When we get "/events" | ||
Then we get list with 0 items | ||
When we fetch events from "ntb-events-file" ingest "ntb_event_xml" | ||
When we get "/events" | ||
Then we get list with 1 items | ||
""" | ||
{ | ||
"_items": [{ | ||
"_id": "NTB-123456", | ||
"original_creator": "__no_value__" | ||
}] | ||
} | ||
""" | ||
|
||
@auth @events_ingest | ||
Scenario: Duplicate Ingested NTB Event file | ||
Given empty "events" | ||
When we get "/events" | ||
Then we get list with 0 items | ||
When we fetch events from "ntb-events-file" ingest "ntb_event_xml" | ||
When we get "/events" | ||
Then we get list with 1 items | ||
""" | ||
{ | ||
"_items": [{ | ||
"_id": "NTB-123456", | ||
"original_creator": "__no_value__" | ||
}] | ||
} | ||
""" | ||
When we duplicate event "NTB-123456" | ||
Then we get OK response | ||
When we get "/events" | ||
Then we get list with 2 items | ||
""" | ||
{ | ||
"_items": [ | ||
{ | ||
"_id": "NTB-123456", | ||
"original_creator": "__no_value__", | ||
"name": "Original Content", | ||
"definition_short": "Original Content", | ||
"state": "ingested" | ||
}, | ||
{ | ||
"_id": "#DUPLICATE_EVENT_ID#", | ||
"original_creator": "#CONTEXT_USER_ID#", | ||
"name": "duplicate", | ||
"definition_short": "duplicate", | ||
"duplicate_from": "NTB-123456", | ||
"state": "draft" | ||
} | ||
] | ||
} | ||
""" | ||
|
||
@auth @events_ingest | ||
Scenario: We reschedule ingested Ingest NTB Event | ||
Given empty "events" | ||
When we get "/events" | ||
Then we get list with 0 items | ||
When we fetch events from "ntb-events-file" ingest "ntb_event_xml" | ||
When we get "/events" | ||
Then we get list with 1 items | ||
""" | ||
{ | ||
"_items": [{ | ||
"_id": "NTB-123456", | ||
"original_creator": "__no_value__", | ||
"state": "ingested" | ||
}] | ||
} | ||
""" | ||
When we post to "/events/NTB-123456/lock" | ||
""" | ||
{"lock_action": "reschedule"} | ||
""" | ||
Then we get OK response | ||
When we perform reschedule on events "NTB-123456" | ||
""" | ||
{ | ||
"reason": "Changed to the next day!", | ||
"dates": { | ||
"start": "2029-11-22T12:00:00.000Z", | ||
"end": "2029-11-22T14:00:00.000Z", | ||
"tz": "Australia/Sydney" | ||
} | ||
} | ||
""" | ||
Then we get OK response | ||
When we get "/events" | ||
Then we get list with 1 items | ||
""" | ||
{ | ||
"_items": [{ | ||
"_id": "NTB-123456", | ||
"original_creator": "__no_value__", | ||
"state": "draft" | ||
}] | ||
} | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# -*- coding: utf-8; -*- | ||
# | ||
# This file is part of Superdesk. | ||
# | ||
# Copyright 2013, 2014 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 | ||
import xml.etree.ElementTree as ET | ||
|
||
from superdesk.errors import ParserError | ||
from superdesk.io.feed_parsers import XMLFeedParser | ||
from superdesk.io.registry import register_feed_parser | ||
from superdesk.metadata.item import ITEM_TYPE, CONTENT_TYPE, GUID_FIELD, GUID_NEWSML, FORMAT, FORMATS | ||
from superdesk.metadata.utils import generate_guid | ||
from superdesk.utc import utcnow | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class NTBEventXMLFeedParser(XMLFeedParser): | ||
"""NTB Event XML parser. | ||
Feed Parser which can parse an NTB created XML file exported from Outlook | ||
the firstcreated and versioncreated times are localised. | ||
""" | ||
|
||
NAME = 'ntb_event_xml' | ||
|
||
label = 'NTB Event XML' | ||
|
||
def can_parse(self, xml): | ||
return xml.tag.endswith('document') | ||
|
||
def parse_email(self, content, content_type, provider): | ||
if content_type != 'text/xml': | ||
raise ParserError.parseMessageError('Not supported content type.') | ||
|
||
content.seek(0) | ||
xml = ET.parse(content) | ||
return self.parse(xml.getroot(), provider) | ||
|
||
def parse_file(self, fstream, provider): | ||
xml = ET.parse(fstream) | ||
return self.parse(xml.getroot(), provider) | ||
|
||
def parse_http(self, content, provider): | ||
xml = ET.fromstring(content) | ||
return self.parse(xml, provider) | ||
|
||
def parse(self, xml, provider=None, content=None): | ||
items = [] | ||
try: | ||
# parse xml file, only expecting one event per file | ||
if not ET.iselement(xml.find('guid')): | ||
guid = generate_guid(type=GUID_NEWSML) | ||
else: | ||
guid = xml.find('guid').text | ||
|
||
item = { | ||
ITEM_TYPE: CONTENT_TYPE.EVENT, | ||
GUID_FIELD: guid, | ||
FORMAT: FORMATS.PRESERVED | ||
} | ||
item['name'] = xml.find('title').text | ||
item['definition_short'] = xml.find('title').text | ||
item['definition_long'] = xml.find('content').text | ||
item['dates'] = { | ||
'start': xml.find('timeStart').text, | ||
'end': xml.find('timeEnd').text, | ||
'tz': '' | ||
} | ||
# add location | ||
item['location'] = [{ | ||
'name': xml.find('location').text, | ||
'qcode': '', | ||
'geo': '' | ||
}] | ||
if ET.iselement(xml.find('geo')): | ||
geo = xml.find('geo') | ||
item['location'][0]['geo'] = '%s, %s' % (geo.find('latitude').text, geo.find('longitude').text) | ||
# IMPORTANT: firstcreated must be less than 2 days past | ||
# we must preserve the original event created and updated in some other fields | ||
item['firstcreated'] = utcnow() | ||
item['versioncreated'] = utcnow() | ||
items.append(item) | ||
|
||
return items | ||
except Exception as ex: | ||
raise ParserError.parseMessageError(ex, provider) | ||
|
||
|
||
register_feed_parser(NTBEventXMLFeedParser.NAME, NTBEventXMLFeedParser()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.