Skip to content

Commit 604e3f1

Browse files
Create users with CLI arguments (piccolo-orm#231)
* added CLI arguments to 'user create' command * added unit test for creating users with cli arguments * fixed lint issues * update `piccolo user create` docs * ignore mypy error Co-authored-by: Daniel Townsend <[email protected]>
1 parent ccdabaa commit 604e3f1

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

docs/src/piccolo/authentication/baseuser.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,23 @@ The app comes with some useful commands.
2626
user create
2727
~~~~~~~~~~~
2828

29-
Create a new user.
29+
Creates a new user. It presents an interactive prompt, asking for the username,
30+
password etc.
3031

3132
.. code-block:: bash
3233
3334
piccolo user create
3435
36+
If you'd prefer to create a user without the interactive prompt (perhaps in a
37+
script), you can pass all of the arguments in as follows:
38+
39+
.. code-block:: bash
40+
41+
piccolo user create --username=bob --password=bob123 [email protected] --is_admin=t --is_superuser=t --is_active=t
42+
43+
If you choose this approach then be careful, as the password will be in the
44+
shell's history.
45+
3546
user change_password
3647
~~~~~~~~~~~~~~~~~~~~
3748

piccolo/apps/user/commands/create.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
import typing as t
23
from getpass import getpass, getuser
34

45
from piccolo.apps.user.tables import BaseUser
@@ -55,24 +56,32 @@ def get_is_active() -> bool:
5556
return active == "y"
5657

5758

58-
def create():
59+
def create(
60+
username: t.Optional[str] = None,
61+
email: t.Optional[str] = None,
62+
password: t.Optional[str] = None,
63+
is_admin: t.Optional[bool] = None,
64+
is_superuser: t.Optional[bool] = None,
65+
is_active: t.Optional[bool] = None,
66+
):
5967
"""
6068
Create a new user.
6169
"""
62-
username = get_username()
63-
email = get_email()
64-
password = get_password()
65-
confirmed_password = get_confirmed_password()
70+
username = get_username() if username is None else username
71+
email = get_email() if email is None else email
72+
if password is None:
73+
password = get_password()
74+
confirmed_password = get_confirmed_password()
6675

67-
if not password == confirmed_password:
68-
sys.exit("Passwords don't match!")
76+
if not password == confirmed_password:
77+
sys.exit("Passwords don't match!")
6978

7079
if len(password) < 4:
7180
sys.exit("The password is too short")
7281

73-
is_admin = get_is_admin()
74-
is_superuser = get_is_superuser()
75-
is_active = get_is_active()
82+
is_admin = get_is_admin() if is_admin is None else is_admin
83+
is_superuser = get_is_superuser() if is_superuser is None else is_superuser
84+
is_active = get_is_active() if is_active is None else is_active
7685

7786
user = BaseUser(
7887
username=username,
@@ -84,4 +93,4 @@ def create():
8493
)
8594
user.save().run_sync()
8695

87-
print(f"Created User {user.id}")
96+
print(f"Created User {user.id}") # type: ignore

tests/apps/user/commands/test_create.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,26 @@ def test_create(self, *args, **kwargs):
5454
)
5555
.run_sync()
5656
)
57+
58+
def test_create_with_arguments(self, *args, **kwargs):
59+
arguments = {
60+
"username": "bob123",
61+
"email": "[email protected]",
62+
"password": "password123",
63+
"is_admin": True,
64+
"is_superuser": True,
65+
"is_active": True,
66+
}
67+
create(**arguments)
68+
69+
self.assertTrue(
70+
BaseUser.exists()
71+
.where(
72+
(BaseUser.admin == True) # noqa: E712
73+
& (BaseUser.username == "bob123")
74+
& (BaseUser.email == "[email protected]")
75+
& (BaseUser.superuser == True)
76+
& (BaseUser.active == True)
77+
)
78+
.run_sync()
79+
)

0 commit comments

Comments
 (0)