Added new Submission column: count

Since I'm not good at complex SQLAlchemy syntax and database querying
through SQL or the ORM, I'm simply using a descriptor to update the
count column everytime votes is modified.
This commit is contained in:
Xevion
2021-02-18 04:06:39 -06:00
parent 1b777a3689
commit bd3778182d

View File

@@ -83,11 +83,24 @@ class Submission(Base):
id = Column(Integer, primary_key=True) # Doubles as the ID this Guild has in Discord id = Column(Integer, primary_key=True) # Doubles as the ID this Guild has in Discord
user = Column(Integer) # The ID of the user who submitted it. user = Column(Integer) # The ID of the user who submitted it.
timestamp = Column(DateTime) # When the Submission was posted timestamp = Column(DateTime) # When the Submission was posted
votes: List[int] = Column(NestedMutableList.as_mutable(JSON)) # A list of IDs correlating to users who voted on this submission.
_votes: List[int] = Column("votes", NestedMutableList.as_mutable(JSON)) # A list of IDs correlating to users who voted on this submission.
count = Column(Integer, default=0, nullable=False)
period_id = Column(Integer, ForeignKey("period.id")) # The id of the period this Submission relates to. period_id = Column(Integer, ForeignKey("period.id")) # The id of the period this Submission relates to.
period = relationship("Period", back_populates="submissions") # The period this submission was made in. period = relationship("Period", back_populates="submissions") # The period this submission was made in.
@property
def votes(self) -> List[int]:
"""Getter function for _votes descriptor."""
return self._votes
@votes.setter
def votes(self, votes: List[int]) -> None:
""""Setter function for _votes descriptor. Modifies count column."""
self._votes = votes
self.count = len(votes)
def __init__(self, **kwds): def __init__(self, **kwds):
# Adds default column behavior for Mutable JSON votes column # Adds default column behavior for Mutable JSON votes column
kwds.setdefault("votes", []) kwds.setdefault("votes", [])
@@ -96,11 +109,6 @@ class Submission(Base):
def __repr__(self) -> str: def __repr__(self) -> str:
return 'Submission(id={id}, user={user}, period={period_id}, {votes})'.format(**self.__dict__) return 'Submission(id={id}, user={user}, period={period_id}, {votes})'.format(**self.__dict__)
@property
def count(self) -> int:
"""The number of votes cast for this submission."""
return len(self.votes)
def increment(self, user: int) -> None: def increment(self, user: int) -> None:
"""Increase the number of votes by one.""" """Increase the number of votes by one."""
if user == self.user: if user == self.user:
@@ -226,6 +234,21 @@ class Period(Base):
voting_time = Column(DateTime, nullable=True) # When this period switched to the Voting state. voting_time = Column(DateTime, nullable=True) # When this period switched to the Voting state.
finished_time = Column(DateTime, nullable=True) # When this period switched to the Finished state. finished_time = Column(DateTime, nullable=True) # When this period switched to the Finished state.
async def get_submission_messages(self, bot: 'ContestBot') -> List[Tuple[Submission, discord.Message]]:
"""
Returns a list of tuples containing Submission objects and full Discord Messages
:param bot: the active Discord Bot instance
"""
found = []
for submission in self.submissions:
try:
message = await bot.fetch_message(self.guild.submission_channel, submission.id)
found.append((submission, message))
except discord.NotFound:
found.append((submission, None))
return found
@check_not_finished @check_not_finished
def advance_state(self) -> PeriodStates: def advance_state(self) -> PeriodStates:
""" """
@@ -246,6 +269,11 @@ class Period(Base):
self.state = next_state self.state = next_state
return next_state return next_state
@property
def voting(self) -> bool:
"""Whether or not the Period (should) be allowing voting updates through."""
return self.active and not self.completed and self.state == PeriodStates.VOTING
@check_not_finished @check_not_finished
def deactivate(self) -> None: def deactivate(self) -> None:
""" """