From c8ab2859fd8db726c535ab27f3848905e1d912e2 Mon Sep 17 00:00:00 2001 From: Tiago Vasconcelos Date: Sun, 17 Jul 2022 19:00:06 +0100 Subject: [PATCH] Splitpayments: floating point percentage for split value (#690) * allow for float in model * recreate DB with float type * remove rounding in UI * black Co-authored-by: callebtc <93376500+callebtc@users.noreply.github.com> --- lnbits/extensions/splitpayments/migrations.py | 38 +++++++++++++++++++ lnbits/extensions/splitpayments/models.py | 4 +- .../splitpayments/static/js/index.js | 2 +- .../templates/splitpayments/index.html | 12 +++--- 4 files changed, 47 insertions(+), 9 deletions(-) diff --git a/lnbits/extensions/splitpayments/migrations.py b/lnbits/extensions/splitpayments/migrations.py index 735afc6c3..b3921c42a 100644 --- a/lnbits/extensions/splitpayments/migrations.py +++ b/lnbits/extensions/splitpayments/migrations.py @@ -14,3 +14,41 @@ async def m001_initial(db): ); """ ) + + +async def m002_float_percent(db): + """ + Add float percent and migrates the existing data. + """ + await db.execute("ALTER TABLE splitpayments.targets RENAME TO splitpayments_old") + await db.execute( + """ + CREATE TABLE splitpayments.targets ( + wallet TEXT NOT NULL, + source TEXT NOT NULL, + percent REAL NOT NULL CHECK (percent >= 0 AND percent <= 100), + alias TEXT, + + UNIQUE (source, wallet) + ); + """ + ) + + for row in [ + list(row) + for row in await db.fetchall("SELECT * FROM splitpayments.splitpayments_old") + ]: + await db.execute( + """ + INSERT INTO splitpayments.targets ( + wallet, + source, + percent, + alias + ) + VALUES (?, ?, ?, ?) + """, + (row[0], row[1], row[2], row[3]), + ) + + await db.execute("DROP TABLE splitpayments.splitpayments_old") diff --git a/lnbits/extensions/splitpayments/models.py b/lnbits/extensions/splitpayments/models.py index 3264bca7c..4b95ed18b 100644 --- a/lnbits/extensions/splitpayments/models.py +++ b/lnbits/extensions/splitpayments/models.py @@ -7,14 +7,14 @@ from pydantic import BaseModel class Target(BaseModel): wallet: str source: str - percent: int + percent: float alias: Optional[str] class TargetPutList(BaseModel): wallet: str = Query(...) alias: str = Query("") - percent: int = Query(..., ge=1) + percent: float = Query(..., ge=0.01) class TargetPut(BaseModel): diff --git a/lnbits/extensions/splitpayments/static/js/index.js b/lnbits/extensions/splitpayments/static/js/index.js index d9750bef1..5d326231d 100644 --- a/lnbits/extensions/splitpayments/static/js/index.js +++ b/lnbits/extensions/splitpayments/static/js/index.js @@ -105,7 +105,7 @@ new Vue({ if (currentTotal > 100 && isPercent) { let diff = (currentTotal - 100) / (100 - this.targets[index].percent) this.targets.forEach((target, t) => { - if (t !== index) target.percent -= Math.round(diff * target.percent) + if (t !== index) target.percent -= +(diff * target.percent).toFixed(2) }) } diff --git a/lnbits/extensions/splitpayments/templates/splitpayments/index.html b/lnbits/extensions/splitpayments/templates/splitpayments/index.html index 1aae4e334..5862abc16 100644 --- a/lnbits/extensions/splitpayments/templates/splitpayments/index.html +++ b/lnbits/extensions/splitpayments/templates/splitpayments/index.html @@ -58,14 +58,14 @@ > - - +
+
Clear - +
- +
Save Targets - - +
+