From 8c345a04488a07b9836cff9559bb17722b5fc77e Mon Sep 17 00:00:00 2001 From: Kevin Morris Date: Fri, 11 Jun 2021 21:48:39 -0700 Subject: [PATCH] TUVoteInfo: generalize Quorum SQLite does not support native DECIMAL columns, and for that reason, we had to switch to using Strings that can hold the data in the case we are using sqlite. This commit sets the TUVoteInfo model up in a generic way, that it always converts to string when setting Quorum (OK for DECIMAL) and always converts to float when getting Quorum. This way, we can treat TUVoteInfo.Quorum as the same thing everywhere. Signed-off-by: Kevin Morris --- aurweb/models/tu_voteinfo.py | 15 ++++++++++++++- test/test_tu_voteinfo.py | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/aurweb/models/tu_voteinfo.py b/aurweb/models/tu_voteinfo.py index 2225b4d7..a246f132 100644 --- a/aurweb/models/tu_voteinfo.py +++ b/aurweb/models/tu_voteinfo.py @@ -1,3 +1,5 @@ +import typing + from sqlalchemy import Column, ForeignKey, Integer from sqlalchemy.exc import IntegrityError from sqlalchemy.orm import backref, relationship @@ -64,7 +66,7 @@ class TUVoteInfo(Base): statement="Column Quorum cannot be null.", orig="TU_VoteInfo.Quorum", params=("NULL")) - self.Quorum = str(Quorum) + self.Quorum = Quorum self.Submitter = Submitter if not self.Submitter: @@ -72,3 +74,14 @@ class TUVoteInfo(Base): statement="Foreign key SubmitterID cannot be null.", orig="TU_VoteInfo.SubmitterID", params=("NULL")) + + def __setattr__(self, key: str, value: typing.Any): + """ Customize setattr to stringify any Quorum keys given. """ + if key == "Quorum": + value = str(value) + return super().__setattr__(key, value) + + def __getattribute__(self, key: str): + """ Customize getattr to floatify any fetched Quorum values. """ + attr = super().__getattribute__(key) + return float(attr) if key == "Quorum" else attr diff --git a/test/test_tu_voteinfo.py b/test/test_tu_voteinfo.py index e95f174b..37609efd 100644 --- a/test/test_tu_voteinfo.py +++ b/test/test_tu_voteinfo.py @@ -39,7 +39,7 @@ def test_tu_voteinfo_creation(): assert tu_voteinfo.User == user.Username assert tu_voteinfo.Submitted == ts assert tu_voteinfo.End == ts + 5 - assert float(tu_voteinfo.Quorum) == 0.5 + assert tu_voteinfo.Quorum == 0.5 assert tu_voteinfo.Submitter == user assert tu_voteinfo.Yes == 0 assert tu_voteinfo.No == 0