Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fixes #63] Bug: no unified handling of page, page_size and filter refactoring required #64

Merged
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()
33 changes: 24 additions & 9 deletions geonoderest/users.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import json
import sys
import logging
from typing import Dict, Optional

from geonoderest.resources import GeonodeResourceHandler
Expand Down Expand Up @@ -75,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 Expand Up @@ -157,7 +159,7 @@ def patch(

def cmd_create(
self,
username: str,
username: Optional[str] = None,
email: str = "",
first_name: str = "",
last_name: str = "",
Expand Down Expand Up @@ -208,7 +210,7 @@ def cmd_create(

def create(
self,
username: str,
username: Optional[str] = None,
email: str = "",
first_name: str = "",
last_name: str = "",
Expand All @@ -231,6 +233,10 @@ def create(
json_content (dict) dict object with addition metadata / fields
"""
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,
Expand All @@ -243,3 +249,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}")
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading