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 <kevr@0cost.org>
This commit is contained in:
Kevin Morris 2021-06-11 21:48:39 -07:00
parent 541c978ac4
commit 8c345a0448
2 changed files with 15 additions and 2 deletions

View file

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

View file

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