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 20:00:06 +02:00
committed by GitHub
co-authored by callebtc
parent 2b827d37fa
commit c8ab2859fd
4 changed files with 47 additions and 9 deletions
@@ -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")
+2 -2
View File
@@ -7,14 +7,14 @@ from pydantic import BaseModel
class Target(BaseModel): class Target(BaseModel):
wallet: str wallet: str
source: str source: str
percent: int percent: float
alias: Optional[str] alias: Optional[str]
class TargetPutList(BaseModel): class TargetPutList(BaseModel):
wallet: str = Query(...) wallet: str = Query(...)
alias: str = Query("") alias: str = Query("")
percent: int = Query(..., ge=1) percent: float = Query(..., ge=0.01)
class TargetPut(BaseModel): class TargetPut(BaseModel):
@@ -105,7 +105,7 @@ new Vue({
if (currentTotal > 100 && isPercent) { if (currentTotal > 100 && isPercent) {
let diff = (currentTotal - 100) / (100 - this.targets[index].percent) let diff = (currentTotal - 100) / (100 - this.targets[index].percent)
this.targets.forEach((target, t) => { 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)
}) })
} }
@@ -58,14 +58,14 @@
></q-input> ></q-input>
</div> </div>
<q-row class="row justify-evenly q-pa-lg"> <div class="row justify-evenly q-pa-lg">
<q-col> <div>
<q-btn unelevated outline color="secondary" @click="clearTargets"> <q-btn unelevated outline color="secondary" @click="clearTargets">
Clear Clear
</q-btn> </q-btn>
</q-col> </div>
<q-col> <div>
<q-btn <q-btn
unelevated unelevated
color="primary" color="primary"
@@ -74,8 +74,8 @@
> >
Save Targets Save Targets
</q-btn> </q-btn>
</q-col> </div>
</q-row> </div>
</q-form> </q-form>
</q-card-section> </q-card-section>
</q-card> </q-card>