<commit_before>import datetime
import urlparse

import requests

from framework.celery_tasks import app as celery_app

from website import settings


@celery_app.task(ignore_results=True)
def on_preprint_updated(preprint_id):
    # WARNING: Only perform Read-Only operations in an asynchronous task, until Repeatable Read/Serializable
    # transactions are implemented in View and Task application layers.
    from website.models import PreprintService
    preprint = PreprintService.load(preprint_id)

    if settings.SHARE_URL and settings.SHARE_API_TOKEN:
        requests.post('{}api/normalizeddata/'.format(settings.SHARE_URL), json={
            'created_at': datetime.datetime.utcnow().isoformat(),
            'normalized_data': {
                '@graph': format_preprint(preprint)
            },
        }, headers={'Authorization': 'Bearer {}'.format(settings.SHARE_API_TOKEN)}).raise_for_status()

def format_institution(institution):
    return [{
        '@id': '_:{}'.format(institution._id),
        '@type': 'institution',
        'name': institution.title,
    }]


def format_user(user):
    return sum([[{
        '@id': '_:{}'.format(user._id),
        '@type': 'person',
        'suffix': user.suffix,
        'given_name': user.given_name,
        'family_name': user.family_name,
        'additional_name': user.middle_names,
    }, {
        '@id': '_:throughidentifier-{}'.format(user._id),
        '@type': 'throughidentifiers',
        'person': {
            '@id': '_:{}'.format(user._id),
            '@type': 'person',
        },
        'identifier': {
            '@id': '_:identifier-{}'.format(user._id),
            '@type': 'identifier',
        }
    }, {
        '@id': '_:identifier-{}'.format(user._id),
        '@type': 'identifier',
        'url': urlparse.urljoin(settings.DOMAIN, user.profile_url),
        'base_url': settings.DOMAIN
    }]] + [[{
        '@id': '_:{}-{}'.format(user._id, institution._id),
        '@type': 'affiliation',
        'entity': {
            '@id': '_:{}'.format(institution._id),
            '@type': 'institution',
        },
        'person': {
            '@id': '_:{}'.format(user._id),
            '@type': 'person',
        }
    }] + format_institution(institution) for institution in user.affiliated_institutions], [])

def format_contributor(preprint, user):
    return [{
        '@id': '_:{}-{}'.format(preprint._id, user._id),
        '@type': 'contributor',
        'person': {'@id': '_:{}'.format(user._id), '@type': 'person'},
        'creative_work': {'@id': '_:{}'.format(preprint._id), '@type': 'preprint'},
    }] + format_user(user)

def format_subjects(preprint):
    flat_subjs = []
    summed_subjs = sum(sum([[[{
        '@id': '_:{}'.format(subject['id']),
        '@type': 'subject',
        'name': subject['text']
    }, {
        '@id': '_:throughsubject-{}'.format(subject['id']),
        '@type': 'throughsubjects',
        'subject': {
            '@id': '_:{}'.format(subject['id']),
            '@type': 'subject',
        },
        'creative_work': {
            '@id': '_:{}'.format(preprint._id),
            '@type': 'preprint'
        }
    }] for subject in subject_hier] for subject_hier in preprint.get_subjects()], []), [])
    for s in summed_subjs:
        if s not in flat_subjs:
            flat_subjs.append(s)
    return flat_subjs

def format_preprint(preprint):
    return sum([[{
        '@id': '_:{}'.format(preprint._id),
        '@type': 'preprint',
        'title': preprint.node.title,
        'description': preprint.node.description,
        'is_deleted': not preprint.is_published or not preprint.node.is_public or preprint.node.is_preprint_orphan
    }, {
        '@id': '_:link-{}'.format(preprint._id),
        '@type': 'link',
        'url': urlparse.urljoin(settings.DOMAIN, preprint.url),
        'type': 'provider'
    }]] + [
        format_contributor(preprint, user) for user in preprint.node.contributors
    ] + [
        format_subjects(preprint)
    ] + [[{
        '@id': '_:{}-{}'.format(preprint._id, institution._id),
        '@type': 'association',
        'institution': {
            '@id': '_:{}'.format(institution._id),
            '@type': 'institution',
        },
        'creative_work': {
            '@id': '_:{}'.format(preprint._id),
            '@type': 'preprint',
        }
    }] + format_institution(institution) for institution in preprint.node.affiliated_institutions], [])<commit_msg>Update on_preprint_updated task     -Fix key     -Add debug logging     -Default to '' over None<commit_after>import datetime
import logging
import urlparse

import requests

from framework.celery_tasks import app as celery_app

from website import settings

logger = logging.getLogger(__name__)

@celery_app.task(ignore_results=True)
def on_preprint_updated(preprint_id):
    # WARNING: Only perform Read-Only operations in an asynchronous task, until Repeatable Read/Serializable
    # transactions are implemented in View and Task application layers.
    from website.models import PreprintService
    preprint = PreprintService.load(preprint_id)

    if settings.SHARE_URL and settings.SHARE_API_TOKEN:
        resp = requests.post('{}api/normalizeddata/'.format(settings.SHARE_URL), json={
            'created_at': datetime.datetime.utcnow().isoformat(),
            'normalized_data': {
                '@graph': format_preprint(preprint)
            },
        }, headers={'Authorization': 'Bearer {}'.format(settings.SHARE_API_TOKEN)})
        logger.debug(resp.content)
        resp.raise_for_status()

def format_institution(institution):
    return [{
        '@id': '_:{}'.format(institution._id),
        '@type': 'institution',
        'name': institution.title,
    }]


def format_user(user):
    return sum([[{
        '@id': '_:{}'.format(user._id),
        '@type': 'person',
        'suffix': user.suffix,
        'given_name': user.given_name,
        'family_name': user.family_name,
        'additional_name': user.middle_names,
    }, {
        '@id': '_:throughidentifier-{}'.format(user._id),
        '@type': 'throughidentifiers',
        'person': {
            '@id': '_:{}'.format(user._id),
            '@type': 'person',
        },
        'identifier': {
            '@id': '_:identifier-{}'.format(user._id),
            '@type': 'identifier',
        }
    }, {
        '@id': '_:identifier-{}'.format(user._id),
        '@type': 'identifier',
        'url': urlparse.urljoin(settings.DOMAIN, user.profile_url),
        'base_url': settings.DOMAIN
    }]] + [[{
        '@id': '_:{}-{}'.format(user._id, institution._id),
        '@type': 'affiliation',
        'entity': {
            '@id': '_:{}'.format(institution._id),
            '@type': 'institution',
        },
        'person': {
            '@id': '_:{}'.format(user._id),
            '@type': 'person',
        }
    }] + format_institution(institution) for institution in user.affiliated_institutions], [])

def format_contributor(preprint, user):
    return [{
        '@id': '_:{}-{}'.format(preprint._id, user._id),
        '@type': 'contributor',
        'person': {'@id': '_:{}'.format(user._id), '@type': 'person'},
        'creative_work': {'@id': '_:{}'.format(preprint._id), '@type': 'preprint'},
    }] + format_user(user)

def format_subjects(preprint):
    flat_subjs = []
    summed_subjs = sum(sum([[[{
        '@id': '_:{}'.format(subject['id']),
        '@type': 'subject',
        'name': subject['text']
    }, {
        '@id': '_:throughsubject-{}'.format(subject['id']),
        '@type': 'throughsubjects',
        'subject': {
            '@id': '_:{}'.format(subject['id']),
            '@type': 'subject',
        },
        'creative_work': {
            '@id': '_:{}'.format(preprint._id),
            '@type': 'preprint'
        }
    }] for subject in subject_hier] for subject_hier in preprint.get_subjects()], []), [])
    for s in summed_subjs:
        if s not in flat_subjs:
            flat_subjs.append(s)
    return flat_subjs

def format_preprint(preprint):
    return sum([[{
        '@id': '_:{}'.format(preprint._id),
        '@type': 'preprint',
        'title': preprint.node.title,
        'description': preprint.node.description or '',
        'is_deleted': not preprint.is_published or not preprint.node.is_public or preprint.node.is_preprint_orphan
    }, {
        '@id': '_:link-{}'.format(preprint._id),
        '@type': 'link',
        'url': urlparse.urljoin(settings.DOMAIN, preprint.url),
        'type': 'provider'
    }]] + [
        format_contributor(preprint, user) for user in preprint.node.contributors
    ] + [
        format_subjects(preprint)
    ] + [[{
        '@id': '_:{}-{}'.format(preprint._id, institution._id),
        '@type': 'association',
        'entity': {
            '@id': '_:{}'.format(institution._id),
            '@type': 'institution',
        },
        'creative_work': {
            '@id': '_:{}'.format(preprint._id),
            '@type': 'preprint',
        }
    }] + format_institution(institution) for institution in preprint.node.affiliated_institutions], [])
{'fraction_non_alphanumeric': 0.18072157034762584, 'fraction_numerical': 0.0, 'mean_word_length': 2.590551181102362, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3920586', 'n_tokens_mistral': 2849, 'n_tokens_neox': 2884, 'n_words': 517}