Exemplo n.º 1
0
def update_aop_bundle_items(issn_id, documents_list):
    executions = []
    try:
        journal_resp = hooks.kernel_connect(f"/journals/{issn_id}", "GET")
    except requests.exceptions.HTTPError as exc:
        raise LinkDocumentToDocumentsBundleException(str(exc))
    else:
        aop_bundle_id = journal_resp.json().get("aop")
        if aop_bundle_id is not None:
            try:
                aop_bundle_resp = hooks.kernel_connect(
                    f"/bundles/{aop_bundle_id}", "GET")
            except requests.exceptions.HTTPError as exc:
                raise LinkDocumentToDocumentsBundleException(str(exc))
            else:
                aop_bundle_items = aop_bundle_resp.json()["items"]
                documents_ids = [document["id"] for document in documents_list]
                updated_aop_items = []
                for aop_item in aop_bundle_items:
                    if aop_item["id"] not in documents_ids:
                        updated_aop_items.append(aop_item)
                    else:
                        Logger.info(
                            'Movindo ex-Ahead of Print "%s" to bundle',
                            aop_item["id"],
                        )
                        executions.append({
                            "pid": aop_item["id"],
                            "bundle_id": aop_bundle_id,
                            "ex_ahead": True,
                            "removed": True,
                        })

                update_documents_in_bundle(aop_bundle_id, updated_aop_items)
    return executions
Exemplo n.º 2
0
def get_or_create_bundle(bundle_id, is_aop):
    try:
        return hooks.kernel_connect("/bundles/" + bundle_id, "GET")
    except requests.exceptions.HTTPError as exc:
        if is_aop and exc.response.status_code == http.client.NOT_FOUND:
            create_aop_bundle(bundle_id)
            try:
                return hooks.kernel_connect("/bundles/" + bundle_id, "GET")
            except requests.exceptions.HTTPError as exc:
                raise LinkDocumentToDocumentsBundleException(str(exc), response=exc.response)
        else:
            raise LinkDocumentToDocumentsBundleException(str(exc), response=exc.response)
 def test_logs_exception_if_update_aop_bundle_items_raise_exception(
         self, mk_open, mk_update_aop_bundle_items, mk_get_or_create_bundle,
         mk_get_bundle_id, mk_regdocument, MockLogger):
     mk_open.return_value.__enter__.return_value.read.return_value = (
         '{"0034-8910": "0101-0101"}')
     mk_get_bundle_id.return_value = "0034-8910-2014-v48-n2"
     error = LinkDocumentToDocumentsBundleException(
         "No AOP Bundle in Journal")
     error.response = Mock(status_code=404)
     mk_update_aop_bundle_items.side_effect = error
     link_documents_to_documentsbundle("path_to_sps_package/package.zip",
                                       self.documents, "/json/index.json")
     MockLogger.error.assert_called_with("No AOP Bundle in Journal")
Exemplo n.º 4
0
def create_aop_bundle(bundle_id):
    try:
        hooks.kernel_connect("/bundles/" + bundle_id, "PUT")
    except requests.exceptions.HTTPError as exc:
        raise LinkDocumentToDocumentsBundleException(str(exc))
    else:
        journal_aop_path = "/journals/{}/aop".format(bundle_id[:9])
        hooks.kernel_connect(journal_aop_path, "PATCH", {"aop": bundle_id})
Exemplo n.º 5
0
def register_document_to_documentsbundle(bundle_id, payload):
    """
        Relaciona documento com seu fascículo(DocumentsBundle).

        Utiliza a endpoint do Kernel /bundles/{{ DUNDLE_ID }}
    """

    try:
        response = hooks.kernel_connect("/bundles/%s/documents" % bundle_id,
                                        "PUT", payload)
        return response
    except requests.exceptions.HTTPError as exc:
        raise LinkDocumentToDocumentsBundleException(str(exc)) from None
Exemplo n.º 6
0
def update_documents_in_bundle(bundle_id, payload):
    """
        Relaciona documento com seu fascículo(DocumentsBundle).

        Utiliza a endpoint do Kernel /bundles/{{ DUNDLE_ID }}
    """

    Logger.info('Updating Bundle "%s" with Documents: %s', bundle_id, payload)
    try:
        response = hooks.kernel_connect(
            "/bundles/%s/documents" % bundle_id, "PUT", payload)
        return response
    except requests.exceptions.HTTPError as exc:
        raise LinkDocumentToDocumentsBundleException(str(exc)) from None
 def raise_exception(*args, **kwargs):
     exc = LinkDocumentToDocumentsBundleException("Bundle not found")
     exc.response = Mock(status_code=404)
     raise exc
def link_documents_to_documentsbundle(documents, issn_index_json_path):
    """
        Relaciona documentos com seu fascículos(DocumentsBundle).

        :param kwargs['documents']: Uma lista de dicionários contento os atributos necessários para a descoberta do fascículo.

            Exemplo contendo a lista de atributos(mínimo):
            [
                {
                 "scielo_id": "S0034-8910.2014048004923",
                 "issn": "0034-8910",
                 "year": "2014",
                 "volume": "48",
                 "number": "2",
                 "order": "347",
                 },
                {
                 "scielo_id": "S0034-8910.2014048004924",
                 "issn": "0034-8910",
                 "year": "2014",
                 "volume": "48",
                 "number": "2",
                 "order": "348",
                 },
                {
                 "scielo_id": "S0034-8910.20140078954641",
                 "issn": "1518-8787",
                 "year": "2014",
                 "volume": "02",
                 "number": "2",
                 "order": "978",
                 },
                {
                 "scielo_id": "S0034-8910.20140078954641",
                 "issn": "1518-8787",
                 "year": "2014",
                 "volume": "02",
                 "number": "2",
                 "order": "978",
                 "supplement": "1",
                 }
            ]
        {"id": "0034-8910-2014-v48-n2", "status":204}
        Return a list of document linkd or not, something like:
            [
             {'id': 'S0034-8910.2014048004923', 'status': 204},
             {'id': 'S0034-8910.20140078954641', 'status': 422},
             {'id': 'S0034-8910.20140078923452', 'status': 404},
            ]
    """

    Logger.info("link_documents_to_documentsbundle PUT")

    ret = []
    bundle_id = ''
    bundle_id_doc = {}

    if documents:
        Logger.info('Reading ISSN index file %s', issn_index_json_path)
        with open(issn_index_json_path) as issn_index_file:
            issn_index_json = issn_index_file.read()
            issn_index = json.loads(issn_index_json)
        for doc in documents:
            try:
                issn_id = issn_index[doc["issn"]]
            except KeyError as exc:
                Logger.info(
                    'Could not get journal ISSN ID: ISSN id "%s" not found',
                    doc["issn"])
            else:
                bundle_id = issue_id(issn_id=issn_id,
                                     year=doc.get("year"),
                                     volume=doc.get("volume", None),
                                     number=doc.get("number", None),
                                     supplement=doc.get("supplement", None))

                bundle_id_doc.setdefault(bundle_id, [])

                payload_doc = {}
                payload_doc['id'] = doc.get("scielo_id")
                payload_doc['order'] = doc.get("order")

                bundle_id_doc[bundle_id].append(payload_doc)

        def _update_items_list(new_items: list, current_items: list) -> list:
            """Retorna uma lista links atualizada a partir dos items atuais
            e dos novos items."""

            items = deepcopy(current_items)

            for new_item in new_items:
                for index, current_item in enumerate(items):
                    if new_item["id"] == current_item["id"]:
                        items[index] = new_item
                        break
                else:
                    items.append(new_item)

            return items

        for bundle_id, new_items in bundle_id_doc.items():
            try:
                conn_response = kernel_connect("/bundles/" + bundle_id, "GET")
                current_items = conn_response.json()["items"]
                payload = _update_items_list(new_items, current_items)

                if DeepDiff(current_items, payload, ignore_order=True):
                    response = register_document_to_documentsbundle(
                        bundle_id, payload)
                    ret.append({
                        "id": bundle_id,
                        "status": response.status_code
                    })
                    logging.info("The bundle %s items list has been updated." %
                                 bundle_id)
                else:
                    logging.info(
                        "The bundle %s items does not need to be updated." %
                        bundle_id)
            except requests.exceptions.HTTPError as exc:
                raise LinkDocumentToDocumentsBundleException(
                    str(exc)) from None
        return ret

    Logger.info("link_documents_to_documentsbundle OUT")