-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_user.py
More file actions
45 lines (35 loc) · 1.1 KB
/
create_user.py
File metadata and controls
45 lines (35 loc) · 1.1 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
44
45
import asyncio
import typer
from app.main.db import async_session
from app.user.constants import UserRole
from app.user.schemas import NewUser
from app.user.services.users import UsersDbService
app = typer.Typer()
@app.command()
def create_user() -> None:
"""Create a new user."""
first_name = typer.prompt("First name")
last_name = typer.prompt("Last name")
email = typer.prompt("Email")
username = typer.prompt("Username")
role = typer.prompt("Role", type=UserRole, default=UserRole.user)
password = typer.prompt("Password", hide_input=True, confirmation_prompt=True)
user = NewUser(
first_name=first_name,
last_name=last_name,
email=email,
username=username,
password=password,
repeat_password=password,
)
asyncio.run(save_user(user, role))
async def save_user(user: NewUser, role: UserRole) -> None:
"""Save info about user into the database.
:param user: info about user to be saved
:return: None
"""
async with async_session() as session:
service = UsersDbService(session)
await service.create(user, role=role.name)
if __name__ == "__main__":
app()