From 639db8ad3106f8345d3d09a29721aa89d3af38ea Mon Sep 17 00:00:00 2001 From: mwallschlaeger Date: Wed, 8 May 2024 10:46:19 +0200 Subject: [PATCH 1/2] [Fixes #48] Feature: allow searching through filter parameter --- geonodectl | 103 +++++++++++++----- geonoderest/geonodeobject.py | 25 +++-- geonoderest/rest.py | 12 +- geonoderest/users.py | 4 +- ...patch.json => resource_attribute_set.json} | 0 json-examples/tkeywords.json | 1 + 6 files changed, 100 insertions(+), 45 deletions(-) rename json-examples/{patch.json => resource_attribute_set.json} (100%) create mode 100644 json-examples/tkeywords.json diff --git a/geonodectl b/geonodectl index 099ac31..a7ad462 100755 --- a/geonodectl +++ b/geonodectl @@ -30,6 +30,7 @@ DEFAULT_CHARSET: str = "UTF-8" DEFAULT_CMD_PAGE_SIZE: int = 80 DEFAULT_CMD_PAGE: int = 1 + class AliasedSubParsersAction(argparse._SubParsersAction): class _AliasedPseudoAction(argparse.Action): def __init__(self, name, aliases, help): @@ -127,7 +128,7 @@ To use this tool you have to set the following environment variables before star type=int, help=" A page number within the paginated result set", ) - + subparsers = parser.add_subparsers( help="geonodectl commands", dest="command", required=True ) @@ -183,7 +184,14 @@ To use this tool you have to set the following environment variables before star ) # LIST - datasets_subparsers.add_parser("list", help="list datasets") + datasets_list = datasets_subparsers.add_parser("list", help="list datasets") + datasets_list.add_argument( + "--filter", + action=kwargs_append_action, + dest="filter", + type=str, + help="filter datasets by key value pairs. E.g. --filter is_published=true owner.username=admin, or --filter title=test", + ) # UPLOAD datasets_upload = datasets_subparsers.add_parser( @@ -224,7 +232,9 @@ To use this tool you have to set the following environment variables before star "patch", help="patch datasets metadata" ) datasets_patch.add_argument(type=int, dest="pk", help="pk of dataset to patch") - datasets_patch_mutually_exclusive_group = datasets_patch.add_mutually_exclusive_group() + datasets_patch_mutually_exclusive_group = ( + datasets_patch.add_mutually_exclusive_group() + ) datasets_patch_mutually_exclusive_group.add_argument( "--set", @@ -268,8 +278,14 @@ To use this tool you have to set the following environment variables before star ) # LIST - documents_subparsers.add_parser("list", help="list documents") - + documents_list = documents_subparsers.add_parser("list", help="list documents") + documents_list.add_argument( + "--filter", + action=kwargs_append_action, + dest="filter", + type=str, + help="filter document by key value pairs. E.g. --filter is_published=true owner.username=admin, or --filter title=test", + ) # UPLOAD documents_upload = documents_subparsers.add_parser( "upload", help="upload new datasets" @@ -291,9 +307,13 @@ To use this tool you have to set the following environment variables before star ) # PATCH - documents_patch = documents_subparsers.add_parser("patch", help="patch documents metadata") + documents_patch = documents_subparsers.add_parser( + "patch", help="patch documents metadata" + ) documents_patch.add_argument(type=int, dest="pk", help="pk of documents to patch") - documents_patch_mutually_exclusive_group = documents_patch.add_mutually_exclusive_group() + documents_patch_mutually_exclusive_group = ( + documents_patch.add_mutually_exclusive_group() + ) documents_patch_mutually_exclusive_group.add_argument( "--set", @@ -332,8 +352,14 @@ To use this tool you have to set the following environment variables before star help="geonodectl maps commands", dest="subcommand", required=True ) # LIST - maps_subparsers.add_parser("list", help="list documents") - + maps_list = maps_subparsers.add_parser("list", help="list documents") + maps_list.add_argument( + "--filter", + action=kwargs_append_action, + dest="filter", + type=str, + help="filter maps by key value pairs. E.g. --filter is_published=true owner.username=admin, or --filter title=test", + ) # PATCH maps_patch = maps_subparsers.add_parser("patch", help="patch maps metadata") maps_patch.add_argument(type=int, dest="pk", help="pk of map to patch") @@ -403,15 +429,23 @@ To use this tool you have to set the following environment variables before star ) # LIST - geoapps_subparsers.add_parser("list", help="list geoapps") - + geoapps_list = geoapps_subparsers.add_parser("list", help="list geoapps") + geoapps_list.add_argument( + "--filter", + action=kwargs_append_action, + dest="filter", + type=str, + help="filter geoapps by key value pairs. E.g. --filter is_published=true owner.username=admin, or --filter title=test", + ) # PATCH geoapps_patch = geoapps_subparsers.add_parser( "patch", help="patch geoapps metadata" ) geoapps_patch.add_argument(type=int, dest="pk", help="pk of geoapp to patch") - geoapps_patch_mutually_exclusive_group = geoapps_patch.add_mutually_exclusive_group() + geoapps_patch_mutually_exclusive_group = ( + geoapps_patch.add_mutually_exclusive_group() + ) geoapps_patch_mutually_exclusive_group.add_argument( "--set", dest="fields", @@ -453,7 +487,7 @@ To use this tool you have to set the following environment variables before star # PATCH users_patch = users_subparsers.add_parser("patch", help="patch users metadata") users_patch.add_argument(type=int, dest="pk", help="pk of user to patch") - + user_patch_mutually_exclusive_group = users_patch.add_mutually_exclusive_group() user_patch_mutually_exclusive_group.add_argument( "--set", @@ -470,12 +504,8 @@ To use this tool you have to set the following environment variables before star ) # DESCRIBE - users_describe = users_subparsers.add_parser( - "describe", help="get users details" - ) - users_describe.add_argument( - type=int, dest="pk", help="pk of users to describe ..." - ) + users_describe = users_subparsers.add_parser("describe", help="get users details") + users_describe.add_argument(type=int, dest="pk", help="pk of users to describe ...") users_describe_subgroup = users_describe.add_mutually_exclusive_group( required=False ) @@ -495,8 +525,14 @@ To use this tool you have to set the following environment variables before star ) # LIST - users_subparsers.add_parser("list", help="list documents") - + users_list = users_subparsers.add_parser("list", help="list documents") + users_list.add_argument( + "--filter", + action=kwargs_append_action, + dest="filter", + type=str, + help="filter users by key value pairs. E.g. --filter last_name=svenson or --filter username=svenson", + ) # DELETE users_delete = users_subparsers.add_parser("delete", help="delete existing user") users_delete.add_argument(type=int, dest="pk", help="pk of geoapp to delete ...") @@ -537,7 +573,7 @@ To use this tool you have to set the following environment variables before star users_create.add_argument( "--is_superuser", - action='store_true', + action="store_true", required=False, dest="is_superuser", default=False, @@ -546,7 +582,7 @@ To use this tool you have to set the following environment variables before star users_create.add_argument( "--is_staff", - action='store_true', + action="store_true", required=False, dest="is_staff", default=False, @@ -576,7 +612,14 @@ To use this tool you have to set the following environment variables before star ) # LIST - uploads_subparsers.add_parser("list", help="list uploads") + uploads_list = uploads_subparsers.add_parser("list", help="list uploads") + uploads_list.add_argument( + "--filter", + action=kwargs_append_action, + dest="filter", + type=str, + help="filter uploads by key value pairs. E.g. --filter title=test", + ) ##################################### # EXECUTIONREQUEST ARGUMENT PARSING # @@ -589,8 +632,16 @@ To use this tool you have to set the following environment variables before star ) # LIST - executionrequest_subparsers.add_parser("list", help="list executionrequests") - + executionrequest_list = executionrequest_subparsers.add_parser( + "list", help="list executionrequests" + ) + executionrequest_list.add_argument( + "--filter", + action=kwargs_append_action, + dest="filter", + type=str, + help="filter execution requests by key value pairs. E.g. --filter status=ready", + ) # DESCRIBE executionrequest_describe = executionrequest_subparsers.add_parser( "describe", help="get executionrequest details" diff --git a/geonoderest/geonodeobject.py b/geonoderest/geonodeobject.py index da01694..7c18240 100644 --- a/geonoderest/geonodeobject.py +++ b/geonoderest/geonodeobject.py @@ -36,10 +36,17 @@ def list(self, **kwargs) -> Dict: Returns: Dict: request response """ - - endpoint = f"{self.ENDPOINT_NAME}/?page_size={kwargs['page_size']}&page={kwargs['page']}" - - r = self.http_get(endpoint=endpoint) + endpoint = f"{self.ENDPOINT_NAME}/" + params = { + "page_size": kwargs["page_size"], + "page": kwargs["page"], + } + if "filter" in kwargs and kwargs["filter"] is not None: + for field, value in kwargs["filter"].items(): + field = "filter{" + field + "}" + params[field] = value + + r = self.http_get(endpoint=endpoint, params=params) return r[self.JSON_OBJECT_NAME] def cmd_delete(self, pk: int, **kwargs): @@ -99,9 +106,7 @@ def patch( json_content: Optional[Dict] = None, **kwargs, ): - obj = self.http_patch( - endpoint=f"{self.ENDPOINT_NAME}/{pk}/", params=json_content - ) + obj = self.http_patch(endpoint=f"{self.ENDPOINT_NAME}/{pk}/", data=json_content) return obj def cmd_describe(self, pk: int, **kwargs): @@ -117,7 +122,7 @@ def get(self, pk: int, **kwargs) -> Dict: Returns: Dict: obj details """ - r = self.http_get( - endpoint=f"{self.ENDPOINT_NAME}/{pk}?page_size={kwargs['page_size']}" - ) + endpoint = f"{self.ENDPOINT_NAME}/{pk}" + r = self.http_get(endpoint=endpoint) + return r[self.SINGULAR_RESOURCE_NAME] diff --git a/geonoderest/rest.py b/geonoderest/rest.py index adda7c0..67bbd1c 100644 --- a/geonoderest/rest.py +++ b/geonoderest/rest.py @@ -131,19 +131,21 @@ def http_get(self, endpoint: str, params: Dict = {}) -> Dict: """ url = self.url + endpoint try: - r = requests.get(url, headers=self.header, json=params, verify=self.verify) + r = requests.get( + url, headers=self.header, params=params, verify=self.verify + ) r.raise_for_status() except requests.exceptions.HTTPError as err: raise SystemExit(err) return r.json() @network_exception_handling - def http_patch(self, endpoint: str, params: Dict = {}) -> Dict: + def http_patch(self, endpoint: str, data: Dict = {}) -> Dict: """execute http patch on endpoint with params Args: endpoint (str): api endpoint - params (Dict, optional): params dict provided with the delete + data (Dict, optional): params dict with data to patch Raises: SystemExit: if bad http resonse raise SystemExit with logging @@ -153,9 +155,7 @@ def http_patch(self, endpoint: str, params: Dict = {}) -> Dict: """ url = self.url + endpoint try: - r = requests.patch( - url, headers=self.header, json=params, verify=self.verify - ) + r = requests.patch(url, headers=self.header, json=data, verify=self.verify) r.raise_for_status() except requests.exceptions.HTTPError as err: logging.error(r.text) diff --git a/geonoderest/users.py b/geonoderest/users.py index 0416f6b..6c54ef8 100644 --- a/geonoderest/users.py +++ b/geonoderest/users.py @@ -152,9 +152,7 @@ def patch( # The JSON content is sent as part of the request parameters. # The response from the API is returned. - obj = self.http_patch( - endpoint=f"{self.ENDPOINT_NAME}/{pk}/", params=json_content - ) + obj = self.http_patch(endpoint=f"{self.ENDPOINT_NAME}/{pk}/", data=json_content) return obj def cmd_create( diff --git a/json-examples/patch.json b/json-examples/resource_attribute_set.json similarity index 100% rename from json-examples/patch.json rename to json-examples/resource_attribute_set.json diff --git a/json-examples/tkeywords.json b/json-examples/tkeywords.json new file mode 100644 index 0000000..210cafc --- /dev/null +++ b/json-examples/tkeywords.json @@ -0,0 +1 @@ +{"tkeywords": [{"alt_label": "Abbau" },{"alt_label": "Öffentlichkeitskampagne"}]} \ No newline at end of file From b4deaad901dd3a2b06396fb8e1e60e53a8aa7cec Mon Sep 17 00:00:00 2001 From: mwallschlaeger Date: Wed, 8 May 2024 10:58:38 +0200 Subject: [PATCH 2/2] [Fixes #48] Feature: allow searching through filter parameter --- geonoderest/geonodeobject.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/geonoderest/geonodeobject.py b/geonoderest/geonodeobject.py index 7c18240..e1891e4 100644 --- a/geonoderest/geonodeobject.py +++ b/geonoderest/geonodeobject.py @@ -37,10 +37,13 @@ def list(self, **kwargs) -> Dict: Dict: request response """ endpoint = f"{self.ENDPOINT_NAME}/" - params = { - "page_size": kwargs["page_size"], - "page": kwargs["page"], - } + params = {} + + if "page_size" in kwargs: + params["page_size"] = kwargs["page_size"] + if "page" in kwargs: + params["page"] = kwargs["page"] + if "filter" in kwargs and kwargs["filter"] is not None: for field, value in kwargs["filter"].items(): field = "filter{" + field + "}"