Merge pull request #1262 from lnbits/fix/mypy/tipjar

fix tipjar mypy
This commit is contained in:
calle 2023-01-03 12:21:16 +01:00 committed by GitHub
commit f6ec1bc92c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 49 deletions

View File

@ -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

View File

@ -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):

View File

@ -1,8 +1,7 @@
from http import HTTPStatus
from fastapi import Request
from fastapi import Depends, Request
from fastapi.param_functions import Query
from fastapi.params import Depends
from fastapi.templating import Jinja2Templates
from starlette.exceptions import HTTPException

View File

@ -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 createTip, createTipJar, 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,28 +92,34 @@ 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)
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:
@ -125,7 +136,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"
@ -135,7 +146,9 @@ 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)
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 +164,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 +194,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)

View File

@ -107,7 +107,6 @@ exclude = """(?x)(
| ^lnbits/extensions/scrub.
| ^lnbits/extensions/splitpayments.
| ^lnbits/extensions/streamalerts.
| ^lnbits/extensions/tipjar.
| ^lnbits/extensions/tpos.
| ^lnbits/extensions/watchonly.
| ^lnbits/extensions/withdraw.