All extensions semi-switched

This commit is contained in:
Ben Arc
2021-08-21 00:34:48 +01:00
parent 8deea85999
commit a9dc087f61
17 changed files with 282 additions and 372 deletions
+1 -1
View File
@@ -5,7 +5,7 @@ from lnbits.decorators import check_user_exists, validate_uuids
from . import splitpayments_ext
@splitpayments_ext.route("/")
@splitpayments_ext.get("/")
@validate_uuids(["usr"], required=True)
@check_user_exists()
async def index():
+13 -24
View File
@@ -9,52 +9,41 @@ from .crud import get_targets, set_targets
from .models import Target
@splitpayments_ext.route("/api/v1/targets", methods=["GET"])
@splitpayments_ext.get("/api/v1/targets")
@api_check_wallet_key("admin")
async def api_targets_get():
targets = await get_targets(g.wallet.id)
return jsonify([target._asdict() for target in targets] or [])
return [target._asdict() for target in targets] or []
class SchemaData(BaseModel):
wallet: str
alias: str
percent: int
@splitpayments_ext.route("/api/v1/targets", methods=["PUT"])
@splitpayments_ext.put("/api/v1/targets")
@api_check_wallet_key("admin")
@api_validate_post_request(
schema={
"targets": {
"type": "list",
"schema": {
"type": "dict",
"schema": {
"wallet": {"type": "string"},
"alias": {"type": "string"},
"percent": {"type": "integer"},
},
},
}
}
)
async def api_targets_set():
async def api_targets_set(targets: Optional(list[SchemaData]) = None):
targets = []
for entry in g.data["targets"]:
for entry in targets:
wallet = await get_wallet(entry["wallet"])
if not wallet:
wallet = await get_wallet_for_key(entry["wallet"], "invoice")
if not wallet:
return (
jsonify({"message": f"Invalid wallet '{entry['wallet']}'."}),
{"message": f"Invalid wallet '{entry['wallet']}'."},
HTTPStatus.BAD_REQUEST,
)
if wallet.id == g.wallet.id:
return (
jsonify({"message": "Can't split to itself."}),
{"message": "Can't split to itself."},
HTTPStatus.BAD_REQUEST,
)
if entry["percent"] < 0:
return (
jsonify({"message": f"Invalid percent '{entry['percent']}'."}),
{"message": f"Invalid percent '{entry['percent']}'."},
HTTPStatus.BAD_REQUEST,
)
@@ -64,7 +53,7 @@ async def api_targets_set():
percent_sum = sum([target.percent for target in targets])
if percent_sum > 100:
return jsonify({"message": "Splitting over 100%."}), HTTPStatus.BAD_REQUEST
return {"message": "Splitting over 100%."}, HTTPStatus.BAD_REQUEST
await set_targets(g.wallet.id, targets)
return "", HTTPStatus.OK