-
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.
Merge pull request #247 from mdhaman/1.6
Update Cores
- Loading branch information
Showing
4 changed files
with
264 additions
and
1 deletion.
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
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
167 changes: 167 additions & 0 deletions
167
server/aap/commands/export_legal_archive_to_archived.py
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,167 @@ | ||
# -*- coding: utf-8; -*- | ||
# | ||
# This file is part of Superdesk. | ||
# | ||
# Copyright 2013, 2014, 2015, 2016, 2017 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 superdesk | ||
import json | ||
|
||
from superdesk.celery_task_utils import get_lock_id | ||
from superdesk.lock import lock, unlock | ||
from superdesk import get_resource_service | ||
from eve.utils import ParsedRequest | ||
from apps.legal_archive import LEGAL_ARCHIVE_NAME | ||
|
||
from copy import deepcopy | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class ExportLegalArchiveToArchived(superdesk.Command): | ||
"""Export legal archive content to archived. | ||
Only the content for the desk provided is exported. | ||
By default this is the 'COMMISSION' desk | ||
""" | ||
|
||
default_desk = 'COMMISSION' | ||
|
||
option_list = [ | ||
superdesk.Option('--desk', '-d', dest='desk', required=False) | ||
] | ||
|
||
def run(self, desk=None): | ||
if desk: | ||
self.default_desk = desk | ||
|
||
logger.info('Starting to export {} desk legal archive content to archived'.format(self.default_desk)) | ||
|
||
lock_name = get_lock_id('legal_archive', 'export_to_archived') | ||
if not lock(lock_name, expire=610): | ||
logger.info('Export legal archive to archived task is already running.') | ||
return | ||
|
||
try: | ||
list_ids = self._export_to_archived() | ||
finally: | ||
unlock(lock_name) | ||
|
||
if list_ids: | ||
logger.info('Completed exporting {} {} desk documents from legal archive to text archived'.format( | ||
len(list_ids), | ||
self.default_desk) | ||
) | ||
else: | ||
logger.info('Completed but nothing was exported...') | ||
|
||
def _get_desk_id(self): | ||
"""Returns the ObjectID of the desk | ||
:return str: The ObjectID for the desk provided, or None if the desk was not found | ||
""" | ||
logger.info('Fetching the ObjectID for the desk {}.'.format(self.default_desk)) | ||
query = {'name': self.default_desk} | ||
req = ParsedRequest() | ||
req.where = json.dumps(query) | ||
|
||
desk_service = get_resource_service('desks') | ||
desk_item = list(desk_service.get_from_mongo(req=req, lookup=None)) | ||
if not desk_item: | ||
logger.error('Failed to find the ObjectID for the provided desk {}'.format(self.default_desk)) | ||
return None | ||
|
||
desk_id = desk_item[0]['_id'] | ||
logger.info('ObjectID for the desk {} is {}.'.format(self.default_desk, desk_id)) | ||
return desk_id | ||
|
||
def _export_to_archived(self): | ||
"""Export legal archive content to archived | ||
Copy all legal archive content that belongs to the desk provided | ||
into archived | ||
:return list: list of ids imported into archived, else if an error occurred reply with an empty list | ||
""" | ||
logger.info('Exporting legal archive content to archived.') | ||
items = list() | ||
try: | ||
desk_id = self._get_desk_id() | ||
if not desk_id: | ||
return [] | ||
|
||
for item in self._get_items(): | ||
items.append(self._generate_archived_item(item, desk_id)) | ||
|
||
return self._add_to_archived(items) | ||
except Exception as e: | ||
logging.exception('Failed to export legal archive content to archived. {}'.format(e)) | ||
|
||
return [] | ||
|
||
def _get_items(self): | ||
"""Get items from the LegalArchive that belong to the COMMISSION desk | ||
:return: list: list of legal archive content | ||
""" | ||
logger.info('Fetching legal archive content for the {} desk'.format(self.default_desk)) | ||
|
||
query = {'task.desk': self.default_desk, 'type': 'text'} | ||
req = ParsedRequest() | ||
req.where = json.dumps(query) | ||
|
||
legal_archive_service = get_resource_service(LEGAL_ARCHIVE_NAME) | ||
|
||
legal_items = list(legal_archive_service.get_from_mongo(req=req, lookup=None)) | ||
|
||
if legal_items: | ||
logger.info( | ||
'Found {} items in the legal archive for the {} desk'.format( | ||
len(legal_items), | ||
self.default_desk) | ||
) | ||
else: | ||
logger.warning('Failed to find any {} desk items in the legal archive'.format(self.default_desk)) | ||
legal_items = [] | ||
|
||
return legal_items | ||
|
||
def _generate_archived_item(self, legal_item, desk_id): | ||
"""Convert the legal archive content for the archived content | ||
:param dict legal_item: legal archive item | ||
:param str desk_id: The ObjectID for the desk provided | ||
:return dict: archived item converted from legal archive item | ||
""" | ||
archived_item = deepcopy(legal_item) | ||
|
||
archived_item.pop('task') | ||
archived_item['task'] = {'desk': desk_id} | ||
|
||
archived_item['item_id'] = archived_item['_id'] | ||
archived_item.pop('_id') | ||
|
||
archived_item.pop('linked_in_packages', None) | ||
|
||
archived_item['last_published_version'] = True | ||
archived_item['moved_to_legal'] = True | ||
archived_item['is_take_item'] = False | ||
|
||
return archived_item | ||
|
||
def _add_to_archived(self, items): | ||
"""Post the supplied list of items to the archived | ||
:param list items: list of archived items to add | ||
:return list: list of ids added to the archived | ||
""" | ||
archived_service = get_resource_service('archived') | ||
return archived_service.post(items) | ||
|
||
|
||
superdesk.command('legal_archive:export_to_archived', ExportLegalArchiveToArchived()) |
95 changes: 95 additions & 0 deletions
95
server/aap/commands/export_legal_archive_to_archived_test.py
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,95 @@ | ||
# -*- coding: utf-8; -*- | ||
# | ||
# This file is part of Superdesk. | ||
# | ||
# Copyright 2013, 2014, 2015, 2016, 2017 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 bson.objectid import ObjectId | ||
from superdesk.tests import TestCase | ||
from .export_legal_archive_to_archived import ExportLegalArchiveToArchived | ||
from superdesk import get_resource_service | ||
|
||
|
||
class ExportLegalArchiveToArchivedTest(TestCase): | ||
def setUp(self): | ||
self.app.data.insert('desks', [{ | ||
'_id': ObjectId('123456789abcdef123456789'), | ||
'name': 'COMMISSION' | ||
}]) | ||
|
||
self.app.data.insert('legal_archive', [ | ||
{ | ||
'_id': 'urn:newsml:localhost:2016-07-19T10:55:52.804692:7504fd81-b423-4a8d-a98e-3cae0d18726a', | ||
'task': {'desk': 'COMMISSION'}, | ||
'type': 'text' | ||
}, | ||
{ | ||
'_id': 'urn:newsml:localhost:2016-07-19T10:55:52.804692:7504fd81-b423-4a8d-a98e-3cae0d18726b', | ||
'task': {'desk': 'COMMISSION'}, | ||
'type': 'text' | ||
}, | ||
{ | ||
'_id': 'urn:newsml:localhost:2016-07-19T10:55:52.804692:7504fd81-b423-4a8d-a98e-3cae0d18726c', | ||
'task': {'desk': 'COMMISSION'}, | ||
'type': 'composite' | ||
}, | ||
{ | ||
'_id': 'urn:newsml:localhost:2016-07-19T10:55:52.804692:7504fd81-b423-4a8d-a98e-3cae0d18726d', | ||
'task': {'desk': 'Sports'}, | ||
'type': 'text' | ||
} | ||
]) | ||
|
||
self.script = ExportLegalArchiveToArchived() | ||
|
||
def test_get_desk_id(self): | ||
desk_id = self.script._get_desk_id() | ||
self.assertEqual(desk_id, ObjectId('123456789abcdef123456789')) | ||
|
||
def test_get_desk_id_fail(self): | ||
self.script.default_desk = 'DoesntExist' | ||
desk_id = self.script._get_desk_id() | ||
self.assertIsNone(desk_id) | ||
|
||
def test_get_items(self): | ||
legal_archive_items = self.script._get_items() | ||
|
||
self.assertEqual(len(legal_archive_items), 2) | ||
self.assertEqual( | ||
legal_archive_items[0]['_id'], | ||
'urn:newsml:localhost:2016-07-19T10:55:52.804692:7504fd81-b423-4a8d-a98e-3cae0d18726a' | ||
) | ||
self.assertEqual(legal_archive_items[0]['type'], 'text') | ||
|
||
def test_generate_archived_item(self): | ||
desk_id = self.script._get_desk_id() | ||
legal_archive_items = self.script._get_items() | ||
archived_item = self.script._generate_archived_item(legal_archive_items[0], desk_id) | ||
|
||
self.assertEqual(archived_item['task']['desk'], desk_id) | ||
self.assertEqual(archived_item['item_id'], legal_archive_items[0]['_id']) | ||
|
||
def test_add_to_archived(self): | ||
desk_id = self.script._get_desk_id() | ||
legal_archive_items = self.script._get_items() | ||
|
||
items = list() | ||
for item in legal_archive_items: | ||
items.append(self.script._generate_archived_item(item, desk_id)) | ||
|
||
archived_items = self.script._add_to_archived(items) | ||
|
||
self.assertEqual(len(archived_items), 2) | ||
|
||
def test_run(self): | ||
self.script.run() | ||
archived_service = get_resource_service('archived') | ||
archived_items = list(archived_service.get(req=None, lookup=None)) | ||
|
||
self.assertEqual(len(archived_items), 2) | ||
self.assertEqual(archived_items[0]['task']['desk'], '123456789abcdef123456789') | ||
self.assertEqual(archived_items[1]['task']['desk'], '123456789abcdef123456789') |