From c90f7878d79dbea33a695eb7241a4fce9e5abc23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Tue, 20 Dec 2022 13:14:49 +0100 Subject: [PATCH] fix usermanager mypy --- lnbits/extensions/usermanager/crud.py | 33 +++++------- lnbits/extensions/usermanager/models.py | 6 +-- lnbits/extensions/usermanager/views.py | 5 +- lnbits/extensions/usermanager/views_api.py | 60 +++++++++++----------- pyproject.toml | 1 - 5 files changed, 50 insertions(+), 55 deletions(-) diff --git a/lnbits/extensions/usermanager/crud.py b/lnbits/extensions/usermanager/crud.py index 649888a8..d7b64dbf 100644 --- a/lnbits/extensions/usermanager/crud.py +++ b/lnbits/extensions/usermanager/crud.py @@ -10,12 +10,10 @@ from lnbits.core.crud import ( from lnbits.core.models import Payment from . import db -from .models import CreateUserData, Users, Wallets - -### Users +from .models import CreateUserData, User, Wallet -async def create_usermanager_user(data: CreateUserData) -> Users: +async def create_usermanager_user(data: CreateUserData) -> User: account = await create_account() user = await get_user(account.id) assert user, "Newly created user couldn't be retrieved" @@ -50,17 +48,17 @@ async def create_usermanager_user(data: CreateUserData) -> Users: return user_created -async def get_usermanager_user(user_id: str) -> Optional[Users]: +async def get_usermanager_user(user_id: str) -> Optional[User]: row = await db.fetchone("SELECT * FROM usermanager.users WHERE id = ?", (user_id,)) - return Users(**row) if row else None + return User(**row) if row else None -async def get_usermanager_users(user_id: str) -> List[Users]: +async def get_usermanager_users(user_id: str) -> list[User]: rows = await db.fetchall( "SELECT * FROM usermanager.users WHERE admin = ?", (user_id,) ) - return [Users(**row) for row in rows] + return [User(**row) for row in rows] async def delete_usermanager_user(user_id: str, delete_core: bool = True) -> None: @@ -73,12 +71,9 @@ async def delete_usermanager_user(user_id: str, delete_core: bool = True) -> Non await db.execute("""DELETE FROM usermanager.wallets WHERE "user" = ?""", (user_id,)) -### Wallets - - async def create_usermanager_wallet( user_id: str, wallet_name: str, admin_id: str -) -> Wallets: +) -> Wallet: wallet = await create_wallet(user_id=user_id, wallet_name=wallet_name) await db.execute( """ @@ -92,28 +87,28 @@ async def create_usermanager_wallet( return wallet_created -async def get_usermanager_wallet(wallet_id: str) -> Optional[Wallets]: +async def get_usermanager_wallet(wallet_id: str) -> Optional[Wallet]: row = await db.fetchone( "SELECT * FROM usermanager.wallets WHERE id = ?", (wallet_id,) ) - return Wallets(**row) if row else None + return Wallet(**row) if row else None -async def get_usermanager_wallets(admin_id: str) -> Optional[Wallets]: +async def get_usermanager_wallets(admin_id: str) -> list[Wallet]: rows = await db.fetchall( "SELECT * FROM usermanager.wallets WHERE admin = ?", (admin_id,) ) - return [Wallets(**row) for row in rows] + return [Wallet(**row) for row in rows] -async def get_usermanager_users_wallets(user_id: str) -> Optional[Wallets]: +async def get_usermanager_users_wallets(user_id: str) -> list[Wallet]: rows = await db.fetchall( """SELECT * FROM usermanager.wallets WHERE "user" = ?""", (user_id,) ) - return [Wallets(**row) for row in rows] + return [Wallet(**row) for row in rows] -async def get_usermanager_wallet_transactions(wallet_id: str) -> Optional[Payment]: +async def get_usermanager_wallet_transactions(wallet_id: str) -> list[Payment]: return await get_payments( wallet_id=wallet_id, complete=True, pending=False, outgoing=True, incoming=True ) diff --git a/lnbits/extensions/usermanager/models.py b/lnbits/extensions/usermanager/models.py index 15f50e28..05122cc8 100644 --- a/lnbits/extensions/usermanager/models.py +++ b/lnbits/extensions/usermanager/models.py @@ -19,7 +19,7 @@ class CreateUserWallet(BaseModel): admin_id: str = Query(...) -class Users(BaseModel): +class User(BaseModel): id: str name: str admin: str @@ -27,7 +27,7 @@ class Users(BaseModel): password: Optional[str] = None -class Wallets(BaseModel): +class Wallet(BaseModel): id: str admin: str name: str @@ -36,5 +36,5 @@ class Wallets(BaseModel): inkey: str @classmethod - def from_row(cls, row: Row) -> "Wallets": + def from_row(cls, row: Row) -> "Wallet": return cls(**dict(row)) diff --git a/lnbits/extensions/usermanager/views.py b/lnbits/extensions/usermanager/views.py index 420669b0..1891a447 100644 --- a/lnbits/extensions/usermanager/views.py +++ b/lnbits/extensions/usermanager/views.py @@ -9,7 +9,10 @@ from . import usermanager_ext, usermanager_renderer @usermanager_ext.get("/", response_class=HTMLResponse) -async def index(request: Request, user: User = Depends(check_user_exists)): +async def index( + request: Request, + user: User = Depends(check_user_exists) #type: ignore +): return usermanager_renderer().TemplateResponse( "usermanager/index.html", {"request": request, "user": user.dict()} ) diff --git a/lnbits/extensions/usermanager/views_api.py b/lnbits/extensions/usermanager/views_api.py index b1bf8ef8..06020cf7 100644 --- a/lnbits/extensions/usermanager/views_api.py +++ b/lnbits/extensions/usermanager/views_api.py @@ -21,40 +21,44 @@ from .crud import ( get_usermanager_wallet_transactions, get_usermanager_wallets, ) -from .models import CreateUserData, CreateUserWallet - -# Users +from .models import CreateUserData, CreateUserWallet, Wallet @usermanager_ext.get("/api/v1/users", status_code=HTTPStatus.OK) -async def api_usermanager_users(wallet: WalletTypeInfo = Depends(require_admin_key)): +async def api_usermanager_users( + wallet: WalletTypeInfo = Depends(require_admin_key) #type: ignore +): user_id = wallet.wallet.user return [user.dict() for user in await get_usermanager_users(user_id)] @usermanager_ext.get("/api/v1/users/{user_id}", status_code=HTTPStatus.OK) -async def api_usermanager_user(user_id, wallet: WalletTypeInfo = Depends(get_key_type)): +async def api_usermanager_user( + user_id, + wallet: WalletTypeInfo = Depends(get_key_type) #type: ignore +): user = await get_usermanager_user(user_id) + if not user: + return None return user.dict() -@usermanager_ext.post("/api/v1/users", status_code=HTTPStatus.CREATED) -async def api_usermanager_users_create( - data: CreateUserData, wallet: WalletTypeInfo = Depends(get_key_type) -): +@usermanager_ext.post("/api/v1/users", status_code=HTTPStatus.CREATED, dependencies=[Depends(get_key_type)]) +async def api_usermanager_users_create(data: CreateUserData): user = await create_usermanager_user(data) full = user.dict() - full["wallets"] = [ - wallet.dict() for wallet in await get_usermanager_users_wallets(user.id) - ] + wallets: list[Wallet] = await get_usermanager_users_wallets(user.id) + if wallets: + full["wallets"] = [ + wallet.dict() for wallet in wallets + ] return full -@usermanager_ext.delete("/api/v1/users/{user_id}") +@usermanager_ext.delete("/api/v1/users/{user_id}", dependencies=[Depends(require_admin_key)]) async def api_usermanager_users_delete( user_id, delete_core: bool = Query(True), - wallet: WalletTypeInfo = Depends(require_admin_key), ): user = await get_usermanager_user(user_id) if not user: @@ -84,10 +88,8 @@ async def api_usermanager_activate_extension( # Wallets -@usermanager_ext.post("/api/v1/wallets") -async def api_usermanager_wallets_create( - data: CreateUserWallet, wallet: WalletTypeInfo = Depends(get_key_type) -): +@usermanager_ext.post("/api/v1/wallets", dependencies=[Depends(get_key_type)]) +async def api_usermanager_wallets_create(data: CreateUserWallet): user = await create_usermanager_wallet( user_id=data.user_id, wallet_name=data.wallet_name, admin_id=data.admin_id ) @@ -95,31 +97,27 @@ async def api_usermanager_wallets_create( @usermanager_ext.get("/api/v1/wallets") -async def api_usermanager_wallets(wallet: WalletTypeInfo = Depends(require_admin_key)): +async def api_usermanager_wallets( + wallet: WalletTypeInfo = Depends(require_admin_key) #type: ignore +): admin_id = wallet.wallet.user return [wallet.dict() for wallet in await get_usermanager_wallets(admin_id)] -@usermanager_ext.get("/api/v1/transactions/{wallet_id}") -async def api_usermanager_wallet_transactions( - wallet_id, wallet: WalletTypeInfo = Depends(get_key_type) -): +@usermanager_ext.get("/api/v1/transactions/{wallet_id}", dependencies=[Depends(get_key_type)]) +async def api_usermanager_wallet_transactions(wallet_id): return await get_usermanager_wallet_transactions(wallet_id) -@usermanager_ext.get("/api/v1/wallets/{user_id}") -async def api_usermanager_users_wallets( - user_id, wallet: WalletTypeInfo = Depends(require_admin_key) -): +@usermanager_ext.get("/api/v1/wallets/{user_id}", dependencies=[Depends(require_admin_key)]) +async def api_usermanager_users_wallets(user_id): return [ s_wallet.dict() for s_wallet in await get_usermanager_users_wallets(user_id) ] -@usermanager_ext.delete("/api/v1/wallets/{wallet_id}") -async def api_usermanager_wallets_delete( - wallet_id, wallet: WalletTypeInfo = Depends(require_admin_key) -): +@usermanager_ext.delete("/api/v1/wallets/{wallet_id}", dependencies=[Depends(require_admin_key)]) +async def api_usermanager_wallets_delete(wallet_id): get_wallet = await get_usermanager_wallet(wallet_id) if not get_wallet: raise HTTPException( diff --git a/pyproject.toml b/pyproject.toml index a08e5f71..e0a42fc3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -110,7 +110,6 @@ exclude = """(?x)( | ^lnbits/extensions/streamalerts. | ^lnbits/extensions/tipjar. | ^lnbits/extensions/tpos. - | ^lnbits/extensions/usermanager. | ^lnbits/extensions/watchonly. | ^lnbits/extensions/withdraw. | ^lnbits/wallets/lnd_grpc_files.