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 9a47259 commit 9210145
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
2 changes: 1 addition & 1 deletion geonodectl
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ To use this tool you have to set the following environment variables before star
# USERS ARGUMENT PARSING #
##########################
users = subparsers.add_parser(
"users", help="user|users commands", aliases=("user")
"users", help="user | users commands", aliases=("user",)
)
users_subparsers = users.add_subparsers(
help="geonodectl users commands", dest="subcommand", required=True
Expand Down
53 changes: 44 additions & 9 deletions geonoderest/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ def cmd_describe(
Raises:
SystemExit: if user_resource and user_groups are true at the same time this function gets confused and exits
"""
if user_resources and user_groups:
raise SystemExit(
"user_resource and user_group cannot be active at the same time ..."
)

obj = self.get(
pk, user_resources=user_resources, user_groups=user_groups, **kwargs
Expand Down Expand Up @@ -66,32 +62,33 @@ def get(
Returns:
Dict: requested info, details of user or list of accessable resources or groups of user are returned
"""
breakpoint()
if user_resources and user_groups:
raise AttributeError(
"cannot handle user_resources and user_groups True at the same time ..."
)
r: Dict
if user_groups is True:
r = self.http_get(
endpoint=f"{self.ENDPOINT_NAME}/{pk}/groups?page_size={kwargs['page_size']}"
endpoint=f"{self.ENDPOINT_NAME}/{pk}/groups?page_size={kwargs['page_size']}&page={kwargs['page']}"
)
return r
elif user_resources is True:
r = self.http_get(
endpoint=f"{self.ENDPOINT_NAME}/{pk}/resources?page_size={kwargs['page_size']}"
endpoint=f"{self.ENDPOINT_NAME}/{pk}/resources?page_size={kwargs['page_size']}&page={kwargs['page']}"
)
return r
else:
r = self.http_get(
endpoint=f"{self.ENDPOINT_NAME}/{pk}?page_size={kwargs['page_size']}"
endpoint=f"{self.ENDPOINT_NAME}/{pk}?page_size={kwargs['page_size']}&page={kwargs['page']}"
)
return r[self.SINGULAR_RESOURCE_NAME]

def cmd_create(
self,
username: str,
password: Optional[str],
email: str = "",
email: str,
first_name: str = "",
last_name: str = "",
**kwargs,
Expand All @@ -107,5 +104,43 @@ def cmd_create(
last_name (str): last name of the new user
"""

obj = self.create(title=title, extra_json_content=json_content, **kwargs)
obj = self.create(username=username,
password=password,
email=email,
first_name=first_name,
last_name=last_name,
**kwargs)
print_json(obj)

def create(
self,
username: str,
password: Optional[str],
email: str,
first_name: str = "",
last_name: str = "",
**kwargs,
) -> Dict:
"""
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
"""
json_content = {
"username": username,
"email": email,
"first_name": first_name,
"last_name": last_name,
}
if password:
json_content["password"] = password

return self.http_post(
endpoint=self.ENDPOINT_NAME,
params=json_content,
)

0 comments on commit 9210145

Please sign in to comment.