From 4b388475ced32ab0f84e7889d6b6e123f3960931 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Fri, 30 Dec 2022 08:48:31 +0100 Subject: [PATCH] fix tipjar mypy issues --- lnbits/extensions/tipjar/crud.py | 17 +++------- lnbits/extensions/tipjar/models.py | 17 ++-------- lnbits/extensions/tipjar/views.py | 3 +- lnbits/extensions/tipjar/views_api.py | 48 +++++++++++++++------------ pyproject.toml | 1 - 5 files changed, 34 insertions(+), 52 deletions(-) diff --git a/lnbits/extensions/tipjar/crud.py b/lnbits/extensions/tipjar/crud.py index 046b9491..ff9220b1 100644 --- a/lnbits/extensions/tipjar/crud.py +++ b/lnbits/extensions/tipjar/crud.py @@ -8,7 +8,7 @@ from .models import Tip, TipJar, createTipJar async def create_tip( - id: int, wallet: str, message: str, name: str, sats: int, tipjar: str + id: str, wallet: str, message: str, name: str, sats: int, tipjar: str ) -> Tip: """Create a new Tip""" await db.execute( @@ -33,11 +33,7 @@ async def create_tip( async def create_tipjar(data: createTipJar) -> TipJar: """Create a new TipJar""" - - returning = "" if db.type == SQLITE else "RETURNING ID" - method = db.execute if db.type == SQLITE else db.fetchone - - result = await (method)( + await db.execute( f""" INSERT INTO tipjar.TipJars ( name, @@ -46,16 +42,11 @@ async def create_tipjar(data: createTipJar) -> TipJar: onchain ) VALUES (?, ?, ?, ?) - {returning} """, (data.name, data.wallet, data.webhook, data.onchain), ) - if db.type == SQLITE: - tipjar_id = result._result_proxy.lastrowid - else: - tipjar_id = result[0] - - tipjar = await get_tipjar(tipjar_id) + row = await db.fetchone( "SELECT * FROM tipjar.TipJars LIMIT 1") + tipjar = TipJar(**row) assert tipjar return tipjar diff --git a/lnbits/extensions/tipjar/models.py b/lnbits/extensions/tipjar/models.py index 92f25ab3..655888da 100644 --- a/lnbits/extensions/tipjar/models.py +++ b/lnbits/extensions/tipjar/models.py @@ -1,20 +1,7 @@ from sqlite3 import Row from typing import Optional -from fastapi.param_functions import Query from pydantic import BaseModel -from pydantic.main import BaseModel - - -class CreateCharge(BaseModel): - onchainwallet: str = Query(None) - lnbitswallet: str = Query(None) - description: str = Query(...) - webhook: str = Query(None) - completelink: str = Query(None) - completelinktext: str = Query(None) - time: int = Query(..., ge=1) - amount: int = Query(..., ge=1) class createTip(BaseModel): @@ -44,8 +31,8 @@ class Tip(BaseModel): class createTipJar(BaseModel): name: str wallet: str - webhook: str = None - onchain: str = None + webhook: Optional[str] + onchain: Optional[str] class createTips(BaseModel): diff --git a/lnbits/extensions/tipjar/views.py b/lnbits/extensions/tipjar/views.py index 21a87246..21da0d2e 100644 --- a/lnbits/extensions/tipjar/views.py +++ b/lnbits/extensions/tipjar/views.py @@ -1,8 +1,7 @@ from http import HTTPStatus -from fastapi import Request +from fastapi import Request, Depends from fastapi.param_functions import Query -from fastapi.params import Depends from fastapi.templating import Jinja2Templates from starlette.exceptions import HTTPException diff --git a/lnbits/extensions/tipjar/views_api.py b/lnbits/extensions/tipjar/views_api.py index 50c5138b..bbc824e8 100644 --- a/lnbits/extensions/tipjar/views_api.py +++ b/lnbits/extensions/tipjar/views_api.py @@ -1,13 +1,13 @@ from http import HTTPStatus -from fastapi.param_functions import Query -from fastapi.params import Depends +from fastapi import Depends, Query from starlette.exceptions import HTTPException from lnbits.core.crud import get_user from lnbits.decorators import WalletTypeInfo, get_key_type from ..satspay.crud import create_charge +from ..satspay.models import CreateCharge from . import tipjar_ext from .crud import ( create_tip, @@ -22,7 +22,7 @@ from .crud import ( update_tipjar, ) from .helpers import get_charge_details -from .models import CreateCharge, createTipJar, createTips +from .models import createTipJar, createTip, createTips @tipjar_ext.post("/api/v1/tipjars") @@ -43,12 +43,16 @@ async def user_from_wallet(wallet: WalletTypeInfo = Depends(get_key_type)): @tipjar_ext.post("/api/v1/tips") async def api_create_tip(data: createTips): """Take data from tip form and return satspay charge""" - sats = data.sats + sats = int(data.sats) message = data.message if not message: message = "No message" - tipjar_id = data.tipjar + tipjar_id = int(data.tipjar) tipjar = await get_tipjar(tipjar_id) + if not tipjar: + raise HTTPException( + status_code=HTTPStatus.NOT_FOUND, detail="Tipjar does not exist." + ) webhook = tipjar.webhook charge_details = await get_charge_details(tipjar.id) @@ -62,13 +66,14 @@ async def api_create_tip(data: createTips): user=charge_details["user"], data=CreateCharge( amount=sats, - webhook=webhook, + webhook=webhook or "", description=description, onchainwallet=charge_details["onchainwallet"], lnbitswallet=charge_details["lnbitswallet"], completelink=charge_details["completelink"], completelinktext=charge_details["completelinktext"], time=charge_details["time"], + custom_css="", ), ) @@ -77,7 +82,7 @@ async def api_create_tip(data: createTips): wallet=tipjar.wallet, message=message, name=name, - sats=data.sats, + sats=int(data.sats), tipjar=data.tipjar, ) @@ -87,29 +92,31 @@ async def api_create_tip(data: createTips): @tipjar_ext.get("/api/v1/tipjars") async def api_get_tipjars(wallet: WalletTypeInfo = Depends(get_key_type)): """Return list of all tipjars assigned to wallet with given invoice key""" - wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids + user = await get_user(wallet.wallet.user) + if not user: + return [] tipjars = [] - for wallet_id in wallet_ids: + for wallet_id in user.wallet_ids: new_tipjars = await get_tipjars(wallet_id) tipjars += new_tipjars if new_tipjars else [] - return [tipjar.dict() for tipjar in tipjars] if tipjars else [] + return [tipjar.dict() for tipjar in tipjars] @tipjar_ext.get("/api/v1/tips") async def api_get_tips(wallet: WalletTypeInfo = Depends(get_key_type)): """Return list of all tips assigned to wallet with given invoice key""" - wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids + user = await get_user(wallet.wallet.user) + if not user: + return [] tips = [] - for wallet_id in wallet_ids: + for wallet_id in user.wallet_ids: new_tips = await get_tips(wallet_id) tips += new_tips if new_tips else [] - return [tip.dict() for tip in tips] if tips else [] + return [tip.dict() for tip in tips] @tipjar_ext.put("/api/v1/tips/{tip_id}") -async def api_update_tip( - wallet: WalletTypeInfo = Depends(get_key_type), tip_id: str = Query(None) -): +async def api_update_tip(data: createTip, wallet: WalletTypeInfo = Depends(get_key_type), tip_id: str = Query(None)): """Update a tip with the data given in the request""" if tip_id: tip = await get_tip(tip_id) @@ -125,7 +132,7 @@ async def api_update_tip( status_code=HTTPStatus.FORBIDDEN, detail="Not your tip." ) - tip = await update_tip(tip_id, **g.data) + tip = await update_tip(tip_id, **data.dict()) else: raise HTTPException( status_code=HTTPStatus.BAD_REQUEST, detail="No tip ID specified" @@ -134,8 +141,7 @@ async def api_update_tip( @tipjar_ext.put("/api/v1/tipjars/{tipjar_id}") -async def api_update_tipjar( - wallet: WalletTypeInfo = Depends(get_key_type), tipjar_id: str = Query(None) +async def api_update_tipjar(data: createTipJar, wallet: WalletTypeInfo = Depends(get_key_type), tipjar_id: int = Query(None) ): """Update a tipjar with the data given in the request""" if tipjar_id: @@ -151,7 +157,7 @@ async def api_update_tipjar( status_code=HTTPStatus.FORBIDDEN, detail="Not your tipjar." ) - tipjar = await update_tipjar(tipjar_id, **data) + tipjar = await update_tipjar(str(tipjar_id), **data.dict()) else: raise HTTPException( status_code=HTTPStatus.BAD_REQUEST, detail="No tipjar ID specified" @@ -181,7 +187,7 @@ async def api_delete_tip( @tipjar_ext.delete("/api/v1/tipjars/{tipjar_id}") async def api_delete_tipjar( - wallet: WalletTypeInfo = Depends(get_key_type), tipjar_id: str = Query(None) + wallet: WalletTypeInfo = Depends(get_key_type), tipjar_id: int = Query(None) ): """Delete the tipjar with the given tipjar_id""" tipjar = await get_tipjar(tipjar_id) diff --git a/pyproject.toml b/pyproject.toml index 573eef1b..0e660abe 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -110,7 +110,6 @@ exclude = """(?x)( | ^lnbits/extensions/scrub. | ^lnbits/extensions/splitpayments. | ^lnbits/extensions/streamalerts. - | ^lnbits/extensions/tipjar. | ^lnbits/extensions/tpos. | ^lnbits/extensions/watchonly. | ^lnbits/extensions/withdraw.