Skip to content

Commit b57961f

Browse files
Merge pull request #44 from GeoNodeUserGroup-DE/issue_#20_add_user_create_function
[Fixes #20] add user create function
2 parents 44997ee + e0ca65b commit b57961f

File tree

7 files changed

+394
-144
lines changed

7 files changed

+394
-144
lines changed

geonodectl

+133-35
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ from geonoderest.resources import (
1818
)
1919
from geonoderest.documents import GeonodeDocumentsHandler
2020
from geonoderest.maps import GeonodeMapsHandler
21-
from geonoderest.people import GeonodePeopleHandler
21+
from geonoderest.users import GeonodeUsersHandler
2222
from geonoderest.geoapps import GeonodeGeoappsHandler
2323
from geonoderest.uploads import GeonodeUploadsHandler
2424
from geonoderest.executionrequest import GeonodeExecutionRequestHandler
@@ -224,14 +224,16 @@ To use this tool you have to set the following environment variables before star
224224
"patch", help="patch datasets metadata"
225225
)
226226
datasets_patch.add_argument(type=int, dest="pk", help="pk of dataset to patch")
227-
datasets_patch.add_argument(
227+
datasets_patch_mutually_exclusive_group = datasets_patch.add_mutually_exclusive_group()
228+
229+
datasets_patch_mutually_exclusive_group.add_argument(
228230
"--set",
229231
dest="fields",
230232
type=str,
231233
help='patch metadata by providing a json string like: \'{"category":"{"identifier": "farming"}}\'',
232234
)
233235

234-
datasets_patch.add_argument(
236+
datasets_patch_mutually_exclusive_group.add_argument(
235237
"--json_path",
236238
dest="json_path",
237239
type=str,
@@ -289,16 +291,22 @@ To use this tool you have to set the following environment variables before star
289291
)
290292

291293
# PATCH
292-
documents_patch = documents_subparsers.add_parser(
293-
"patch", help="patch documents metadata"
294-
)
295-
documents_patch.add_argument(type=int, dest="pk", help="pk of document to patch")
296-
documents_patch.add_argument(
294+
documents_patch = documents_subparsers.add_parser("patch", help="patch documents metadata")
295+
documents_patch.add_argument(type=int, dest="pk", help="pk of documents to patch")
296+
documents_patch_mutually_exclusive_group = documents_patch.add_mutually_exclusive_group()
297+
298+
documents_patch_mutually_exclusive_group.add_argument(
297299
"--set",
298300
dest="fields",
299301
type=str,
300302
help='patch metadata by providing a json string like: \'{"category":"{"identifier": "farming"}}\'',
301303
)
304+
documents_patch_mutually_exclusive_group.add_argument(
305+
"--json_path",
306+
dest="json_path",
307+
type=str,
308+
help="add metadata by providing a path to a json file",
309+
)
302310

303311
# DESCRIBE
304312
documents_describe = documents_subparsers.add_parser(
@@ -329,12 +337,20 @@ To use this tool you have to set the following environment variables before star
329337
# PATCH
330338
maps_patch = maps_subparsers.add_parser("patch", help="patch maps metadata")
331339
maps_patch.add_argument(type=int, dest="pk", help="pk of map to patch")
332-
maps_patch.add_argument(
340+
maps_patch_mutually_exclusive_group = maps.add_mutually_exclusive_group()
341+
342+
maps_patch_mutually_exclusive_group.add_argument(
333343
"--set",
334344
dest="fields",
335345
type=str,
336346
help='patch metadata by providing a json string like: \'{"category":"{"identifier": "farming"}}\'',
337347
)
348+
maps_patch_mutually_exclusive_group.add_argument(
349+
"--json_path",
350+
dest="json_path",
351+
type=str,
352+
help="add metadata by providing a path to a json file",
353+
)
338354

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

350-
maps_create.add_argument(
366+
maps_create_mutually_exclusive_group = maps_create.add_mutually_exclusive_group()
367+
maps_create_mutually_exclusive_group.add_argument(
351368
"--title",
352369
type=str,
353-
required=True,
354370
dest="title",
355371
help="title of the new dataset ...",
356372
)
357-
358-
maps_create.add_argument(
373+
maps_create_mutually_exclusive_group.add_argument(
359374
"--set",
360375
dest="fields",
361376
type=str,
362377
help='add metadata by providing a json string like: \'\'{ "category": {"identifier": "farming"}, "abstract": "test abstract" }\'\'',
363378
)
364379

365-
maps_create.add_argument(
380+
maps_create_mutually_exclusive_group.add_argument(
366381
"--json_path",
367382
dest="json_path",
368383
type=str,
@@ -395,13 +410,22 @@ To use this tool you have to set the following environment variables before star
395410
"patch", help="patch geoapps metadata"
396411
)
397412
geoapps_patch.add_argument(type=int, dest="pk", help="pk of geoapp to patch")
398-
geoapps_patch.add_argument(
413+
414+
geoapps_patch_mutually_exclusive_group = geoapps_patch.add_mutually_exclusive_group()
415+
geoapps_patch_mutually_exclusive_group.add_argument(
399416
"--set",
400417
dest="fields",
401418
type=str,
402419
help='patch metadata by providing a json string like: \'{"category":"{"identifier": "farming"}}\'',
403420
)
404421

422+
geoapps_patch_mutually_exclusive_group.add_argument(
423+
"--json_path",
424+
dest="json_path",
425+
type=str,
426+
help="patch metadata (user credentials) by providing a path to a json file, like --set written in file ...",
427+
)
428+
405429
# DESCRIBE
406430
geoapps_describe = geoapps_subparsers.add_parser(
407431
"describe", help="get geoapp details"
@@ -419,41 +443,50 @@ To use this tool you have to set the following environment variables before star
419443
##########################
420444
# USERS ARGUMENT PARSING #
421445
##########################
422-
people = subparsers.add_parser(
423-
"people", help="people|users commands", aliases=("users", "user")
446+
users = subparsers.add_parser(
447+
"users", help="user | users commands", aliases=("user",)
424448
)
425-
people_subparsers = people.add_subparsers(
426-
help="geonodectl people commands", dest="subcommand", required=True
449+
users_subparsers = users.add_subparsers(
450+
help="geonodectl users commands", dest="subcommand", required=True
427451
)
428452

429453
# PATCH
430-
people_patch = people_subparsers.add_parser("patch", help="patch users metadata")
431-
people_patch.add_argument(type=int, dest="pk", help="pk of user to patch")
432-
people_patch.add_argument(
454+
users_patch = users_subparsers.add_parser("patch", help="patch users metadata")
455+
users_patch.add_argument(type=int, dest="pk", help="pk of user to patch")
456+
457+
user_patch_mutually_exclusive_group = users_patch.add_mutually_exclusive_group()
458+
user_patch_mutually_exclusive_group.add_argument(
433459
"--set",
434460
dest="fields",
435461
type=str,
436462
help='patch metadata by providing a json string like: \'{"category":"{"identifier": "farming"}}\'',
437463
)
438464

465+
user_patch_mutually_exclusive_group.add_argument(
466+
"--json_path",
467+
dest="json_path",
468+
type=str,
469+
help="patch metadata (user credentials) by providing a path to a json file, like --set written in file ...",
470+
)
471+
439472
# DESCRIBE
440-
people_describe = people_subparsers.add_parser(
441-
"describe", help="get people details"
473+
users_describe = users_subparsers.add_parser(
474+
"describe", help="get users details"
442475
)
443-
people_describe.add_argument(
444-
type=int, dest="pk", help="pk of people to describe ..."
476+
users_describe.add_argument(
477+
type=int, dest="pk", help="pk of users to describe ..."
445478
)
446-
people_describe_subgroup = people_describe.add_mutually_exclusive_group(
479+
users_describe_subgroup = users_describe.add_mutually_exclusive_group(
447480
required=False
448481
)
449-
people_describe_subgroup.add_argument(
482+
users_describe_subgroup.add_argument(
450483
"--groups",
451484
dest="user_groups",
452485
required=False,
453486
action="store_true",
454487
help="show groups of user with given -pk ...",
455488
)
456-
people_describe_subgroup.add_argument(
489+
users_describe_subgroup.add_argument(
457490
"--resources",
458491
dest="user_resources",
459492
required=False,
@@ -462,11 +495,77 @@ To use this tool you have to set the following environment variables before star
462495
)
463496

464497
# LIST
465-
people_subparsers.add_parser("list", help="list documents")
498+
users_subparsers.add_parser("list", help="list documents")
466499

467500
# DELETE
468-
people_delete = people_subparsers.add_parser("delete", help="delete existing user")
469-
people_delete.add_argument(type=int, dest="pk", help="pk of geoapp to delete ...")
501+
users_delete = users_subparsers.add_parser("delete", help="delete existing user")
502+
users_delete.add_argument(type=int, dest="pk", help="pk of geoapp to delete ...")
503+
504+
# CREATE
505+
users_create = users_subparsers.add_parser("create", help="create a new user")
506+
user_create_mutually_exclusive_group = users_create.add_mutually_exclusive_group()
507+
user_create_mutually_exclusive_group.add_argument(
508+
"--username",
509+
type=str,
510+
dest="username",
511+
help="username of the new user ... (mutually exclusive [a])",
512+
)
513+
514+
users_create.add_argument(
515+
"--email",
516+
type=str,
517+
required=False,
518+
dest="email",
519+
help="email of the new user ... (only working combined with --username) ...",
520+
)
521+
522+
users_create.add_argument(
523+
"--first_name",
524+
type=str,
525+
required=False,
526+
dest="first_name",
527+
help="first_name of the new user (only working combined with --username) ...",
528+
)
529+
530+
users_create.add_argument(
531+
"--last_name",
532+
type=str,
533+
required=False,
534+
dest="last_name",
535+
help="last_name of the new user (only working combined with --username) ...",
536+
)
537+
538+
users_create.add_argument(
539+
"--is_superuser",
540+
action='store_true',
541+
required=False,
542+
dest="is_superuser",
543+
default=False,
544+
help="set to make the new user a superuser (only working combined with --username) ...",
545+
)
546+
547+
users_create.add_argument(
548+
"--is_staff",
549+
action='store_true',
550+
required=False,
551+
dest="is_staff",
552+
default=False,
553+
help="set to make the new user a staff user (only working combined with --username) ...",
554+
)
555+
556+
user_create_mutually_exclusive_group.add_argument(
557+
"--json_path",
558+
dest="json_path",
559+
type=str,
560+
help="add metadata (user credentials) by providing a path to a json file, like --set written in file ...(mutually exclusive [b])",
561+
)
562+
563+
user_create_mutually_exclusive_group.add_argument(
564+
"--set",
565+
dest="fields",
566+
type=str,
567+
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])',
568+
)
470569

471570
###########################
472571
# UPLOAD ARGUMENT PARSING #
@@ -530,8 +629,8 @@ To use this tool you have to set the following environment variables before star
530629
g_obj = GeonodeDocumentsHandler(env=geonode_env)
531630
case "maps":
532631
g_obj = GeonodeMapsHandler(env=geonode_env)
533-
case "people" | "users" | "user":
534-
g_obj = GeonodePeopleHandler(env=geonode_env)
632+
case "users" | "user":
633+
g_obj = GeonodeUsersHandler(env=geonode_env)
535634
case "geoapps" | "apps":
536635
g_obj = GeonodeGeoappsHandler(env=geonode_env)
537636
case "uploads":
@@ -540,7 +639,6 @@ To use this tool you have to set the following environment variables before star
540639
g_obj = GeonodeExecutionRequestHandler(env=geonode_env)
541640
case _:
542641
raise NotImplemented
543-
544642
g_obj_func = getattr(g_obj, "cmd_" + args.subcommand)
545643
g_obj_func(**args.__dict__)
546644

geonoderest/apiconf.py

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ def from_env_vars() -> "GeonodeApiConf":
3939
"GEONODE_API_URL" not in os.environ
4040
or "GEONODE_API_BASIC_AUTH" not in os.environ
4141
):
42-
4342
raise SystemExit(
4443
"env vars not set: GEONODE_API_URL, GEONODE_API_BASIC_AUTH"
4544
)

geonoderest/geonodeobject.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -69,26 +69,23 @@ def cmd_patch(
6969
ValueError: catches json.decoder.JSONDecodeError and raises ValueError as decoding is not working
7070
"""
7171

72-
json_content: Dict = {}
7372
if json_path:
7473
with open(json_path, "r") as file:
7574
try:
76-
j_dict = json.load(file)
75+
json_content = json.load(file)
7776
except json.decoder.JSONDecodeError as E:
7877
json_decode_error_handler(str(file), E)
7978

80-
if "attribute_set" in j_dict:
81-
j_dict.pop("attribute_set", None)
82-
json_content = {**json_content, **j_dict}
79+
if json_content is not None and "attribute_set" in json_content:
80+
json_content.pop("attribute_set", None)
8381

84-
if fields:
82+
elif fields:
8583
try:
86-
f_dict = json.loads(fields)
87-
json_content = {**json_content, **f_dict}
84+
json_content = json.loads(fields)
8885
except json.decoder.JSONDecodeError as E:
8986
json_decode_error_handler(fields, E)
9087

91-
if json_content == {}:
88+
else:
9289
raise ValueError(
9390
"At least one of 'fields' or 'json_path' must be provided."
9491
)

geonoderest/maps.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def cmd_create(
2727
title: Path,
2828
fields: Optional[str] = None,
2929
json_path: Optional[str] = None,
30+
maplayers: Optional[List[int]] = [],
3031
**kwargs,
3132
):
3233
"""
@@ -41,25 +42,22 @@ def cmd_create(
4142
Raises:
4243
Json.decoder.JSONDecodeError: when decoding is not working
4344
"""
44-
json_content: Dict = {}
45+
json_content = None
4546
if json_path:
4647
with open(json_path, "r") as file:
4748
try:
48-
j_dict = json.load(file)
49+
json_content = json.load(file)
4950
except json.decoder.JSONDecodeError as E:
5051
json_decode_error_handler(str(file), E)
51-
52-
if "attribute_set" in j_dict:
53-
j_dict.pop("attribute_set", None)
54-
json_content = {**json_content, **j_dict}
55-
if fields:
52+
elif fields:
5653
try:
57-
f_dict = json.loads(fields)
58-
json_content = {**json_content, **f_dict}
54+
json_content = json.loads(fields)
5955
except json.decoder.JSONDecodeError as E:
6056
json_decode_error_handler(fields, E)
6157

62-
obj = self.create(title=title, extra_json_content=json_content, **kwargs)
58+
obj = self.create(
59+
title=title, json_content=json_content, maplayers=maplayers, **kwargs
60+
)
6361
print_json(obj)
6462

6563
def create(

0 commit comments

Comments
 (0)