-
Notifications
You must be signed in to change notification settings - Fork 5
/
cli.py
123 lines (101 loc) · 3.72 KB
/
cli.py
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import asyncio
import typing
from collections.abc import Callable
from functools import wraps
import typer
from apps.authorization.managers import AuthorizationManager
from apps.CORE.db import async_session_factory, engine
from loggers import get_logger, setup_logging
setup_logging() # enable logging inside CLI
logger = get_logger(name=__name__)
auth_manager = AuthorizationManager(engine=engine)
Function = typing.TypeVar("Function", bound=Callable[..., typing.Any])
def make_async(func: Function) -> Function: # type: ignore
"""Decorator to user Typer commands with asyncio."""
@wraps(func)
def wrapper(*args, **kwargs): # type: ignore # noqa: ANN202
"""Run function with asyncio."""
return asyncio.run(func(*args, **kwargs))
return wrapper
app = typer.Typer(
name="FastAPI Quickstart CLI",
help="Command Line Interface for FastAPI application.",
)
authorization_commands = typer.Typer(name="auth", help="Commands for 'Authorization' app.")
permissions_commands = typer.Typer(name="permissions", help="Manage Permissions objects in db.")
superusers_commands = typer.Typer(name="superusers", help="Manage superusers.")
app.add_typer(typer_instance=authorization_commands)
authorization_commands.add_typer(typer_instance=permissions_commands)
authorization_commands.add_typer(typer_instance=superusers_commands)
# app
# --> auth
# --|--> permissions
# --|--|--> setup (`poetry run python cli.py auth permissions setup`)
# --|--> superusers
# --|--|--> setup (`poetry run python cli.py auth superusers setup`)
# --|--|--> create (`poetry run python cli.py auth superusers create`)
@permissions_commands.command(name="setup", help="Creates Permissions for all models.")
@make_async
async def setup_permissions() -> None:
"""Scan all tables and creates Permissions for them (CRUD action for every table)."""
async with async_session_factory() as session:
await auth_manager.create_object_permissions(session=session)
@superusers_commands.command(
name="setup",
help="Creates superusers Permissions, 'Superusers' Group, 'Superuser' Role.",
)
@make_async
async def setup_superusers() -> None:
"""Creates standard Groups/Roles/Permissions.
Note:
Group: `Superusers`
Role: `Superuser`
Permission: object_name="__all__" with actions="create|read|update|delete"
"""
async with async_session_factory() as session:
await auth_manager.setup_superusers(session=session)
@superusers_commands.command(
name="create",
help="Create User and assign 'Superusers' Group, 'Superuser' Role and superusers Permissions.",
)
@make_async
async def create_superuser(
first_name: str = typer.Option(
"Kostiantyn",
"--first_name",
"-f",
prompt="Enter First name",
show_default=True,
help="First name of user.",
),
last_name: str = typer.Option(
"Salnykov",
"--last_name",
"-l",
prompt="Enter Last name",
show_default=True,
help="Last name of user.",
),
email: str = typer.Option(
"kostiantyn.salnykov@gmail.com",
"--email",
"-e",
prompt="Enter Email",
show_default=True,
help="Email of user.",
),
password: str = typer.Option(
...,
"--password",
"-p",
prompt="Enter Password",
confirmation_prompt=True,
hide_input=True,
help="Password for user.",
),
) -> None:
"""Create superuser with provided inputs."""
logger.debug(msg=f"Superuser created successfully ({first_name=}, {last_name=}, {email=}, {password=}).")
... # TODO: Create User, add to `Superusers` Group, add `Superuser` Role, assign Permissions.
if __name__ == "__main__":
app()