From fce8ec2d05ac5aa0f983fb6ba2b9454c5462d14d Mon Sep 17 00:00:00 2001 From: Christoph Ladurner Date: Wed, 17 May 2023 16:41:15 +0200 Subject: [PATCH] utils: implement get_embargo_range * this function returns the embargo range, start - end --- .ruff.toml | 2 +- MANIFEST.in | 1 + invenio_campusonline/api.py | 1 + invenio_campusonline/types.py | 18 +++++++++ invenio_campusonline/utils.py | 24 ++++++++++- tests/conftest.py | 9 +++++ tests/minimal_record.xml | 75 +++++++++++++++++++++++++++++++++++ tests/test_utils.py | 19 +++++++++ 8 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 tests/minimal_record.xml diff --git a/.ruff.toml b/.ruff.toml index cc72b19..d56f520 100644 --- a/.ruff.toml +++ b/.ruff.toml @@ -1,4 +1,4 @@ select = ["ALL"] -ignore = ["ANN101", "ANN102", "D203", "D211", "D212", "D213", "UP009", "S101", "S314", "INP001", "FLY002", "TD002", "TD003"] +ignore = ["ANN101", "ANN102", "D203", "D211", "D212", "D213", "UP009", "S101", "S314", "INP001", "FLY002", "TD002", "TD003", "DTZ007"] exclude = ["docs"] diff --git a/MANIFEST.in b/MANIFEST.in index 0baab13..eb76e56 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -27,4 +27,5 @@ recursive-include docs *.rst recursive-include docs *.txt recursive-include docs Makefile recursive-include tests *.py +recursive-include tests *.xml include .git-blame-ignore-revs diff --git a/invenio_campusonline/api.py b/invenio_campusonline/api.py index ab1ad78..4a2f997 100644 --- a/invenio_campusonline/api.py +++ b/invenio_campusonline/api.py @@ -54,4 +54,5 @@ def fetch_all_ids( root = fromstring(response.text) xpath = "{http://www.campusonline.at/thesisservice/basetypes}ID" ids += [(CampusOnlineID(node.text), state) for node in root.iter(xpath)] + return ids diff --git a/invenio_campusonline/types.py b/invenio_campusonline/types.py index 0f27b08..7191fac 100644 --- a/invenio_campusonline/types.py +++ b/invenio_campusonline/types.py @@ -10,6 +10,7 @@ """Types.""" from dataclasses import astuple, dataclass +from datetime import datetime from enum import Enum from xml.etree.ElementTree import Element @@ -38,6 +39,23 @@ """ +@dataclass(frozen=True) +class Embargo: + """The class is for the embargo management.""" + + start: datetime = None + end: datetime = None + + @property + def end_date(self) -> str: + """Calculate str date in format %Y-%m-%d.""" + return self.end.strftime("%Y-%m-%d") + + def __bool__(self) -> bool: + """Check if values are set, otherwise return is false.""" + return bool(self.start and self.end) + + @dataclass(frozen=True) class Color: """The class is for the output color management.""" diff --git a/invenio_campusonline/utils.py b/invenio_campusonline/utils.py index 1321066..8280193 100644 --- a/invenio_campusonline/utils.py +++ b/invenio_campusonline/utils.py @@ -7,13 +7,14 @@ # file for more details. """Command line interface to interact with the CampusOnline-Connector module.""" +from datetime import datetime from pathlib import Path from shutil import copyfileobj from xml.etree.ElementTree import Element, fromstring from requests import get, post -from .types import URL, CampusOnlineID, CampusOnlineToken, FilePath +from .types import URL, CampusOnlineID, CampusOnlineToken, Embargo, FilePath def create_request_body_metadata( @@ -101,6 +102,27 @@ def create_request_header(service: str) -> dict: } +def get_embargo_range(thesis: Element) -> Element: + """Extract the embargo range.""" + ns = "http://www.campusonline.at/thesisservice/basetypes" + xpath_start = f".//{{{ns}}}attr[@key='SPVON']" + xpath_end = f".//{{{ns}}}attr[@key='SPBIS']" + start = thesis.find(xpath_start) + end = thesis.find(xpath_end) + + if start is None or end is None: + return Embargo() + + if start.text is None or end.text is None: + return Embargo() + + in_format = "%Y-%m-%d %H:%M:%S" + start_embargo = datetime.strptime(start.text, in_format) + end_embargo = datetime.strptime(end.text, in_format) + + return Embargo(start_embargo, end_embargo) + + def get_metadata( endpoint: URL, token: CampusOnlineToken, diff --git a/tests/conftest.py b/tests/conftest.py index c01720b..3022a0e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -13,12 +13,21 @@ """ +from pathlib import Path +from xml.etree.ElementTree import Element, parse + import pytest from flask import Flask from invenio_campusonline import InvenioCampusonline +@pytest.fixture() +def minimal_record() -> Element: + """Create minimal record.""" + return parse(Path(__file__).parent / "minimal_record.xml") + + @pytest.fixture(scope="module") def create_app(instance_path: str) -> Flask: """Application factory fixture.""" diff --git a/tests/minimal_record.xml b/tests/minimal_record.xml new file mode 100644 index 0000000..a8c6028 --- /dev/null +++ b/tests/minimal_record.xml @@ -0,0 +1,75 @@ + + + + + + + + 2023-02-16 14:10:32 + 2023-04-18 12:42:28 + + + + + IFG + 2023-04-20 10:49:32 + Institute + University + DISS + Dissertation + AZ + anonym zugänglich + NG + 2023-03-03 01:15:28 + 2025-03-03 01:15:28 + + EN + DE; + J + + AUTHOR + + A + Max + Mustermann + Mustermann, Max + + + + + + + + SUPERVISOR + + 1BUTUG + John + Doe + Doe, John + + + J + + + + TEXT + + EN + J + Title Lorem Ipsum + + + + + DE + N + Titel Lorem Ipsum + + + + + + 0 + + + diff --git a/tests/test_utils.py b/tests/test_utils.py index ded8cf5..8ce6f65 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -8,11 +8,15 @@ """Module utils.""" +from xml.etree.ElementTree import Element + +from invenio_campusonline.types import Embargo from invenio_campusonline.utils import ( create_request_body_download, create_request_body_ids, create_request_body_metadata, create_request_header, + get_embargo_range, ) @@ -118,3 +122,18 @@ def test_create_request_header() -> None: } assert header == expected + + +def test_get_embargo_range(minimal_record: Element) -> None: + """Test the get_embargo_range function.""" + embargo = get_embargo_range(minimal_record) + + assert embargo.end_date == "2025-03-03" + assert bool(Embargo()) is False + + embargo = get_embargo_range(Element("root")) + + assert bool(embargo) is False + + if not bool(embargo := get_embargo_range(Element("root"))): + assert bool(embargo) is False