splitpayments working - hard one

This commit is contained in:
Tiago vasconcelos 2021-10-18 19:39:35 +01:00
parent cf23e56dc5
commit ca1ad33c7b
4 changed files with 24 additions and 29 deletions

View file

@ -1,21 +1,20 @@
from pydantic.main import BaseModel from typing import List, Optional
from fastapi.param_functions import Query
from pydantic import BaseModel from pydantic import BaseModel
from fastapi import FastAPI, Request
from typing import List
class Target(BaseModel): class Target(BaseModel):
wallet: str wallet: str
source: str source: str
percent: int percent: int
alias: str alias: Optional[str]
class TargetPutList(BaseModel): class TargetPutList(BaseModel):
wallet: str wallet: str = Query(...)
aliat: str alias: str = Query("")
percent: int percent: int = Query(..., ge=1)
class TargetPut(BaseModel): class TargetPut(BaseModel):
targets: List[TargetPutList] __root__: List[TargetPutList]

View file

@ -119,7 +119,7 @@ new Vue({
'/splitpayments/api/v1/targets', '/splitpayments/api/v1/targets',
this.selectedWallet.adminkey, this.selectedWallet.adminkey,
{ {
targets: this.targets "targets": this.targets
.filter(isTargetComplete) .filter(isTargetComplete)
.map(({wallet, percent, alias}) => ({wallet, percent, alias})) .map(({wallet, percent, alias}) => ({wallet, percent, alias}))
} }

View file

@ -1,12 +1,12 @@
from http import HTTPStatus from fastapi import Request
from lnbits.decorators import check_user_exists, WalletTypeInfo, get_key_type
from . import splitpayments_ext, splitpayments_renderer
from fastapi import FastAPI, Request
from fastapi.params import Depends from fastapi.params import Depends
from fastapi.templating import Jinja2Templates from fastapi.templating import Jinja2Templates
from starlette.exceptions import HTTPException
from starlette.responses import HTMLResponse from starlette.responses import HTMLResponse
from lnbits.core.models import User, Payment
from lnbits.core.models import User
from lnbits.decorators import check_user_exists
from . import splitpayments_ext, splitpayments_renderer
templates = Jinja2Templates(directory="templates") templates = Jinja2Templates(directory="templates")

View file

@ -1,17 +1,11 @@
import base64
import json
from http import HTTPStatus from http import HTTPStatus
from typing import Optional
import httpx
from fastapi import Request from fastapi import Request
from fastapi.param_functions import Query
from fastapi.params import Depends from fastapi.params import Depends
from starlette.exceptions import HTTPException from starlette.exceptions import HTTPException
from starlette.responses import HTMLResponse, JSONResponse # type: ignore
from lnbits.core.crud import get_wallet, get_wallet_for_key from lnbits.core.crud import get_wallet, get_wallet_for_key
from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key from lnbits.decorators import WalletTypeInfo, require_admin_key
from . import splitpayments_ext from . import splitpayments_ext
from .crud import get_targets, set_targets from .crud import get_targets, set_targets
@ -20,17 +14,19 @@ from .models import Target, TargetPut
@splitpayments_ext.get("/api/v1/targets") @splitpayments_ext.get("/api/v1/targets")
async def api_targets_get(wallet: WalletTypeInfo = Depends(require_admin_key)): async def api_targets_get(wallet: WalletTypeInfo = Depends(require_admin_key)):
print(wallet)
targets = await get_targets(wallet.wallet.id) targets = await get_targets(wallet.wallet.id)
return [target.dict() for target in targets] or [] return [target.dict() for target in targets] or []
@splitpayments_ext.put("/api/v1/targets") @splitpayments_ext.put("/api/v1/targets")
async def api_targets_set( async def api_targets_set(
data: TargetPut, wallet: WalletTypeInfo = Depends(require_admin_key) req: Request, wal: WalletTypeInfo = Depends(require_admin_key)
): ):
body = await req.json()
targets = [] targets = []
for entry in data.targets: data = TargetPut.parse_obj(body["targets"])
for entry in data.__root__:
print("ENTRY", entry)
wallet = await get_wallet(entry.wallet) wallet = await get_wallet(entry.wallet)
if not wallet: if not wallet:
wallet = await get_wallet_for_key(entry.wallet, "invoice") wallet = await get_wallet_for_key(entry.wallet, "invoice")
@ -40,7 +36,7 @@ async def api_targets_set(
detail=f"Invalid wallet '{entry.wallet}'.", detail=f"Invalid wallet '{entry.wallet}'.",
) )
if wallet.id == wallet.wallet.id: if wallet.id == wal.wallet.id:
raise HTTPException( raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, status_code=HTTPStatus.BAD_REQUEST,
detail="Can't split to itself.", detail="Can't split to itself.",
@ -53,7 +49,7 @@ async def api_targets_set(
) )
targets.append( targets.append(
Target(wallet.id, wallet.wallet.id, entry.percent, entry.alias or "") Target(wallet=wallet.id, source=wal.wallet.id, percent=entry.percent, alias=entry.alias)
) )
percent_sum = sum([target.percent for target in targets]) percent_sum = sum([target.percent for target in targets])
@ -63,5 +59,5 @@ async def api_targets_set(
detail="Splitting over 100%.", detail="Splitting over 100%.",
) )
await set_targets(wallet.wallet.id, targets) await set_targets(wal.wallet.id, targets)
return "" return ""