Skip to content

Commit

Permalink
utils: implement get_embargo_range
Browse files Browse the repository at this point in the history
* this function returns the embargo range, start - end
  • Loading branch information
utnapischtim committed May 25, 2023
1 parent 68f273c commit fce8ec2
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .ruff.toml
Original file line number Diff line number Diff line change
@@ -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"]
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions invenio_campusonline/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions invenio_campusonline/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"""Types."""

from dataclasses import astuple, dataclass
from datetime import datetime
from enum import Enum
from xml.etree.ElementTree import Element

Expand Down Expand Up @@ -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."""
Expand Down
24 changes: 23 additions & 1 deletion invenio_campusonline/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
75 changes: 75 additions & 0 deletions tests/minimal_record.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<getMetadataByThesisIDResponse xmlns="http://www.campusonline.at/thesisservice/basetypes">
<thesis>
<ID></ID>
<attr key="PAG"></attr>
<attr key="CO">2023-02-16 14:10:32</attr>
<attr key="CHD">2023-04-18 12:42:28</attr>
<attr key="EJAHR"/>
<attr key="ARCHD"/>
<attr key="PUBD"/>
<attr key="PUBLIC"/>
<attr key="STATUS">IFG</attr>
<attr key="STATUSD">2023-04-20 10:49:32</attr>
<attr key="ORG">Institute</attr>
<attr key="ORGP">University</attr>
<attr key="TYPKB">DISS</attr>
<attr key="TYP">Dissertation</attr>
<attr key="ZUGKB">AZ</attr>
<attr key="ZUG">anonym zugänglich</attr>
<attr key="SPSTAT">NG</attr>
<attr key="SPVON">2023-03-03 01:15:28</attr>
<attr key="SPBIS">2025-03-03 01:15:28</attr>
<attr key="SPBGR"/>
<attr key="OLANG">EN</attr>
<attr key="TLANGS">DE;</attr>
<attr key="VOLLTEXT">J</attr>
<metaclass>
<name>AUTHOR</name>
<metaobj>
<attr key="TYP">A</attr>
<attr key="FN">Max</attr>
<attr key="LN">Mustermann</attr>
<attr key="FNLN">Mustermann, Max</attr>
<attr key="AKK"/>
<attr key="MNR"></attr>
<attr key="STRI"></attr>
<attr key="STKZ"></attr>
</metaobj>
</metaclass>
<metaclass>
<name>SUPERVISOR</name>
<metaobj>
<attr key="TYP">1BUTUG</attr>
<attr key="FN">John</attr>
<attr key="LN">Doe</attr>
<attr key="FNLN">Doe, John</attr>
<attr key="AKK"></attr>
<attr key="ORG"></attr>
<attr key="INTERN">J</attr>
</metaobj>
</metaclass>
<metaclass>
<name>TEXT</name>
<metaobj>
<attr key="LANG">EN</attr>
<attr key="ORIG">J</attr>
<attr key="TIT">Title Lorem Ipsum</attr>
<attr key="ABS"></attr>
<attr key="KEYW"></attr>
</metaobj>
<metaobj>
<attr key="LANG">DE</attr>
<attr key="ORIG">N</attr>
<attr key="TIT">Titel Lorem Ipsum</attr>
<attr key="ABS"></attr>
<attr key="KEYW"/>
</metaobj>
</metaclass>
</thesis>
<errorCode>0</errorCode>
</getMetadataByThesisIDResponse>
</soapenv:Body>
</soapenv:Envelope>
19 changes: 19 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)


Expand Down Expand Up @@ -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

0 comments on commit fce8ec2

Please sign in to comment.