Skip to content

Commit

Permalink
Merge pull request #50 from GeoNodeUserGroup-DE/issue_#48_Feature_all…
Browse files Browse the repository at this point in the history
…ow_searching_through_filter_parameter

[Fixes #48] Feature: allow searching through filter parameter
  • Loading branch information
mwallschlaeger authored May 8, 2024
2 parents dff23ef + b4deaad commit b1b0885
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 43 deletions.
103 changes: 77 additions & 26 deletions geonodectl
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"
Expand All @@ -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",
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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
)
Expand All @@ -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 ...")
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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 #
Expand All @@ -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"
Expand Down
24 changes: 16 additions & 8 deletions geonoderest/geonodeobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,20 @@ def list(self, **kwargs) -> Dict:
Returns:
Dict: request response
"""
endpoint = f"{self.ENDPOINT_NAME}/"
params = {}

endpoint = f"{self.ENDPOINT_NAME}/?page_size={kwargs['page_size']}&page={kwargs['page']}"
if "page_size" in kwargs:
params["page_size"] = kwargs["page_size"]
if "page" in kwargs:
params["page"] = kwargs["page"]

r = self.http_get(endpoint=endpoint)
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):
Expand Down Expand Up @@ -99,9 +109,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):
Expand All @@ -117,7 +125,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]
12 changes: 6 additions & 6 deletions geonoderest/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions geonoderest/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions json-examples/tkeywords.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"tkeywords": [{"alt_label": "Abbau" },{"alt_label": "Öffentlichkeitskampagne"}]}

0 comments on commit b1b0885

Please sign in to comment.