forked from mr-eggleton/python-flask-todo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_superuser.py
More file actions
43 lines (34 loc) · 1.17 KB
/
create_superuser.py
File metadata and controls
43 lines (34 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""
Run this script to create an admin superuser for the Piccolo admin panel.
Usage: python3 create_superuser.py
"""
import asyncio
import getpass
from piccolo.apps.user.tables import BaseUser
from piccolo_api.session_auth.tables import SessionsBase
async def main():
await BaseUser.create_table(if_not_exists=True)
await SessionsBase.create_table(if_not_exists=True)
username = input("Username: ")
password = getpass.getpass("Password: ")
existing_user = await BaseUser.objects().where(BaseUser.username == username).first()
if existing_user:
await BaseUser.update_password(user=existing_user.id, password=password)
await BaseUser.update(
{
BaseUser.admin: True,
BaseUser.superuser: True,
BaseUser.active: True,
}
).where(BaseUser.id == existing_user.id)
print(f"Superuser '{username}' updated successfully.")
return
await BaseUser.create_user(
username=username,
password=password,
admin=True,
superuser=True,
active=True,
)
print(f"Superuser '{username}' created successfully.")
asyncio.run(main())