| 1 |
a47eb4c |
Dawsh |
2y ago |
from typing import List, Optional |
| 2 |
a47eb4c |
Dawsh |
2y ago |
|
| 3 |
a47eb4c |
Dawsh |
2y ago |
import typer |
| 4 |
a47eb4c |
Dawsh |
2y ago |
from rich.table import Table |
| 5 |
a47eb4c |
Dawsh |
2y ago |
|
| 6 |
a47eb4c |
Dawsh |
2y ago |
from app.db import GetDB, crud |
| 7 |
a47eb4c |
Dawsh |
2y ago |
from app.db.models import User |
| 8 |
a47eb4c |
Dawsh |
2y ago |
from app.utils.system import readable_size |
| 9 |
a47eb4c |
Dawsh |
2y ago |
|
| 10 |
a47eb4c |
Dawsh |
2y ago |
from . import utils |
| 11 |
a47eb4c |
Dawsh |
2y ago |
|
| 12 |
a47eb4c |
Dawsh |
2y ago |
app = typer.Typer(no_args_is_help=True) |
| 13 |
a47eb4c |
Dawsh |
2y ago |
|
| 14 |
a47eb4c |
Dawsh |
2y ago |
|
| 15 |
a47eb4c |
Dawsh |
2y ago |
@app.command(name="list") |
| 16 |
a47eb4c |
Dawsh |
2y ago |
def list_users( |
| 17 |
a47eb4c |
Dawsh |
2y ago |
offset: Optional[int] = typer.Option(None, *utils.FLAGS["offset"]), |
| 18 |
a47eb4c |
Dawsh |
2y ago |
limit: Optional[int] = typer.Option(None, *utils.FLAGS["limit"]), |
| 19 |
01cdf2e |
Dawsh |
2y ago |
username: Optional[List[str]] = typer.Option( |
| 20 |
01cdf2e |
Dawsh |
2y ago |
None, *utils.FLAGS["username"], help="Search by username(s)" |
| 21 |
01cdf2e |
Dawsh |
2y ago |
), |
| 22 |
a47eb4c |
Dawsh |
2y ago |
status: Optional[crud.UserStatus] = typer.Option(None, *utils.FLAGS["status"]), |
| 23 |
01cdf2e |
Dawsh |
2y ago |
admin: Optional[str] = typer.Option( |
| 24 |
01cdf2e |
Dawsh |
2y ago |
None, "--admin", "--owner", help="Search by owner admin's username" |
| 25 |
01cdf2e |
Dawsh |
2y ago |
), |
| 26 |
a47eb4c |
Dawsh |
2y ago |
): |
| 27 |
a47eb4c |
Dawsh |
2y ago |
""" |
| 28 |
a47eb4c |
Dawsh |
2y ago |
Displays a table of users |
| 29 |
a47eb4c |
Dawsh |
2y ago |
|
| 30 |
a47eb4c |
Dawsh |
2y ago |
NOTE: Sorting is not currently available. |
| 31 |
a47eb4c |
Dawsh |
2y ago |
""" |
| 32 |
a47eb4c |
Dawsh |
2y ago |
with GetDB() as db: |
| 33 |
a47eb4c |
Dawsh |
2y ago |
users: list[User] = crud.get_users( |
| 34 |
01cdf2e |
Dawsh |
2y ago |
db=db, |
| 35 |
01cdf2e |
Dawsh |
2y ago |
offset=offset, |
| 36 |
01cdf2e |
Dawsh |
2y ago |
limit=limit, |
| 37 |
01cdf2e |
Dawsh |
2y ago |
usernames=username, |
| 38 |
01cdf2e |
Dawsh |
2y ago |
status=status, |
| 39 |
01cdf2e |
Dawsh |
2y ago |
admin=crud.get_admin(db, admin) if admin else None, |
| 40 |
a47eb4c |
Dawsh |
2y ago |
) |
| 41 |
a47eb4c |
Dawsh |
2y ago |
|
| 42 |
a47eb4c |
Dawsh |
2y ago |
utils.print_table( |
| 43 |
a47eb4c |
Dawsh |
2y ago |
table=Table( |
| 44 |
01cdf2e |
Dawsh |
2y ago |
"ID", |
| 45 |
01cdf2e |
Dawsh |
2y ago |
"Username", |
| 46 |
01cdf2e |
Dawsh |
2y ago |
"Status", |
| 47 |
01cdf2e |
Dawsh |
2y ago |
"Used traffic", |
| 48 |
01cdf2e |
Dawsh |
2y ago |
"Data limit", |
| 49 |
01cdf2e |
Dawsh |
2y ago |
"Reset strategy", |
| 50 |
01cdf2e |
Dawsh |
2y ago |
"Expires at", |
| 51 |
01cdf2e |
Dawsh |
2y ago |
"Owner", |
| 52 |
a47eb4c |
Dawsh |
2y ago |
), |
| 53 |
a47eb4c |
Dawsh |
2y ago |
rows=[ |
| 54 |
a47eb4c |
Dawsh |
2y ago |
( |
| 55 |
a47eb4c |
Dawsh |
2y ago |
str(user.id), |
| 56 |
a47eb4c |
Dawsh |
2y ago |
user.username, |
| 57 |
a47eb4c |
Dawsh |
2y ago |
user.status.value, |
| 58 |
a47eb4c |
Dawsh |
2y ago |
readable_size(user.used_traffic), |
| 59 |
a47eb4c |
Dawsh |
2y ago |
readable_size(user.data_limit) if user.data_limit else "Unlimited", |
| 60 |
a47eb4c |
Dawsh |
2y ago |
user.data_limit_reset_strategy.value, |
| 61 |
a47eb4c |
Dawsh |
2y ago |
utils.readable_datetime(user.expire, include_time=False), |
| 62 |
01cdf2e |
Dawsh |
2y ago |
user.admin.username if user.admin else "", |
| 63 |
a47eb4c |
Dawsh |
2y ago |
) |
| 64 |
a47eb4c |
Dawsh |
2y ago |
for user in users |
| 65 |
01cdf2e |
Dawsh |
2y ago |
], |
| 66 |
a47eb4c |
Dawsh |
2y ago |
) |
| 67 |
a47eb4c |
Dawsh |
2y ago |
|
| 68 |
a47eb4c |
Dawsh |
2y ago |
|
| 69 |
a47eb4c |
Dawsh |
2y ago |
@app.command(name="set-owner") |
| 70 |
a47eb4c |
Dawsh |
2y ago |
def set_owner( |
| 71 |
a47eb4c |
Dawsh |
2y ago |
username: str = typer.Option(None, *utils.FLAGS["username"], prompt=True), |
| 72 |
01cdf2e |
Dawsh |
2y ago |
admin: str = typer.Option( |
| 73 |
01cdf2e |
Dawsh |
2y ago |
None, "--admin", "--owner", prompt=True, help="Admin's username" |
| 74 |
01cdf2e |
Dawsh |
2y ago |
), |
| 75 |
01cdf2e |
Dawsh |
2y ago |
yes_to_all: bool = typer.Option( |
| 76 |
01cdf2e |
Dawsh |
2y ago |
False, *utils.FLAGS["yes_to_all"], help="Skips confirmations" |
| 77 |
01cdf2e |
Dawsh |
2y ago |
), |
| 78 |
a47eb4c |
Dawsh |
2y ago |
): |
| 79 |
a47eb4c |
Dawsh |
2y ago |
""" |
| 80 |
a47eb4c |
Dawsh |
2y ago |
Transfers user's ownership |
| 81 |
a47eb4c |
Dawsh |
2y ago |
|
| 82 |
a47eb4c |
Dawsh |
2y ago |
NOTE: This command needs additional confirmation for users who already have an owner. |
| 83 |
a47eb4c |
Dawsh |
2y ago |
""" |
| 84 |
a47eb4c |
Dawsh |
2y ago |
with GetDB() as db: |
| 85 |
a47eb4c |
Dawsh |
2y ago |
user: User = utils.raise_if_falsy( |
| 86 |
01cdf2e |
Dawsh |
2y ago |
crud.get_user(db, username=username), f'User "{username}" not found.' |
| 87 |
01cdf2e |
Dawsh |
2y ago |
) |
| 88 |
a47eb4c |
Dawsh |
2y ago |
|
| 89 |
a47eb4c |
Dawsh |
2y ago |
dbadmin = utils.raise_if_falsy( |
| 90 |
01cdf2e |
Dawsh |
2y ago |
crud.get_admin(db, username=admin), f'Admin "{admin}" not found.' |
| 91 |
01cdf2e |
Dawsh |
2y ago |
) |
| 92 |
a47eb4c |
Dawsh |
2y ago |
|
| 93 |
a47eb4c |
Dawsh |
2y ago |
# Ask for confirmation if user already has an owner |
| 94 |
01cdf2e |
Dawsh |
2y ago |
if ( |
| 95 |
01cdf2e |
Dawsh |
2y ago |
user.admin |
| 96 |
01cdf2e |
Dawsh |
2y ago |
and not yes_to_all |
| 97 |
01cdf2e |
Dawsh |
2y ago |
and not typer.confirm( |
| 98 |
01cdf2e |
Dawsh |
2y ago |
f'{username}\'s current owner is "{user.admin.username}".' |
| 99 |
01cdf2e |
Dawsh |
2y ago |
f' Are you sure about transferring its ownership to "{admin}"?' |
| 100 |
01cdf2e |
Dawsh |
2y ago |
) |
| 101 |
a47eb4c |
Dawsh |
2y ago |
): |
| 102 |
a47eb4c |
Dawsh |
2y ago |
utils.error("Aborted.") |
| 103 |
a47eb4c |
Dawsh |
2y ago |
|
| 104 |
a47eb4c |
Dawsh |
2y ago |
crud.set_owner(db, user, dbadmin) |
| 105 |
a47eb4c |
Dawsh |
2y ago |
|
| 106 |
a47eb4c |
Dawsh |
2y ago |
utils.success(f'{username}\'s owner successfully set to "{admin}".') |