mirror of
https://gitlab.archlinux.org/archlinux/aurweb.git
synced 2025-02-03 10:43:03 +01:00
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:
parent
541c978ac4
commit
8c345a0448
2 changed files with 15 additions and 2 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
import typing
|
||||||
|
|
||||||
from sqlalchemy import Column, ForeignKey, Integer
|
from sqlalchemy import Column, ForeignKey, Integer
|
||||||
from sqlalchemy.exc import IntegrityError
|
from sqlalchemy.exc import IntegrityError
|
||||||
from sqlalchemy.orm import backref, relationship
|
from sqlalchemy.orm import backref, relationship
|
||||||
|
@ -64,7 +66,7 @@ class TUVoteInfo(Base):
|
||||||
statement="Column Quorum cannot be null.",
|
statement="Column Quorum cannot be null.",
|
||||||
orig="TU_VoteInfo.Quorum",
|
orig="TU_VoteInfo.Quorum",
|
||||||
params=("NULL"))
|
params=("NULL"))
|
||||||
self.Quorum = str(Quorum)
|
self.Quorum = Quorum
|
||||||
|
|
||||||
self.Submitter = Submitter
|
self.Submitter = Submitter
|
||||||
if not self.Submitter:
|
if not self.Submitter:
|
||||||
|
@ -72,3 +74,14 @@ class TUVoteInfo(Base):
|
||||||
statement="Foreign key SubmitterID cannot be null.",
|
statement="Foreign key SubmitterID cannot be null.",
|
||||||
orig="TU_VoteInfo.SubmitterID",
|
orig="TU_VoteInfo.SubmitterID",
|
||||||
params=("NULL"))
|
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
|
||||||
|
|
|
@ -39,7 +39,7 @@ def test_tu_voteinfo_creation():
|
||||||
assert tu_voteinfo.User == user.Username
|
assert tu_voteinfo.User == user.Username
|
||||||
assert tu_voteinfo.Submitted == ts
|
assert tu_voteinfo.Submitted == ts
|
||||||
assert tu_voteinfo.End == ts + 5
|
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.Submitter == user
|
||||||
assert tu_voteinfo.Yes == 0
|
assert tu_voteinfo.Yes == 0
|
||||||
assert tu_voteinfo.No == 0
|
assert tu_voteinfo.No == 0
|
||||||
|
|
Loading…
Add table
Reference in a new issue