Ejemplo n.º 1
0
def issue_as_kernel(issue: dict) -> dict:
    def parse_date(date: str) -> str:
        """Traduz datas em formato simples ano-mes-dia, ano-mes para
        o formato iso utilizado durantr a persistência do Kernel"""

        _date = None
        try:
            _date = datetime.strptime(date, "%Y-%m-%d")
        except ValueError:
            try:
                _date = datetime.strptime(date, "%Y-%m")
            except ValueError:
                _date = datetime.strptime(date, "%Y")

        return _date

    def generate_scielo_issue_pid(issn_id, v36_field):
        year = v36_field[0:4]
        order = v36_field[4:].zfill(4)
        return issn_id + year + order

    _payload = {}
    _payload["volume"] = issue.volume or ""
    _payload["number"] = issue.number or ""

    suppl = issue.supplement_volume or issue.supplement_number
    if suppl or issue.type is "supplement":
        _payload["supplement"] = suppl or "0"

    if issue.titles:
        _titles = [
            {"language": lang, "value": value} for lang, value in issue.titles.items()
        ]
        _payload["titles"] = _titles
    else:
        _payload["titles"] = []

    _payload["publication_months"] = {}
    if issue.start_month and issue.end_month:
        _payload["publication_months"]["range"] = [
            int(issue.start_month), int(issue.end_month)]
    elif issue.start_month:
        _payload["publication_months"]["month"] = int(issue.start_month)

    issn_id = issue.data.get("issue").get("v35")[0]["_"]
    _creation_date = parse_date(issue.publication_date)

    _payload["_id"] = get_bundle_id(
        issn_id,
        str(_creation_date.year),
        issue.volume,
        issue.number,
        _payload.get("supplement"),
    )
    _payload["publication_year"] = str(_creation_date.year)
    _payload["pid"] = generate_scielo_issue_pid(
        issn_id, issue.data.get("issue").get("v36")[0]["_"]
    )

    return _payload
Ejemplo n.º 2
0
 def test_returns_issue_bundle_id_with_year_volume_and_supplement_number(
         self):
     self.assertEqual(
         get_bundle_id("0101-0202",
                       "2019",
                       "53",
                       number="3",
                       supplement="1"), "0101-0202-2019-v53-n3-s1")
Ejemplo n.º 3
0
def _get_bundle_id(kernel_front_data):
    """
    Retorna o bundle_id do fascículo a partir dos dados
    provenientes do kernel front
    """
    article_meta = _nestget(kernel_front_data, "article_meta", 0)

    issue = _nestget(article_meta, "pub_issue", 0)
    number, supplement = extract_number_and_supplment_from_issue_element(issue)
    volume = _nestget(article_meta, "pub_volume", 0)
    scielo_pid_v2 = _nestget(article_meta, "scielo_pid_v2", 0)
    issn_id = scielo_pid_v2[1:10]
    year = _get_bundle_pub_year(_nestget(kernel_front_data, "pub_date"))
    return get_bundle_id(issn_id, year, volume or None, number or None,
                         supplement or None)
def link_documents_to_documentsbundle(sps_package, documents, issn_index_json_path):
    """
        Relaciona documentos com seu fascículos(DocumentsBundle).

        :param kwargs['sps_package']: Path do pacote SPS com os documentos
        :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 = []
    issn_id = ''
    bundle_id = ''
    bundle_id_doc = {}
    executions = []

    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"]
                )
                executions.append(
                    {
                        "pid": doc.get("scielo_id"),
                        "bundle_id": None,
                        "error": 'Could not get journal ISSN ID: ISSN id "%s" not found'
                        % doc["issn"],
                    }
                )
            else:
                bundle_id = get_bundle_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

        is_aop_bundle = "ahead" in sps_package
        for bundle_id, new_items in bundle_id_doc.items():
            try:
                conn_response = get_or_create_bundle(bundle_id, is_aop=is_aop_bundle)
            except LinkDocumentToDocumentsBundleException as exc:
                ret.append({"id": bundle_id, "status": exc.response.status_code})
                Logger.info("Could not get bundle %: Bundle not found", bundle_id)
                for new_item_relationship in new_items:
                    executions.append(
                        {
                            "pid": new_item_relationship.get("id"),
                            "bundle_id": bundle_id,
                            "failed": True,
                            "error": str(exc)
                        }
                    )
            else:
                current_items = conn_response.json()["items"]
                payload = _update_items_list(new_items, current_items)
                Logger.info("Registering bundle_id %s with %s", bundle_id, payload)

                if DeepDiff(current_items, payload, ignore_order=True):
                    response = update_documents_in_bundle(bundle_id, payload)
                    ret.append({"id": bundle_id, "status": response.status_code})
                    logging.info(
                        "The bundle %s items list has been updated." % bundle_id
                    )

                    for new_item_relationship in new_items:
                        executions.append(
                            {
                                "pid": new_item_relationship.get("id"),
                                "bundle_id": bundle_id,
                            }
                        )
                else:
                    logging.info(
                        "The bundle %s items does not need to be updated." % bundle_id
                    )
                if not is_aop_bundle:
                    try:
                        articles_removed_from_aop = update_aop_bundle_items(
                            issn_id, payload
                        )
                    except LinkDocumentToDocumentsBundleException as exc:
                        Logger.error(str(exc))
                    else:
                        executions.extend(articles_removed_from_aop)

        return (ret, executions)

    Logger.info("link_documents_to_documentsbundle OUT")
Ejemplo n.º 5
0
 def test_returns_issue_bundle_id_with_year_volume_and_special_number(self):
     self.assertEqual(
         get_bundle_id("0101-0202", "2019", "53", number="spe3"),
         "0101-0202-2019-v53-nspe3")
Ejemplo n.º 6
0
 def test_returns_issue_bundle_id_with_year_and_volume(self):
     self.assertEqual(get_bundle_id("0101-0202", "2019", "53"),
                      "0101-0202-2019-v53")
Ejemplo n.º 7
0
 def test_returns_aop_bundle_id_if_no_volume_number_and_supplement(self):
     self.assertEqual(get_bundle_id("0101-0202", "2019"), "0101-0202-aop")