<commit_before>__copyright__ = "Copyright 2017 Birkbeck, University of London"
__author__ = "<NAME> & <NAME>"
__license__ = "AGPL v3"
__maintainer__ = "Birkbeck Centre for Technology and Publishing"


import datetime
from uuid import uuid4
import requests
from bs4 import BeautifulSoup

from django.urls import reverse
from django.template.loader import render_to_string
from django.utils.http import urlencode
from django.conf import settings
from django.contrib import messages
from django.utils import timezone

import sys
from utils import models as util_models
from utils.function_cache import cache
from utils.logger import get_logger
from crossref.restful import Depositor

logger = get_logger(__name__)


def register_crossref_doi(identifier):
    from utils import setting_handler

    domain = identifier.article.journal.domain
    pingback_url = urlencode({'pingback': 'http://{0}{1}'.format(domain, reverse('crossref_pingback'))})

    use_crossref = setting_handler.get_setting('Identifiers', 'use_crossref',
                                               identifier.article.journal).processed_value

    if not use_crossref:
        logger.info("[DOI] Not using Crossref DOIs on this journal. Aborting registration.")
        return 'Crossref Disabled', 'Disabled'

    test_mode = setting_handler.get_setting(
            'Identifiers', 'crossref_test', identifier.article.journal
    ).processed_value or settings.DEBUG

    if test_mode:
        util_models.LogEntry.add_entry('Submission', "DOI registration running in test mode", 'Info',
                                       target=identifier.article)
    else:
        util_models.LogEntry.add_entry('Submission', "DOI registration running in live mode", 'Info',
                                       target=identifier.article)

    return send_crossref_deposit(test_mode, identifier)


def register_crossref_component(article, xml, supp_file):
    from utils import setting_handler

    use_crossref = setting_handler.get_setting('Identifiers', 'use_crossref',
                                               article.journal).processed_value

    if not use_crossref:
        logger.info("[DOI] Not using Crossref DOIs on this journal. Aborting registration.")
        return

    test_mode = setting_handler.get_setting(
            'Identifiers', 'crossref_test', article.journal
    ).processed_value or settings.DEBUG

    if test_mode:
        util_models.LogEntry.add_entry('Submission', "DOI component registration running in test mode", 'Info',
                                       target=article)
    else:
        util_models.LogEntry.add_entry('Submission', "DOI component registration running in live mode", 'Info',
                                       target=article)

    doi_prefix = setting_handler.get_setting('Identifiers', 'crossref_prefix', article.journal)
    username = setting_handler.get_setting('Identifiers', 'crossref_username', article.journal).processed_value
    password = setting_handler.get_setting('Identifiers', 'crossref_password', article.journal).processed_value

    depositor = Depositor(prefix=doi_prefix, api_user=username, api_key=password, use_test_server=test_mode)
    response = depositor.register_doi(submission_id='component{0}.xml'.format(uuid4()), request_xml=xml)

    logger.debug("[CROSSREF:DEPOSIT:{0}] Sending".format(article.id))
    logger.debug("[CROSSREF:DEPOSIT:%s] Response code %s" % (article.id, response.status_code))

    if response.status_code != 200:
        util_models.LogEntry.add_entry('Error',
                                       "Error depositing: {0}. {1}".format(response.status_code, response.text),
                                       'Debug',
                                       target=article)

        status = "Error depositing: {code}, {text}".format(
            code=response.status_code,
            text=response.text
        )
        logger.error(status)
        logger.error(response.text)
        error = True
    else:
        util_models.LogEntry.add_entry('Submission', "Deposited DOI.", 'Info', target=article)


def create_crossref_template(identifier):
    from utils import setting_handler
    template_context = {
        'batch_id': uuid4(),
        'timestamp': int(round((datetime.datetime.now() - datetime.datetime(1970, 1, 1)).total_seconds())),
        'depositor_name': setting_handler.get_setting('Identifiers', 'crossref_name',
                                                      identifier.article.journal).processed_value,
        'depositor_email': setting_handler.get_setting('Identifiers', 'crossref_email',
                                                       identifier.article.journal).processed_value,
        'registrant': setting_handler.get_setting('Identifiers', 'crossref_registrant',
                                                  identifier.article.journal).processed_value,
        'journal_title': identifier.article.journal.name,
        'journal_issn': identifier.article.journal.issn,
        'date_published': identifier.article.date_published,
        'issue': identifier.article.issue,
        'article_title': '{0}{1}{2}'.format(
            identifier.article.title,
            ' ' if identifier.article.subtitle is not None else '',
            identifier.article.subtitle if identifier.article.subtitle is not None else ''),
        'authors': identifier.article.authors.all(),
        'doi': identifier.identifier,
        'article_url': identifier.article.url,
        'now': timezone.now(),
    }

    # append citations for i4oc compatibility
    xml = identifier.article.get_render_galley

    if xml:
        try:
            # do a transform that mutates the references into Crossref format from the render galley
            logger.debug('Doing crossref citation list transform')
            xml_transformed = identifier.article.render_galley.render_crossref()
            logger.debug(xml_transformed)

            # extract the citation list
            souped_xml = BeautifulSoup(str(xml_transformed), 'lxml')
            citation_list = souped_xml.find('citation_list')

            if souped_xml:
                template_context['citation_list'] = str(citation_list.extract()).replace("<cyear", "<cYear").replace("</cyear", "</cYear")
        except Exception as e:
            logger.error('Error transforming Crossref citations: %s' % e)
    else:
        logger.debug('No XML galleys found for transform')<commit_msg>Only run i40c on XML render galleys<commit_after>__copyright__ = "Copyright 2017 Birkbeck, University of London"
__author__ = "<NAME> & <NAME>"
__license__ = "AGPL v3"
__maintainer__ = "Birkbeck Centre for Technology and Publishing"


import datetime
from uuid import uuid4
import requests
from bs4 import BeautifulSoup

from django.urls import reverse
from django.template.loader import render_to_string
from django.utils.http import urlencode
from django.conf import settings
from django.contrib import messages
from django.utils import timezone

import sys
from utils import models as util_models
from utils.function_cache import cache
from utils.logger import get_logger
from crossref.restful import Depositor

logger = get_logger(__name__)


def register_crossref_doi(identifier):
    from utils import setting_handler

    domain = identifier.article.journal.domain
    pingback_url = urlencode({'pingback': 'http://{0}{1}'.format(domain, reverse('crossref_pingback'))})

    use_crossref = setting_handler.get_setting('Identifiers', 'use_crossref',
                                               identifier.article.journal).processed_value

    if not use_crossref:
        logger.info("[DOI] Not using Crossref DOIs on this journal. Aborting registration.")
        return 'Crossref Disabled', 'Disabled'

    test_mode = setting_handler.get_setting(
            'Identifiers', 'crossref_test', identifier.article.journal
    ).processed_value or settings.DEBUG

    if test_mode:
        util_models.LogEntry.add_entry('Submission', "DOI registration running in test mode", 'Info',
                                       target=identifier.article)
    else:
        util_models.LogEntry.add_entry('Submission', "DOI registration running in live mode", 'Info',
                                       target=identifier.article)

    return send_crossref_deposit(test_mode, identifier)


def register_crossref_component(article, xml, supp_file):
    from utils import setting_handler

    use_crossref = setting_handler.get_setting('Identifiers', 'use_crossref',
                                               article.journal).processed_value

    if not use_crossref:
        logger.info("[DOI] Not using Crossref DOIs on this journal. Aborting registration.")
        return

    test_mode = setting_handler.get_setting(
            'Identifiers', 'crossref_test', article.journal
    ).processed_value or settings.DEBUG

    if test_mode:
        util_models.LogEntry.add_entry('Submission', "DOI component registration running in test mode", 'Info',
                                       target=article)
    else:
        util_models.LogEntry.add_entry('Submission', "DOI component registration running in live mode", 'Info',
                                       target=article)

    doi_prefix = setting_handler.get_setting('Identifiers', 'crossref_prefix', article.journal)
    username = setting_handler.get_setting('Identifiers', 'crossref_username', article.journal).processed_value
    password = setting_handler.get_setting('Identifiers', 'crossref_password', article.journal).processed_value

    depositor = Depositor(prefix=doi_prefix, api_user=username, api_key=password, use_test_server=test_mode)
    response = depositor.register_doi(submission_id='component{0}.xml'.format(uuid4()), request_xml=xml)

    logger.debug("[CROSSREF:DEPOSIT:{0}] Sending".format(article.id))
    logger.debug("[CROSSREF:DEPOSIT:%s] Response code %s" % (article.id, response.status_code))

    if response.status_code != 200:
        util_models.LogEntry.add_entry('Error',
                                       "Error depositing: {0}. {1}".format(response.status_code, response.text),
                                       'Debug',
                                       target=article)

        status = "Error depositing: {code}, {text}".format(
            code=response.status_code,
            text=response.text
        )
        logger.error(status)
        logger.error(response.text)
        error = True
    else:
        util_models.LogEntry.add_entry('Submission', "Deposited DOI.", 'Info', target=article)


def create_crossref_template(identifier):
    from utils import setting_handler
    template_context = {
        'batch_id': uuid4(),
        'timestamp': int(round((datetime.datetime.now() - datetime.datetime(1970, 1, 1)).total_seconds())),
        'depositor_name': setting_handler.get_setting('Identifiers', 'crossref_name',
                                                      identifier.article.journal).processed_value,
        'depositor_email': setting_handler.get_setting('Identifiers', 'crossref_email',
                                                       identifier.article.journal).processed_value,
        'registrant': setting_handler.get_setting('Identifiers', 'crossref_registrant',
                                                  identifier.article.journal).processed_value,
        'journal_title': identifier.article.journal.name,
        'journal_issn': identifier.article.journal.issn,
        'date_published': identifier.article.date_published,
        'issue': identifier.article.issue,
        'article_title': '{0}{1}{2}'.format(
            identifier.article.title,
            ' ' if identifier.article.subtitle is not None else '',
            identifier.article.subtitle if identifier.article.subtitle is not None else ''),
        'authors': identifier.article.authors.all(),
        'doi': identifier.identifier,
        'article_url': identifier.article.url,
        'now': timezone.now(),
    }

    # append citations for i4oc compatibility
    render_galley = identifier.article.get_render_galley

    if render_galley and render_galley.type == 'xml':
        try:
            # do a transform that mutates the references into Crossref format from the render galley
            logger.debug('Doing crossref citation list transform')
            xml_transformed = identifier.article.render_galley.render_crossref()
            logger.debug(xml_transformed)

            # extract the citation list
            souped_xml = BeautifulSoup(str(xml_transformed), 'lxml')
            citation_list = souped_xml.find('citation_list')

            if souped_xml:
                template_context['citation_list'] = str(citation_list.extract()).replace("<cyear", "<cYear").replace("</cyear", "</cYear")
        except Exception as e:
            logger.error('Error transforming Crossref citations: %s' % e)
    else:
        logger.debug('No XML galleys found for transform')
{'fraction_non_alphanumeric': 0.0972628017837921, 'fraction_numerical': 0.004459480239889282, 'mean_word_length': 2.794340723453909, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 25}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 34, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1746942', 'n_tokens_mistral': 3470, 'n_tokens_neox': 3342, 'n_words': 764}