<commit_before># -*- coding: utf-8; -*-
#
# This file is part of Superdesk.
#
# Copyright 2013, 2016 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 superdesk

from apps.archive.archive import SOURCE as ARCHIVE
from apps.content import push_content_notification
from apps.auth import get_user_id
from superdesk import get_resource_service
from superdesk.errors import SuperdeskApiError, InvalidStateTransitionError
from superdesk.metadata.item import CONTENT_STATE, ITEM_STATE
from superdesk.metadata.packages import RESIDREF
from superdesk.resource import Resource
from superdesk.services import BaseService
from superdesk.workflow import is_workflow_state_transition_valid
from superdesk.utc import utcnow
from apps.packages import PackageService
from flask_babel import _

package_service = PackageService()


class TranslateResource(Resource):
    endpoint_name = 'translate'
    resource_title = endpoint_name

    schema = {
        'guid': {
            'type': 'string',
            'required': True
        },
        'language': {
            'type': 'string',
            'required': True
        },
        'desk': Resource.rel('desks', nullable=True),
    }

    url = 'archive/translate'

    resource_methods = ['POST']
    item_methods = []

    privileges = {'POST': 'translate'}


class TranslateService(BaseService):

    def _translate_item(self, guid, language, task=None, service=None, **kwargs):
        if not service:
            service = ARCHIVE
        archive_service = get_resource_service(service)
        macros_service = get_resource_service('macros')
        published_service = get_resource_service('published')

        item = archive_service.find_one(req=None, _id=guid)
        if not item:
            raise SuperdeskApiError.notFoundError(_('Fail to found item with guid: {guid}').format(guid=guid))

        if not is_workflow_state_transition_valid('translate', item[ITEM_STATE]):
            raise InvalidStateTransitionError()

        if item.get('language') == language:
            return guid

        if package_service.is_package(item):
            refs = package_service.get_item_refs(item)
            for ref in refs:
                ref[RESIDREF] = self._translate_item(ref[RESIDREF], language,
                                                     service=ref.get('location'),
                                                     task=task)

        if not item.get('translation_id'):
            item['translation_id'] = item['_id']

        macros_service.execute_translation_macro(
            item, item.get('language', None), language)

        item['language'] = language
        item['translated_from'] = guid
        item['versioncreated'] = utcnow()
        item['firstcreated'] = utcnow()
        if task:
            item['task'] = task

        extra_fields = ['translation_id', 'translated_from']
        _id = archive_service.duplicate_item(item, extra_fields=extra_fields, operation='translate')

        item.setdefault('translations', []).append(_id)

        updates = {
            'translation_id': item['translation_id'],
            'translations': item['translations'],
        }

        archive_service.system_update(item['_id'], updates, item)<commit_msg>Create a macro to be used in internal destinations for translations. [SDCP-231] (#1914)<commit_after># -*- coding: utf-8; -*-
#
# This file is part of Superdesk.
#
# Copyright 2013, 2016 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 superdesk

from apps.archive.archive import SOURCE as ARCHIVE
from apps.content import push_content_notification
from apps.auth import get_user_id
from superdesk import get_resource_service
from superdesk.errors import SuperdeskApiError, InvalidStateTransitionError
from superdesk.metadata.item import CONTENT_STATE, ITEM_STATE
from superdesk.metadata.packages import RESIDREF
from superdesk.resource import Resource
from superdesk.services import BaseService
from superdesk.workflow import is_workflow_state_transition_valid
from superdesk.utc import utcnow
from apps.packages import PackageService
from flask_babel import _

package_service = PackageService()


class TranslateResource(Resource):
    endpoint_name = 'translate'
    resource_title = endpoint_name

    schema = {
        'guid': {
            'type': 'string',
            'required': True
        },
        'language': {
            'type': 'string',
            'required': True
        },
        'desk': Resource.rel('desks', nullable=True),
    }

    url = 'archive/translate'

    resource_methods = ['POST']
    item_methods = []

    privileges = {'POST': 'translate'}


class TranslateService(BaseService):

    def _translate_item(self, guid, language, task=None, service=None, state=None, **kwargs):
        if not service:
            service = ARCHIVE
        archive_service = get_resource_service(service)
        macros_service = get_resource_service('macros')
        published_service = get_resource_service('published')

        item = archive_service.find_one(req=None, _id=guid)
        if not item:
            raise SuperdeskApiError.notFoundError(_('Fail to found item with guid: {guid}').format(guid=guid))

        if not is_workflow_state_transition_valid('translate', item[ITEM_STATE]):
            raise InvalidStateTransitionError()

        if item.get('language') == language:
            return guid

        if package_service.is_package(item):
            refs = package_service.get_item_refs(item)
            for ref in refs:
                ref[RESIDREF] = self._translate_item(ref[RESIDREF], language,
                                                     service=ref.get('location'),
                                                     task=task)

        if not item.get('translation_id'):
            item['translation_id'] = item['_id']

        macros_service.execute_translation_macro(
            item, item.get('language', None), language)

        item['language'] = language
        item['translated_from'] = guid
        item['versioncreated'] = utcnow()
        item['firstcreated'] = utcnow()
        if task:
            item['task'] = task

        extra_fields = ['translation_id', 'translated_from']

        _id = archive_service.duplicate_item(item, extra_fields=extra_fields, state=state, operation='translate')

        item.setdefault('translations', []).append(_id)

        updates = {
            'translation_id': item['translation_id'],
            'translations': item['translations'],
        }

        archive_service.system_update(item['_id'], updates, item)
{'fraction_non_alphanumeric': 0.09821171041246034, 'fraction_numerical': 0.0036054225555235075, 'mean_word_length': 2.85706340378198, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3502794', 'n_tokens_mistral': 1910, 'n_tokens_neox': 1824, 'n_words': 457}