Skip to content

Commit

Permalink
[Fixes #20] add user create function
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Wallschlaeger committed May 1, 2024
1 parent 98b4ea8 commit e0f0540
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 76 deletions.
100 changes: 69 additions & 31 deletions geonodectl
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,16 @@ 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.add_argument(
datasets_patch_mutually_exclusive_group = datasets_patch.add_mutually_exclusive_group()

datasets_patch_mutually_exclusive_group.add_argument(
"--set",
dest="fields",
type=str,
help='patch metadata by providing a json string like: \'{"category":"{"identifier": "farming"}}\'',
)

datasets_patch.add_argument(
datasets_patch_mutually_exclusive_group.add_argument(
"--json_path",
dest="json_path",
type=str,
Expand Down Expand Up @@ -289,16 +291,22 @@ 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.add_argument(type=int, dest="pk", help="pk of document to patch")
documents_patch.add_argument(
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.add_argument(
"--set",
dest="fields",
type=str,
help='patch metadata by providing a json string like: \'{"category":"{"identifier": "farming"}}\'',
)
documents_patch_mutually_exclusive_group.add_argument(
"--json_path",
dest="json_path",
type=str,
help="add metadata by providing a path to a json file",
)

# DESCRIBE
documents_describe = documents_subparsers.add_parser(
Expand Down Expand Up @@ -329,12 +337,20 @@ To use this tool you have to set the following environment variables before star
# 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")
maps_patch.add_argument(
maps_patch_mutually_exclusive_group = maps.add_mutually_exclusive_group()

maps_patch_mutually_exclusive_group.add_argument(
"--set",
dest="fields",
type=str,
help='patch metadata by providing a json string like: \'{"category":"{"identifier": "farming"}}\'',
)
maps_patch_mutually_exclusive_group.add_argument(
"--json_path",
dest="json_path",
type=str,
help="add metadata by providing a path to a json file",
)

# DESCRIBE
maps_describe = maps_subparsers.add_parser("describe", help="get map details")
Expand All @@ -347,22 +363,21 @@ To use this tool you have to set the following environment variables before star
# CREATE
maps_create = maps_subparsers.add_parser("create", help="create an (empty) map")

maps_create.add_argument(
maps_create_mutually_exclusive_group = maps_create.add_mutually_exclusive_group()
maps_create_mutually_exclusive_group.add_argument(
"--title",
type=str,
required=True,
dest="title",
help="title of the new dataset ...",
)

maps_create.add_argument(
maps_create_mutually_exclusive_group.add_argument(
"--set",
dest="fields",
type=str,
help='add metadata by providing a json string like: \'\'{ "category": {"identifier": "farming"}, "abstract": "test abstract" }\'\'',
)

maps_create.add_argument(
maps_create_mutually_exclusive_group.add_argument(
"--json_path",
dest="json_path",
type=str,
Expand Down Expand Up @@ -395,13 +410,22 @@ To use this tool you have to set the following environment variables before star
"patch", help="patch geoapps metadata"
)
geoapps_patch.add_argument(type=int, dest="pk", help="pk of geoapp to patch")
geoapps_patch.add_argument(

geoapps_patch_mutually_exclusive_group = geoapps_patch.add_mutually_exclusive_group()
geoapps_patch_mutually_exclusive_group.add_argument(
"--set",
dest="fields",
type=str,
help='patch metadata by providing a json string like: \'{"category":"{"identifier": "farming"}}\'',
)

geoapps_patch_mutually_exclusive_group.add_argument(
"--json_path",
dest="json_path",
type=str,
help="patch metadata (user credentials) by providing a path to a json file, like --set written in file ...",
)

# DESCRIBE
geoapps_describe = geoapps_subparsers.add_parser(
"describe", help="get geoapp details"
Expand Down Expand Up @@ -429,13 +453,22 @@ 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")
users_patch.add_argument(

user_patch_mutually_exclusive_group = users_patch.add_mutually_exclusive_group()
user_patch_mutually_exclusive_group.add_argument(
"--set",
dest="fields",
type=str,
help='patch metadata by providing a json string like: \'{"category":"{"identifier": "farming"}}\'',
)

user_patch_mutually_exclusive_group.add_argument(
"--json_path",
dest="json_path",
type=str,
help="patch metadata (user credentials) by providing a path to a json file, like --set written in file ...",
)

# DESCRIBE
users_describe = users_subparsers.add_parser(
"describe", help="get users details"
Expand Down Expand Up @@ -470,37 +503,36 @@ To use this tool you have to set the following environment variables before star

# CREATE
users_create = users_subparsers.add_parser("create", help="create a new user")

users_create.add_argument(
user_create_mutually_exclusive_group = users_create.add_mutually_exclusive_group()
user_create_mutually_exclusive_group.add_argument(
"--username",
type=str,
required=True,
dest="username",
help="username of the new user ...",
help="username of the new user ... (mutually exclusive [a])",
)

users_create.add_argument(
"--email",
type=str,
required=True,
required=False,
dest="email",
help="email of the new user ...",
help="email of the new user ... (only working combined with --username) ...",
)

users_create.add_argument(
"--first_name",
type=str,
required=False,
dest="first_name",
help="first_name of the new user ...",
help="first_name of the new user (only working combined with --username) ...",
)

users_create.add_argument(
"--last_name",
type=str,
required=False,
dest="last_name",
help="last_name of the new user ...",
help="last_name of the new user (only working combined with --username) ...",
)

users_create.add_argument(
Expand All @@ -509,7 +541,7 @@ To use this tool you have to set the following environment variables before star
required=False,
dest="is_superuser",
default=False,
help="set to make the new user a superuser ...",
help="set to make the new user a superuser (only working combined with --username) ...",
)

users_create.add_argument(
Expand All @@ -518,15 +550,21 @@ To use this tool you have to set the following environment variables before star
required=False,
dest="is_staff",
default=False,
help="set to make the new user a staff user ...",
help="set to make the new user a staff user (only working combined with --username) ...",
)

users_create.add_argument(
"--password",
user_create_mutually_exclusive_group.add_argument(
"--json_path",
dest="json_path",
type=str,
required=False,
dest="password",
help="password of the new user ...",
help="add metadata (user credentials) by providing a path to a json file, like --set written in file ...(mutually exclusive [b])",
)

user_create_mutually_exclusive_group.add_argument(
"--set",
dest="fields",
type=str,
help='create user by providing a json string like: \'{"username":"test_user", "email":"[email protected]", "first_name": "test_first_name", "last_name":"test_last_name", "is_staff": true, "is_superuser": true}\' ... (mutually exclusive [c])',
)

###########################
Expand Down Expand Up @@ -601,7 +639,7 @@ To use this tool you have to set the following environment variables before star
g_obj = GeonodeExecutionRequestHandler(env=geonode_env)
case _:
raise NotImplemented

print(g_obj,args.__dict__)
g_obj_func = getattr(g_obj, "cmd_" + args.subcommand)
g_obj_func(**args.__dict__)

Expand Down
1 change: 0 additions & 1 deletion geonoderest/apiconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def from_env_vars() -> "GeonodeApiConf":
"GEONODE_API_URL" not in os.environ
or "GEONODE_API_BASIC_AUTH" not in os.environ
):

raise SystemExit(
"env vars not set: GEONODE_API_URL, GEONODE_API_BASIC_AUTH"
)
Expand Down
4 changes: 2 additions & 2 deletions geonoderest/geonodeobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def cmd_patch(
ValueError: catches json.decoder.JSONDecodeError and raises ValueError as decoding is not working
"""

json_content: Dict = {}
json_content = None
if json_path:
with open(json_path, "r") as file:
try:
Expand All @@ -88,7 +88,7 @@ def cmd_patch(
except json.decoder.JSONDecodeError as E:
json_decode_error_handler(fields, E)

if json_content == {}:
if json_content is None:
raise ValueError(
"At least one of 'fields' or 'json_path' must be provided."
)
Expand Down
18 changes: 8 additions & 10 deletions geonoderest/maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def cmd_create(
title: Path,
fields: Optional[str] = None,
json_path: Optional[str] = None,
maplayers: Optional[List[int]] = [],
**kwargs,
):
"""
Expand All @@ -41,25 +42,22 @@ def cmd_create(
Raises:
Json.decoder.JSONDecodeError: when decoding is not working
"""
json_content: Dict = {}
json_content = None
if json_path:
with open(json_path, "r") as file:
try:
j_dict = json.load(file)
json_content = json.load(file)
except json.decoder.JSONDecodeError as E:
json_decode_error_handler(str(file), E)

if "attribute_set" in j_dict:
j_dict.pop("attribute_set", None)
json_content = {**json_content, **j_dict}
if fields:
elif fields:
try:
f_dict = json.loads(fields)
json_content = {**json_content, **f_dict}
json_content = json.loads(fields)
except json.decoder.JSONDecodeError as E:
json_decode_error_handler(fields, E)

obj = self.create(title=title, extra_json_content=json_content, **kwargs)
obj = self.create(
title=title, json_content=json_content, maplayers=maplayers, **kwargs
)
print_json(obj)

def create(
Expand Down
1 change: 0 additions & 1 deletion geonoderest/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@


class GeonodeRest(object):

DEFAULTS = {"page_size": 100, "page": 1}

def __init__(self, env: GeonodeApiConf):
Expand Down
Loading

0 comments on commit e0f0540

Please sign in to comment.