Add abstaining from voting.

This commit is contained in:
2023-11-03 13:38:23 -04:00
parent 289b752678
commit 80c4f053bc
2 changed files with 18 additions and 6 deletions
+16 -5
View File
@@ -6,13 +6,12 @@ from typing import List
import nextcord import nextcord
from nextcord import SelectOption from nextcord import SelectOption
from nextcord.ext import commands
from nextcord.interactions import Interaction from nextcord.interactions import Interaction
from sqlalchemy import select from sqlalchemy import exc, select
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from config import DakinyConfig, ElectionCycle from config import DakinyConfig
from database_setup.sql_data_classes import CouncilMembers, ElectionInformation, engine from database_setup.sql_data_classes import CouncilMembers, ElectionInformation, Votes, engine
from utils import build_votes_str, get_election_cycle_name from utils import build_votes_str, get_election_cycle_name
@@ -33,7 +32,12 @@ class VoteSelect(nextcord.ui.StringSelect):
ElectionInformation.ElectionCycle == election_name ElectionInformation.ElectionCycle == election_name
) )
vote_msg_id = session.scalar(stmt) vote_msg_id = session.scalar(stmt)
if self.values[0] == "Abstain":
voted_for = None
else:
voted_for = int(self.values[0]) voted_for = int(self.values[0])
new_vote = Votes(ElectionCycle=election_name, VoterId=interaction.user.id, VotedForId=voted_for) new_vote = Votes(ElectionCycle=election_name, VoterId=interaction.user.id, VotedForId=voted_for)
try: try:
@@ -54,7 +58,13 @@ class VoteSelect(nextcord.ui.StringSelect):
vote_message_str = build_votes_str(self.config) vote_message_str = build_votes_str(self.config)
vote_msg = await interaction.channel.fetch_message(vote_msg_id) vote_msg = await interaction.channel.fetch_message(vote_msg_id)
await vote_msg.edit(content=vote_message_str) await vote_msg.edit(content=vote_message_str)
await interaction.edit(content=f"You have voted for <@{self.values[0]}>", view=None)
if voted_for is not None:
msg = f"You have voted for <@{voted_for}>"
else:
msg = "You have abstained from voting."
await interaction.edit(content=msg, view=None)
class VoteView(nextcord.ui.View): class VoteView(nextcord.ui.View):
@@ -98,6 +108,7 @@ class VoteView(nextcord.ui.View):
) )
for nominee in session.scalars(stmt) for nominee in session.scalars(stmt)
] ]
options.append(SelectOption(label="Abstain ☑️" if voted_for is None else "Abstain", value="Abstain"))
view = nextcord.ui.View() view = nextcord.ui.View()
view.add_item(VoteSelect(options=options, config=self.config)) view.add_item(VoteSelect(options=options, config=self.config))
+2 -1
View File
@@ -90,7 +90,8 @@ def build_votes_str(config: DakinyConfig) -> str:
stmt = select(Votes).where(Votes.ElectionCycle == get_election_cycle_name(config)) stmt = select(Votes).where(Votes.ElectionCycle == get_election_cycle_name(config))
for vote in session.scalars(stmt): for vote in session.scalars(stmt):
votes[vote.VotedForId] += 1 if vote.VotedForId is not None:
votes[vote.VotedForId] += 1
votes_str: str = "====================" votes_str: str = "===================="
for user_id, num_votes in votes.items(): for user_id, num_votes in votes.items():