From 16770694910f3ef97b3858053f52fbe03551188d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20M=C3=BCller?= Date: Sat, 22 Aug 2020 16:33:22 +0200 Subject: [PATCH] minor fixes and improvements --- CHANGELOG | 6 ++++++ dcoraid/api.py | 5 ++++- dcoraid/upload/dataset.py | 11 ++++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index e20847d..a1f7e3f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,9 @@ +0.0.6 + - enh: allow to create the circle if it does not exist during dataset + creation + - fix: set "state" to "active" in dictionary returned by `create_dataset` + - fix: catch error when user uses `CKANAPI.get` when he should use + `CKANAPI.post` 0.0.5 - basic upload functional 0.0.4 diff --git a/dcoraid/api.py b/dcoraid/api.py index 57900f4..50b0537 100644 --- a/dcoraid/api.py +++ b/dcoraid/api.py @@ -53,7 +53,10 @@ def get(self, api_call, **kwargs): url_call = self.api_url + api_call req = requests.get(url_call, headers=self.headers) data = req.json() - if not data["success"]: + if isinstance(data, str): + raise ValueError( + "Command did not succeed, reason: '{}'".format(data)) + elif not data["success"]: raise ConnectionError( "Could not run API call '{}'! ".format(url_call) + "Reason: {} ({})".format(req.reason, diff --git a/dcoraid/upload/dataset.py b/dcoraid/upload/dataset.py index 50b0a6c..be4489b 100644 --- a/dcoraid/upload/dataset.py +++ b/dcoraid/upload/dataset.py @@ -32,7 +32,7 @@ def activate_dataset(dataset_id, server, api_key): def create_dataset(dataset_dict, server, api_key, resources=[], - activate=False): + create_circle=False, activate=False): """Create a draft dataset Parameters @@ -45,6 +45,8 @@ def create_dataset(dataset_dict, server, api_key, resources=[], API key of the CKAN/DCOR user resources: list of str or pathlib.Path Paths to dataset resources + create_circle: bool + Create the circle if it does not already exist activate: bool If True, then the dataset state is changed to "active" after uploading of the resources is complete. For DCOR, @@ -52,6 +54,12 @@ def create_dataset(dataset_dict, server, api_key, resources=[], dataset. """ api = CKANAPI(server=server, api_key=api_key) + if create_circle: + circles = [c["name"] for c in api.get("organization_list_for_user")] + if dataset_dict["owner_org"] not in circles: + # Create the circle before creating the dataset + api.post("organization_create", + data={"name": dataset_dict["owner_org"]}) dataset_dict = copy.deepcopy(dataset_dict) dataset_dict["state"] = "draft" data = api.post("package_create", dataset_dict) @@ -63,6 +71,7 @@ def create_dataset(dataset_dict, server, api_key, resources=[], api_key=api_key) if activate: activate_dataset(dataset_id=data["id"], server=server, api_key=api_key) + data["state"] = "active" return data