From 601f77755d49db4d7a0a99d79ba018602045f672 Mon Sep 17 00:00:00 2001 From: mwallschlaeger Date: Thu, 13 Jun 2024 15:51:44 +0200 Subject: [PATCH 1/6] [Fixes #63] Bug: no unified handling of page, page_size and filter refactoring required --- geonoderest/geonodeobject.py | 24 ++++++++++++++---------- geonoderest/rest.py | 2 ++ geonoderest/users.py | 14 +++++++------- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/geonoderest/geonodeobject.py b/geonoderest/geonodeobject.py index dc8e1d2..6edb791 100644 --- a/geonoderest/geonodeobject.py +++ b/geonoderest/geonodeobject.py @@ -22,6 +22,19 @@ class GeonodeObjectHandler(GeonodeRest): ENDPOINT_NAME: str = "" SINGULAR_RESOURCE_NAME: str = "" + def __handle_http_params__(self, params: Dict, kwargs: Dict) -> Dict: + 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 + "}" + params[field] = value + + return params + def cmd_list(self, **kwargs): """show list of geonode obj on the cmdline""" obj = self.list(**kwargs) @@ -37,17 +50,8 @@ def list(self, **kwargs) -> Dict: Dict: request response """ endpoint = f"{self.ENDPOINT_NAME}/" - 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 + "}" - params[field] = value + params = self.__handle_http_params__({}, kwargs) r = self.http_get(endpoint=endpoint, params=params) return r[self.JSON_OBJECT_NAME] diff --git a/geonoderest/rest.py b/geonoderest/rest.py index 67bbd1c..3251ed2 100644 --- a/geonoderest/rest.py +++ b/geonoderest/rest.py @@ -181,6 +181,8 @@ def http_delete(self, endpoint: str, params: Dict = {}) -> Dict: try: r = requests.delete(url, headers=self.header, verify=self.verify) r.raise_for_status() + if r.status_code in [204]: + return "" except requests.exceptions.HTTPError as err: raise SystemExit(err) return r.json() diff --git a/geonoderest/users.py b/geonoderest/users.py index 6c54ef8..3714353 100644 --- a/geonoderest/users.py +++ b/geonoderest/users.py @@ -75,19 +75,19 @@ def get( "cannot handle user_resources and user_groups True at the same time ..." ) r: Dict + params = self.__handle_http_params__({}, kwargs) + if user_groups is True: - r = self.http_get( - endpoint=f"{self.ENDPOINT_NAME}/{pk}/groups?page_size={kwargs['page_size']}&page={kwargs['page']}" - ) + r = self.http_get(endpoint=f"{self.ENDPOINT_NAME}/{pk}/groups") return r elif user_resources is True: - endpoint = f"{GeonodeResourceHandler.ENDPOINT_NAME}?page_size={kwargs['page_size']}&page={kwargs['page']}" - endpoint += "&filter{owner.pk}=" + str(pk) - r = self.http_get(endpoint=endpoint) + endpoint = f"{GeonodeResourceHandler.ENDPOINT_NAME}" + r = self.http_get(endpoint=endpoint, params=params) return r else: r = self.http_get( - endpoint=f"{self.ENDPOINT_NAME}/{pk}?page_size={kwargs['page_size']}&page={kwargs['page']}" + endpoint=f"{self.ENDPOINT_NAME}/{pk}", + params=params, ) return r[self.SINGULAR_RESOURCE_NAME] From b28bf2bcd34147e7c4ff6ebd038a01cc96c0c5a1 Mon Sep 17 00:00:00 2001 From: mwallschlaeger Date: Thu, 13 Jun 2024 08:30:42 +0200 Subject: [PATCH 2/6] [Fixes #51] Bug: creating user via json_content expects username, also fixes bug when using delete user --- geonoderest/users.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/geonoderest/users.py b/geonoderest/users.py index 3714353..4b00d68 100644 --- a/geonoderest/users.py +++ b/geonoderest/users.py @@ -157,7 +157,7 @@ def patch( def cmd_create( self, - username: str, + username: str = None, email: str = "", first_name: str = "", last_name: str = "", @@ -208,7 +208,7 @@ def cmd_create( def create( self, - username: str, + username: str = None, email: str = "", first_name: str = "", last_name: str = "", @@ -231,6 +231,7 @@ def create( json_content (dict) dict object with addition metadata / fields """ if json_content is None: + if username is None: json_content = { "username": username, "email": email, @@ -243,3 +244,12 @@ def create( endpoint=self.ENDPOINT_NAME, params=json_content, ) + + def cmd_delete(self, pk: int, **kwargs): + self.delete(pk=pk, **kwargs) + print(f"{self.JSON_OBJECT_NAME}: {pk} deleted ...") + + def delete(self, pk: int, **kwargs): + """delete geonode resource object""" + self.http_get(endpoint=f"{self.ENDPOINT_NAME}/{pk}") + self.http_delete(endpoint=f"users/{pk}") \ No newline at end of file From aa90dace9e8adecb29651149f927c191e206ee56 Mon Sep 17 00:00:00 2001 From: mwallschlaeger Date: Thu, 13 Jun 2024 15:55:37 +0200 Subject: [PATCH 3/6] [Fixes #51] Bug: creating user via json_content expects username, also fixes bug when using delete user --- geonoderest/users.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/geonoderest/users.py b/geonoderest/users.py index 4b00d68..3ef20df 100644 --- a/geonoderest/users.py +++ b/geonoderest/users.py @@ -1,4 +1,6 @@ import json +import sys +import logging from typing import Dict, Optional from geonoderest.resources import GeonodeResourceHandler @@ -232,6 +234,9 @@ def create( """ if json_content is None: if username is None: + logging.error("missing username for user creation ...") + sys.exit(1) + json_content = { "username": username, "email": email, @@ -252,4 +257,4 @@ def cmd_delete(self, pk: int, **kwargs): def delete(self, pk: int, **kwargs): """delete geonode resource object""" self.http_get(endpoint=f"{self.ENDPOINT_NAME}/{pk}") - self.http_delete(endpoint=f"users/{pk}") \ No newline at end of file + self.http_delete(endpoint=f"users/{pk}") From a64d7a80bb4cb09aec2148a6a3ded30ad88ba03c Mon Sep 17 00:00:00 2001 From: mwallschlaeger Date: Mon, 17 Jun 2024 11:24:42 +0200 Subject: [PATCH 4/6] [Fixes #51] Bug: creating user via json_content expects username, also fixes bug when using delete user --- geonoderest/users.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/geonoderest/users.py b/geonoderest/users.py index 3ef20df..a86a386 100644 --- a/geonoderest/users.py +++ b/geonoderest/users.py @@ -159,7 +159,7 @@ def patch( def cmd_create( self, - username: str = None, + username: Optional[str] = None, email: str = "", first_name: str = "", last_name: str = "", @@ -210,7 +210,7 @@ def cmd_create( def create( self, - username: str = None, + username: Optional[str] = None, email: str = "", first_name: str = "", last_name: str = "", From 9ac5c430048f0a134e271deb554ee190bc35f014 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Tue, 21 May 2024 08:22:06 +0000 Subject: [PATCH 5/6] --- updated-dependencies: - dependency-name: requests dependency-type: direct:production ... Signed-off-by: dependabot[bot] --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index fd7ef0f..658de5d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -requests==2.31.0 +requests==2.32.0 types-requests==2.31.0.20240406 tabulate==0.9.0 types-tabulate==0.9.0.20240106 From 9c94714b54069f66172c46013a6b8ca427b765c2 Mon Sep 17 00:00:00 2001 From: mwallschlaeger Date: Mon, 17 Jun 2024 11:42:39 +0200 Subject: [PATCH 6/6] [Fixes #63] Bug: no unified handling of page, page_size and filter refactoring required --- geonoderest/rest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/geonoderest/rest.py b/geonoderest/rest.py index 3251ed2..185dfb0 100644 --- a/geonoderest/rest.py +++ b/geonoderest/rest.py @@ -182,7 +182,7 @@ def http_delete(self, endpoint: str, params: Dict = {}) -> Dict: r = requests.delete(url, headers=self.header, verify=self.verify) r.raise_for_status() if r.status_code in [204]: - return "" + return {} except requests.exceptions.HTTPError as err: raise SystemExit(err) return r.json()