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>
This commit is contained in:
Tiago Vasconcelos 2022-07-17 19:00:06 +01:00 committed by GitHub
parent 2b827d37fa
commit c8ab2859fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 9 deletions

View File

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

View File

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

View File

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

View File

@ -58,14 +58,14 @@
></q-input>
</div>
<q-row class="row justify-evenly q-pa-lg">
<q-col>
<div class="row justify-evenly q-pa-lg">
<div>
<q-btn unelevated outline color="secondary" @click="clearTargets">
Clear
</q-btn>
</q-col>
</div>
<q-col>
<div>
<q-btn
unelevated
color="primary"
@ -74,8 +74,8 @@
>
Save Targets
</q-btn>
</q-col>
</q-row>
</div>
</div>
</q-form>
</q-card-section>
</q-card>