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
mwallschlaeger committed Apr 29, 2024
1 parent 2b1402a commit bd02b13
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
40 changes: 20 additions & 20 deletions geonodectl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ from geonoderest.resources import (
)
from geonoderest.documents import GeonodeDocumentsHandler
from geonoderest.maps import GeonodeMapsHandler
from geonoderest.people import GeonodePeopleHandler
from geonoderest.users import GeonodeUsersHandler
from geonoderest.geoapps import GeonodeGeoappsHandler
from geonoderest.uploads import GeonodeUploadsHandler
from geonoderest.executionrequest import GeonodeExecutionRequestHandler
Expand Down Expand Up @@ -411,41 +411,41 @@ To use this tool you have to set the following environment variables before star
##########################
# USERS ARGUMENT PARSING #
##########################
people = subparsers.add_parser(
"people", help="people|users commands", aliases=("users", "user")
users = subparsers.add_parser(
"users", help="user|users commands", aliases=("user")
)
people_subparsers = people.add_subparsers(
help="geonodectl people commands", dest="subcommand", required=True
users_subparsers = users.add_subparsers(
help="geonodectl users commands", dest="subcommand", required=True
)

# PATCH
people_patch = people_subparsers.add_parser("patch", help="patch users metadata")
people_patch.add_argument(type=int, dest="pk", help="pk of user to patch")
people_patch.add_argument(
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(
"--set",
dest="fields",
type=str,
help='patch metadata by providing a json string like: \'{"category":"{"identifier": "farming"}}\'',
)

# DESCRIBE
people_describe = people_subparsers.add_parser(
"describe", help="get people details"
users_describe = users_subparsers.add_parser(
"describe", help="get users details"
)
people_describe.add_argument(
type=int, dest="pk", help="pk of people to describe ..."
users_describe.add_argument(
type=int, dest="pk", help="pk of users to describe ..."
)
people_describe_subgroup = people_describe.add_mutually_exclusive_group(
users_describe_subgroup = users_describe.add_mutually_exclusive_group(
required=False
)
people_describe_subgroup.add_argument(
users_describe_subgroup.add_argument(
"--groups",
dest="user_groups",
required=False,
action="store_true",
help="show groups of user with given -pk ...",
)
people_describe_subgroup.add_argument(
users_describe_subgroup.add_argument(
"--resources",
dest="user_resources",
required=False,
Expand All @@ -454,11 +454,11 @@ To use this tool you have to set the following environment variables before star
)

# LIST
people_subparsers.add_parser("list", help="list documents")
users_subparsers.add_parser("list", help="list documents")

# DELETE
people_delete = people_subparsers.add_parser("delete", help="delete existing user")
people_delete.add_argument(type=int, dest="pk", help="pk of geoapp to 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 ...")

###########################
# UPLOAD ARGUMENT PARSING #
Expand Down Expand Up @@ -522,8 +522,8 @@ To use this tool you have to set the following environment variables before star
g_obj = GeonodeDocumentsHandler(env=geonode_env)
case "maps":
g_obj = GeonodeMapsHandler(env=geonode_env)
case "people" | "users" | "user":
g_obj = GeonodePeopleHandler(env=geonode_env)
case "users" | "user":
g_obj = GeonodeUsersHandler(env=geonode_env)
case "geoapps" | "apps":
g_obj = GeonodeGeoappsHandler(env=geonode_env)
case "uploads":
Expand Down
30 changes: 28 additions & 2 deletions geonoderest/people.py → geonoderest/users.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import Dict
from typing import Dict, Optional

from geonoderest.geonodeobject import GeonodeObjectHandler
from geonoderest.geonodetypes import GeonodeCmdOutListKey
from geonoderest.cmdprint import print_list_on_cmd, print_json


class GeonodePeopleHandler(GeonodeObjectHandler):
class GeonodeUsersHandler(GeonodeObjectHandler):
ENDPOINT_NAME = JSON_OBJECT_NAME = "users"
SINGULAR_RESOURCE_NAME = "user"

Expand All @@ -14,6 +14,9 @@ class GeonodePeopleHandler(GeonodeObjectHandler):
GeonodeCmdOutListKey(key="username"),
GeonodeCmdOutListKey(key="first_name"),
GeonodeCmdOutListKey(key="last_name"),
GeonodeCmdOutListKey(key="email"),
GeonodeCmdOutListKey(key="is_staff"),
GeonodeCmdOutListKey(key="is_superuser"),
]

def cmd_describe(
Expand Down Expand Up @@ -83,3 +86,26 @@ def get(
endpoint=f"{self.ENDPOINT_NAME}/{pk}?page_size={kwargs['page_size']}"
)
return r[self.SINGULAR_RESOURCE_NAME]

def cmd_create(
self,
username: str,
password: Optional[str],
email: str = "",
first_name: str = "",
last_name: str = "",
**kwargs,
):
"""
creates an user with the given characteristics
Args:
username (str): username of the new user
password (Optional[str]): password of the new user
email (str): email of the new user
first_name (str): first name of the new user
last_name (str): last name of the new user
"""

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

0 comments on commit bd02b13

Please sign in to comment.