Skip to content

Commit ae1e492

Browse files
nheebpoikilotherm
authored andcommitted
Init choose access_right & dataverse check_if_all_valid
1 parent 0f5814f commit ae1e492

2 files changed

Lines changed: 101 additions & 11 deletions

File tree

src/hermes/commands/deposit/dataverse.py

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,87 @@ class DataverseDepositPlugin(BaseDepositPlugin):
3333
settings_class = DataverseDepositSettings
3434

3535
def __init__(self, command, ctx) -> None:
36+
"""
37+
Sets up the DataverseDepositPlugin with data from the hermes toml.
38+
Tests if everything is valid and creates an easyDataverse client.
39+
"""
3640
super().__init__(command, ctx)
3741
self.config = getattr(self.command.settings, self.platform_name)
42+
self.check_if_all_valid()
43+
self.client = Dataverse(server_url=self.config.site_url, api_token=self.config.api_token)
44+
self.ctx_path = ContextPath.parse(f"deposit.{self.platform_name}")
45+
46+
def check_if_all_valid(self) -> None:
47+
"""
48+
Tests if all conditions are met before starting the rest of the deposit.
49+
"""
50+
self.check_version()
51+
self.check_api_token()
52+
self.check_target_collection()
53+
self.check_target_pid()
54+
self.check_publication_type()
55+
56+
def check_version(self) -> None:
57+
"""
58+
Tests if the site_url is reachable as dataverse instance.
59+
Also saves the dataverse version in the context incase we want to use it later on.
60+
"""
61+
url = f"{self.config.site_url}/api/info/version"
62+
res = requests.get(url)
63+
if not res.ok:
64+
raise RuntimeError(f"Dataverse ({self.config.site_url}) not reachable.")
65+
version_info = res.json().get("data", {}).get("version", "")
66+
self.ctx.update(self.ctx_path["dataverse_version"], version_info)
67+
68+
def check_api_token(self) -> None:
3869
api_token = self.config.api_token
3970
if not api_token:
40-
raise DepositionUnauthorizedError("No valid auth token given for deposition platform")
41-
self.client = Dataverse(server_url=self.config.site_url, api_token=api_token)
42-
self.ctx_path = ContextPath.parse(f"deposit.{self.platform_name}")
71+
raise DepositionUnauthorizedError("No api-token given for deposition platform (dataverse).")
72+
token_valid_url = f"{self.config.site_url}/api/users/token"
73+
token_valid_response = requests.get(token_valid_url, headers={"X-Dataverse-key": api_token})
74+
if not token_valid_response.ok:
75+
raise DepositionUnauthorizedError("Given api-token for deposition platform (dataverse) is not valid.")
76+
77+
def check_target_collection(self) -> None:
78+
"""
79+
Tests if the target collection exists.
80+
"""
81+
target_collection = self.config.target_collection
82+
url = f"{self.config.site_url}/api/dataverses/{target_collection}"
83+
res = requests.get(url)
84+
if not res.ok:
85+
raise RuntimeError(f"Dataverse collection '{target_collection}' not found.")
86+
87+
def check_target_pid(self) -> None:
88+
"""
89+
Tests if the given pid is valid.
90+
"""
91+
if not self.config.target_pid:
92+
return
93+
url = f"{self.config.site_url}/api/datasets/:persistentId/?persistentId={self.config.target_pid}"
94+
res = requests.get(url)
95+
if not res.ok:
96+
raise RuntimeError(f"Dataset {self.config.target_pid} not found.")
97+
data = res.json().get("data", {})
98+
if self.config.target_collection and not data.get("ownerAlias") == self.config.target_collection:
99+
_log.warning("Dataset is not located inside the target collection.")
100+
101+
def check_publication_type(self) -> None:
102+
"""
103+
Tests if the given publication type (most likely "software") is supported by the target dataverse.
104+
"""
105+
url = f"{self.config.site_url}/api/datasets/datasetTypes"
106+
res = requests.get(url)
107+
if res.ok:
108+
types = res.json().get("data", [])
109+
type_names = [t["name"] for t in types]
110+
if self.config.publication_type not in type_names:
111+
raise RuntimeError(
112+
f"Publication type '{self.config.publication_type}' not supported on target Dataverse.")
113+
else:
114+
# TBD what to do when showing supported datasetTypes does not work?
115+
# This is currently the case for https://data.fz-juelich.de/ & https://data-beta.fz-juelich.de/
116+
pass
43117

44118
def map_metadata(self) -> None:
45119
"""
@@ -54,7 +128,7 @@ def map_metadata(self) -> None:
54128
def is_initial_publication(self) -> bool:
55129
return self.config.target_pid is None
56130

57-
def update_metadata_on_dataset(self, dataset: Dataset):
131+
def update_metadata_on_dataset(self, dataset: Dataset) -> None:
58132
"""
59133
Sets metadata on an easyDataverse.Dataset using the depositionMetadata
60134
"""

src/hermes/commands/init/base.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -703,15 +703,31 @@ def integrate_deposit_platform(self) -> None:
703703
self.hermes_toml_data["deposit"][deposit_plugin]["site_url"] = self.deposit_platform.url
704704
self.ci_parameters["deposit_parameter_token"] = f"-O {deposit_plugin}.auth_token"
705705
self.ci_parameters["deposit_token_name"] = self.deposit_platform.token_name
706-
# Invenio needs access_right = "open" (no idea what that does, might be outdated)
707-
if deposit_plugin.startswith("invenio"):
708-
self.hermes_toml_data["deposit"][deposit_plugin]["access_right"] = "open"
709-
# Dataverse needs a target_collection name as some sort of directory where the publication will appear
710-
elif deposit_plugin.startswith("dataverse"):
706+
707+
if deposit_plugin.startswith("invenio") or deposit_plugin.startswith("rodare"):
708+
# Invenio & rodare need access_right
709+
# For possible customization we ask the user here
710+
target_access_right = sc.choose(
711+
text="Select an access right for your publication",
712+
options=["open", "closed", "restricted", "embargoed"]
713+
)
714+
self.hermes_toml_data["deposit"][deposit_plugin]["access_right"] = target_access_right
715+
if target_access_right == "restricted":
716+
conditions = sc.answer("Enter the access conditions of the restriction: ")
717+
self.hermes_toml_data["deposit"][deposit_plugin]["access_conditions"] = conditions
718+
elif target_access_right == "embargoed":
719+
embargo_date = sc.answer("Enter the embargo date (YYYY-MM-DD): ")
720+
self.hermes_toml_data["deposit"][deposit_plugin]["embargo_date"] = embargo_date
721+
722+
if deposit_plugin.startswith("dataverse"):
723+
# Dataverse needs a target_collection name as some sort of directory where the publication will appear
711724
target_collection = sc.answer("Enter the name of the collection where you want to publish: ")
712725
self.hermes_toml_data["deposit"][deposit_plugin]["target_collection"] = target_collection
713-
# Rodare needs the robis_pub_id
714-
elif deposit_plugin.startswith("rodare"):
726+
# To be decided: Should we call it api_token in the other deposit plugins or just in dataverse?
727+
self.ci_parameters["deposit_parameter_token"] = f"-O {deposit_plugin}.api_token"
728+
729+
if deposit_plugin.startswith("rodare"):
730+
# Rodare needs the robis_pub_id
715731
robis_pub_id = sc.answer("Enter the corresponding Robis Publication ID: ")
716732
self.hermes_toml_data["deposit"][deposit_plugin]["robis_pub_id"] = robis_pub_id
717733

0 commit comments

Comments
 (0)