Skip to content

Commit

Permalink
Merge pull request #64 from GeoNodeUserGroup-DE/issue_#63_Bug_no_unif…
Browse files Browse the repository at this point in the history
…ied_handling_of_page_page_size_and_filter_refactoring_required

[Fixes #63] Bug: no unified handling of page, page_size and filter refactoring required
  • Loading branch information
mwallschlaeger authored Jun 17, 2024
2 parents 7fd3de0 + 9c94714 commit 11c7e59
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
24 changes: 14 additions & 10 deletions geonoderest/geonodeobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]
Expand Down
2 changes: 2 additions & 0 deletions geonoderest/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
14 changes: 7 additions & 7 deletions geonoderest/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,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]

Expand Down

0 comments on commit 11c7e59

Please sign in to comment.