<commit_before># -*- 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
from eve.utils import config
from superdesk.errors import SuperdeskApiError
from superdesk import get_resource_service
from apps.archive.archive import SOURCE as ARCHIVE
from apps.content import LINKED_IN_PACKAGES, PACKAGE_TYPE, TAKES_PACKAGE, ITEM_TYPE, ITEM_TYPE_COMPOSITE, PACKAGE
from apps.archive.common import ASSOCIATIONS, MAIN_GROUP, SEQUENCE, PUBLISH_STATES
from .package_service import get_item_ref, create_root_group


LAST_TAKE = 'last_take'
logger = logging.getLogger(__name__)


class TakesPackageService():

    def get_take_package_id(self, item):
        """
        Checks if the item is in a 'takes' package and returns the package id
        :return: _id of the package or None
        """
        takes_package = [package.get(PACKAGE) for package in item.get(LINKED_IN_PACKAGES, [])
                         if package.get(PACKAGE_TYPE)]
        if len(takes_package) > 1:
            message = 'Multiple takes found for item: {0}'.format(item['_id'])
            logger.error(message)
            raise SuperdeskApiError.forbiddenError(message=message)
        return takes_package[0] if takes_package else None

    def enhance_with_package_info(self, item):
        package_id = self.get_take_package_id(item)
        if package_id:
            takes_package = get_resource_service(ARCHIVE).find_one(req=None, _id=package_id)
            item.setdefault(TAKES_PACKAGE, takes_package)

    def __link_items__(self, takes_package, target, link):
        sequence = takes_package.get(SEQUENCE, 0) if takes_package else 0
        main_group = next((group for group in takes_package['groups'] if group['id'] == MAIN_GROUP))

        if sequence == 0:
            target_ref = get_item_ref(target)
            sequence = self.__next_sequence__(sequence)
            target_ref[SEQUENCE] = sequence
            main_group[ASSOCIATIONS].append(target_ref)

        link_ref = get_item_ref(link)
        link_ref[SEQUENCE] = self.__next_sequence__(sequence)
        main_group[ASSOCIATIONS].append(link_ref)
        takes_package[SEQUENCE] = link_ref[SEQUENCE]
        takes_package[LAST_TAKE] = link['_id']

    def __next_sequence__(self, seq):
        return seq + 1

    def __strip_take_info__(self, take_info):
        take_index = take_info.rfind('=')
        return take_info[0:take_index] if take_info[take_index + 1:].isdigit() else take_info

    def __copy_metadata__(self, target, to, package):
        # TODO: length validation may be required.
        # if target is the first take hence default sequence is for first take.
        sequence = package.get(SEQUENCE, 1) if package else 1
        sequence = self.__next_sequence__(sequence)
        headline = self.__strip_take_info__(target.get('headline', ''))
        take_key = self.__strip_take_info__(target.get('anpa_take_key', ''))
        to['headline'] = '{}={}'.format(headline, sequence)
        to['anpa_take_key'] = '{}={}'.format(take_key, sequence)
        if target.get(config.CONTENT_STATE) in PUBLISH_STATES:
            to['anpa_take_key'] = '{} (reopens)'.format(take_key)
        to['_version'] = 1
        to[config.CONTENT_STATE] = 'in_progress' if to.get('task', {}).get('desk', None) else 'draft'

        copy_from = package if (package.get(config.CONTENT_STATE) in PUBLISH_STATES) else target
        for field in ['abstract', 'anpa-category', 'pubstatus', 'destination_groups',
                      'slugline', 'urgency', 'subject', 'dateline']:
            to[field] = copy_from.get(field)

    def create_takes_package(self, takes_package, target, link):
        takes_package.update({
            ITEM_TYPE: ITEM_TYPE_COMPOSITE,
            PACKAGE_TYPE: TAKES_PACKAGE,
            'headline': target.get('headline'),
            'slugline': target.get('slugline'),
            'abstract': target.get('abstract')
        })
        create_root_group([takes_package])
        self.__link_items__(takes_package, target, link)
        archive_service = get_resource_service(ARCHIVE)
        tasks_service = get_resource_service('tasks')
        ids = archive_service.post([takes_package])
        takes_package_id = ids[0]

        # send the package to the desk where the first take was sent
        current_task = target.get('task')
        tasks_service.patch(takes_package_id, {'task': current_task})
<commit_msg>Removed abstract from the list fields to copy from.<commit_after># -*- 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
from eve.utils import config
from superdesk.errors import SuperdeskApiError
from superdesk import get_resource_service
from apps.archive.archive import SOURCE as ARCHIVE
from apps.content import LINKED_IN_PACKAGES, PACKAGE_TYPE, TAKES_PACKAGE, ITEM_TYPE, ITEM_TYPE_COMPOSITE, PACKAGE
from apps.archive.common import ASSOCIATIONS, MAIN_GROUP, SEQUENCE, PUBLISH_STATES
from .package_service import get_item_ref, create_root_group


LAST_TAKE = 'last_take'
logger = logging.getLogger(__name__)


class TakesPackageService():

    def get_take_package_id(self, item):
        """
        Checks if the item is in a 'takes' package and returns the package id
        :return: _id of the package or None
        """
        takes_package = [package.get(PACKAGE) for package in item.get(LINKED_IN_PACKAGES, [])
                         if package.get(PACKAGE_TYPE)]
        if len(takes_package) > 1:
            message = 'Multiple takes found for item: {0}'.format(item['_id'])
            logger.error(message)
            raise SuperdeskApiError.forbiddenError(message=message)
        return takes_package[0] if takes_package else None

    def enhance_with_package_info(self, item):
        package_id = self.get_take_package_id(item)
        if package_id:
            takes_package = get_resource_service(ARCHIVE).find_one(req=None, _id=package_id)
            item.setdefault(TAKES_PACKAGE, takes_package)

    def __link_items__(self, takes_package, target, link):
        sequence = takes_package.get(SEQUENCE, 0) if takes_package else 0
        main_group = next((group for group in takes_package['groups'] if group['id'] == MAIN_GROUP))

        if sequence == 0:
            target_ref = get_item_ref(target)
            sequence = self.__next_sequence__(sequence)
            target_ref[SEQUENCE] = sequence
            main_group[ASSOCIATIONS].append(target_ref)

        link_ref = get_item_ref(link)
        link_ref[SEQUENCE] = self.__next_sequence__(sequence)
        main_group[ASSOCIATIONS].append(link_ref)
        takes_package[SEQUENCE] = link_ref[SEQUENCE]
        takes_package[LAST_TAKE] = link['_id']

    def __next_sequence__(self, seq):
        return seq + 1

    def __strip_take_info__(self, take_info):
        take_index = take_info.rfind('=')
        return take_info[0:take_index] if take_info[take_index + 1:].isdigit() else take_info

    def __copy_metadata__(self, target, to, package):
        # TODO: length validation may be required.
        # if target is the first take hence default sequence is for first take.
        sequence = package.get(SEQUENCE, 1) if package else 1
        sequence = self.__next_sequence__(sequence)
        headline = self.__strip_take_info__(target.get('headline', ''))
        take_key = self.__strip_take_info__(target.get('anpa_take_key', ''))
        to['headline'] = '{}={}'.format(headline, sequence)
        to['anpa_take_key'] = '{}={}'.format(take_key, sequence)
        if target.get(config.CONTENT_STATE) in PUBLISH_STATES:
            to['anpa_take_key'] = '{} (reopens)'.format(take_key)
        to['_version'] = 1
        to[config.CONTENT_STATE] = 'in_progress' if to.get('task', {}).get('desk', None) else 'draft'

        copy_from = package if (package.get(config.CONTENT_STATE) in PUBLISH_STATES) else target
        for field in ['anpa-category', 'pubstatus', 'destination_groups',
                      'slugline', 'urgency', 'subject', 'dateline']:
            to[field] = copy_from.get(field)

    def create_takes_package(self, takes_package, target, link):
        takes_package.update({
            ITEM_TYPE: ITEM_TYPE_COMPOSITE,
            PACKAGE_TYPE: TAKES_PACKAGE,
            'headline': target.get('headline'),
            'slugline': target.get('slugline'),
            'abstract': target.get('abstract')
        })
        create_root_group([takes_package])
        self.__link_items__(takes_package, target, link)
        archive_service = get_resource_service(ARCHIVE)
        tasks_service = get_resource_service('tasks')
        ids = archive_service.post([takes_package])
        takes_package_id = ids[0]

        # send the package to the desk where the first take was sent
        current_task = target.get('task')
        tasks_service.patch(takes_package_id, {'task': current_task})

{'fraction_non_alphanumeric': 0.10245989304812834, 'fraction_numerical': 0.004705882352941176, 'mean_word_length': 3.2679141944317665, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4883163', 'n_tokens_mistral': 2897, 'n_tokens_neox': 2804, 'n_words': 669}