Skip to content

Commit

Permalink
Merge pull request #21 from GeoNodeUserGroup-DE/issue#18_UPLOAD_suppo…
Browse files Browse the repository at this point in the history
…rt_new_importer_design_in_verison_4.1

Issue#18 upload support new importer design in verison 4.1
  • Loading branch information
mwallschlaeger authored Aug 23, 2023
2 parents a2e83b2 + 3c9f088 commit 955fdcd
Show file tree
Hide file tree
Showing 16 changed files with 260 additions and 49 deletions.
33 changes: 24 additions & 9 deletions geonodectl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import logging
import os
import sys
import argparse
from typing import List
from typing import List, Union
from argparse import RawTextHelpFormatter
from pathlib import Path

Expand All @@ -17,6 +17,7 @@ from src.maps import GeonodeMapsHandler
from src.people import GeonodePeopleHandler
from src.geoapps import GeonodeGeoappsHandler
from src.uploads import GeonodeUploadsHandler
from src.executionrequest import GeonodeExecutionRequestHandler

GEONODECTL_URL_ENV_VAR: str = "GEONODECTL_URL"
GEONODECTL_BASIC_ENV_VAR: str = "GEONODECTL_BASIC"
Expand Down Expand Up @@ -186,12 +187,6 @@ To use this tool you have to set the following environment variables before star
required=True,
help="file to upload",
)
datasets_upload.add_argument(
"-t", "--title", type=str, dest="title", required=True, help="dataset title"
)
datasets_upload.add_argument(
"-a", "--abstract", type=str, dest="abstract", help="dataset abstract"
)
datasets_upload.add_argument(
"--time",
action="store_true",
Expand Down Expand Up @@ -337,7 +332,6 @@ To use this tool you have to set the following environment variables before star
help="patch metadata by providing a json string like: \'{\"category\":\"{\"identifier\": \"farming\"}}\'",
)


# DESCRIBE
maps_describe = maps_subparsers.add_parser("describe", help="get map details")
maps_describe.add_argument(
Expand Down Expand Up @@ -460,6 +454,25 @@ To use this tool you have to set the following environment variables before star
# LIST
uploads_subparsers.add_parser("list", help="list uploads")

#####################################
# EXECUTIONREQUEST ARGUMENT PARSING #
#####################################
executionrequest = subparsers.add_parser("executionrequest", help="executionrequest commands")
executionrequest_subparsers = executionrequest.add_subparsers(
help="geonodectl executionrequest commands", dest="subcommand", required=True
)

# LIST
executionrequest_subparsers.add_parser("list", help="list executionrequests")

# DESCRIBE
executionrequest_describe = executionrequest_subparsers.add_parser(
"describe", help="get executionrequest details"
)
executionrequest_describe.add_argument(
type=str, dest="exec_id", help="exec_id of executionrequest to describe ..."
)

#####################
# END OF ARGPARSING #
#####################
Expand All @@ -480,7 +493,7 @@ To use this tool you have to set the following environment variables before star
)

geonode_env = GeonodeEnv(url=url, auth_basic=basic, verify=args.ssl_verify)
g_obj: GeonodeObjectHandler
g_obj: Union[GeonodeObjectHandler, GeonodeExecutionRequestHandler]
match args.command:
case "resources" | "resource":
g_obj = GeonodeResourceHandler(env=geonode_env)
Expand All @@ -496,6 +509,8 @@ To use this tool you have to set the following environment variables before star
g_obj = GeonodeGeoappsHandler(env=geonode_env)
case "uploads":
g_obj = GeonodeUploadsHandler(env=geonode_env)
case "executionrequest" | "execrequest":
g_obj = GeonodeExecutionRequestHandler(env=geonode_env)
case _:
raise NotImplemented

Expand Down
11 changes: 11 additions & 0 deletions json-examples/contact_roles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"processor": [
{
"pk": 1000,
"username": "admin"
},
{
"pk": 1001
}
]
}
12 changes: 12 additions & 0 deletions json-examples/funders.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"funders": [
{
"award_title": "MOTivational strength of ecosystem services",
"award_uri": "http://cordis.europa.eu/project/rcn/100180_en.html",
"award_number": "282625",
"funding_reference": {
"funder_identifier_type": "BMBF"
}
}
]
}
23 changes: 23 additions & 0 deletions json-examples/related_identifier.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"related_identifier": [
{
"related_identifier": "test10",
"related_identifier_type": {
"label": "ARK"
},
"relation_type": {
"label": "IsNewVersionOf"
}
},
{
"related_identifier": "test2",
"related_identifier_type": {
"label": "bibcode"
},
"relation_type": {
"label": "Documents"
}
}
]
}

1 change: 1 addition & 0 deletions json-examples/relatedprojects.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"related_projects": [{"label": "signal"},{"label": "rhizo_bread_and_beer"}]}
36 changes: 21 additions & 15 deletions src/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
from src.geonodetypes import GeonodeHTTPFile
from src.cmdprint import show_list
from src.geonodetypes import GeonodeCmdOutListKey, GeonodeCmdOutDictKey
from src.executionrequest import GeonodeExecutionRequestHandler


class GeonodeDatasetsHandler(GeonodeResourceHandler):
RESOURCE_TYPE = "datasets"
"""docstring for GeonodeDatasetsHandler"""

ENDPOINT_NAME = JSON_OBJECT_NAME = "datasets"
SINGULAR_RESOURCE_NAME = "dataset"

LIST_CMDOUT_HEADER = [
Expand All @@ -30,7 +33,7 @@ def cmd_upload(
charset: str = "UTF-8",
time: bool = False,
mosaic: bool = False,
**kwargs
**kwargs,
):
"""upload data and show them on the cmdline
Expand All @@ -47,30 +50,35 @@ def cmd_upload(
charset=charset,
time=time,
mosaic=mosaic,
**kwargs
**kwargs,
)
if kwargs["json"] is True:
self.print_json(r)
if "execution_id" not in r:
raise SystemExit(f"unexpected API response ...\n{r}")

execution_request_handler = GeonodeExecutionRequestHandler(
env=self.gn_credentials
)
er = execution_request_handler.get(exec_id=str(r["execution_id"]), **kwargs)

if kwargs["json"] is True:
self.print_json(er)
else:
list_items = [
["title", title],
["success", str(r["success"])],
["status", r["status"]],
["bbox", r["bbox"] if "bbox" in r else ""],
["crs", r["crs"] if "crs" in r else ""],
["url", r["url"] if "url" in r else ""],
["exec_id", str(er["exec_id"])],
["status", str(er["status"])],
["created", str(er["created"])],
["name", str(er["name"])],
["link", str(er["link"])],
]
show_list(values=list_items, headers=["key", "value"])

def upload(
self,
title: str,
file_path: Path,
charset: str = "UTF-8",
time: bool = False,
mosaic: bool = False,
**kwargs
**kwargs,
) -> Dict:
"""Upload dataset to geonode.
Expand Down Expand Up @@ -144,8 +152,6 @@ def upload(
params = {
# layer permissions
"permissions": '{ "users": {"AnonymousUser": ["view_resourcebase"]} , "groups":{}}',
"dataset_title": title,
"abstract": kwargs["abstract"] if "abstract" in kwargs else "",
"mosaic": mosaic,
"time": str(time),
"charset": charset,
Expand Down
2 changes: 1 addition & 1 deletion src/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


class GeonodeDocumentsHandler(GeonodeResourceHandler):
RESOURCE_TYPE = "documents"
ENDPOINT_NAME = JSON_OBJECT_NAME = "documents"
SINGULAR_RESOURCE_NAME = "document"

LIST_CMDOUT_HEADER = [
Expand Down
60 changes: 60 additions & 0 deletions src/executionrequest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from src.geonodetypes import GeonodeCmdOutListKey
from src.geonodeobject import GeonodeObjectHandler
from src.rest import GeonodeRest

from typing import Dict


class GeonodeExecutionRequestHandler(GeonodeRest):
ENDPOINT_NAME = "executionrequest"
JSON_OBJECT_NAME = "requests"
SINGULAR_RESOURCE_NAME = "request"

# TODO
LIST_CMDOUT_HEADER = [
GeonodeCmdOutListKey(key="exec_id"),
GeonodeCmdOutListKey(key="name"),
GeonodeCmdOutListKey(key="status"),
GeonodeCmdOutListKey(key="user"),
GeonodeCmdOutListKey(key="source"),
GeonodeCmdOutListKey(key="created"),
GeonodeCmdOutListKey(key="log"),
]

def cmd_describe(self, exec_id: str, **kwargs):
obj = self.get(exec_id=exec_id, **kwargs)
GeonodeObjectHandler.print_json(obj)

def get(self, exec_id: str, **kwargs) -> Dict:
"""
get details for a given exec_id
Args:
exec_id (str): exec_id of the object
Returns:
Dict: obj details
"""
r = self.http_get(
endpoint=f"{self.ENDPOINT_NAME}/{exec_id}?page_size={kwargs['page_size']}"
)
return r[self.SINGULAR_RESOURCE_NAME]

def cmd_list(self, **kwargs):
"""show list of geonode obj on the cmdline"""
obj = self.list(**kwargs)
if kwargs["json"]:
GeonodeObjectHandler.print_json(obj)
else:
GeonodeObjectHandler.print_list_on_cmd(obj)

def list(self, **kwargs) -> Dict:
"""returns dict of datasets from geonode
Returns:
Dict: request response
"""
r = self.http_get(
endpoint=f"{self.ENDPOINT_NAME}/?page_size={kwargs['page_size']}"
)
return r[self.JSON_OBJECT_NAME]
2 changes: 1 addition & 1 deletion src/geoapps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


class GeonodeGeoappsHandler(GeonodeResourceHandler):
RESOURCE_TYPE = "geoapps"
ENDPOINT_NAME = JSON_OBJECT_NAME = "geoapps"
SINGULAR_RESOURCE_NAME = "geoapp"

LIST_CMDOUT_HEADER = [
Expand Down
32 changes: 21 additions & 11 deletions src/geonodeobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@ class GeonodeObjectHandler(GeonodeRest):
GeonodeCmdOutListKey(type=list, key="pk")
]
DEFAULT_UPLOAD_KEYS: List[str] = ["key", "value"]
RESOURCE_TYPE: str = ""
JSON_OBJECT_NAME: str = ""
ENDPOINT_NAME: str = ""
SINGULAR_RESOURCE_NAME: str = ""

@classmethod
def print_json(cls, json_str: Union[str, dict]):
print(json.dumps(json_str, indent=2))

def cmd_list(self, **kwargs):
"""show list of geonode obj on the cmdline"""
obj = self.list(**kwargs)
Expand All @@ -36,17 +33,17 @@ def list(self, **kwargs) -> Dict:
Dict: request response
"""
r = self.http_get(
endpoint=f"{self.RESOURCE_TYPE}/?page_size={kwargs['page_size']}"
endpoint=f"{self.ENDPOINT_NAME}/?page_size={kwargs['page_size']}"
)
return r[self.RESOURCE_TYPE]
return r[self.JSON_OBJECT_NAME]

def cmd_delete(self, pk: int, **kwargs):
self.delete(pk=pk, **kwargs)
print(f"{self.RESOURCE_TYPE}: {pk} deleted ...")
print(f"{self.JSON_OBJECT_NAME}: {pk} deleted ...")

def delete(self, pk: int, **kwargs):
"""delete geonode resource object"""
self.http_get(endpoint=f"{self.RESOURCE_TYPE}/{pk}")
self.http_get(endpoint=f"{self.ENDPOINT_NAME}/{pk}")
self.http_delete(endpoint=f"resources/{pk}/delete")

def cmd_patch(self, pk: int, fields: str, **kwargs):
Expand Down Expand Up @@ -75,7 +72,7 @@ def patch(self, pk: int, fields: str, **kwargs) -> Dict:
)
)

return self.http_patch(endpoint=f"{self.RESOURCE_TYPE}/{pk}/", params=json_data)
return self.http_patch(endpoint=f"{self.ENDPOINT_NAME}/{pk}/", params=json_data)

def cmd_describe(self, pk: int, **kwargs):
obj = self.get(pk=pk, **kwargs)
Expand All @@ -91,7 +88,7 @@ def get(self, pk: int, **kwargs) -> Dict:
Dict: obj details
"""
r = self.http_get(
endpoint=f"{self.RESOURCE_TYPE}/{pk}?page_size={kwargs['page_size']}"
endpoint=f"{self.ENDPOINT_NAME}/{pk}?page_size={kwargs['page_size']}"
)
return r[self.SINGULAR_RESOURCE_NAME]

Expand Down Expand Up @@ -119,3 +116,16 @@ def generate_line(i, obj: Dict, headers: List[GeonodeCmdOutObjectKey]) -> List:
generate_line(i, obj, cls.LIST_CMDOUT_HEADER) for i in range(len(obj))
]
show_list(headers=cls.cmd_list_header(), values=values)

@classmethod
def print_json(cls, json_str: Union[str, dict]):
"""
Print the given JSON string or dictionary with an indentation of 2 spaces.
Args:
json_str (Union[str, dict]): The JSON string or dictionary to be printed.
Returns:
None
"""
print(json.dumps(json_str, indent=2))
Loading

0 comments on commit 955fdcd

Please sign in to comment.