From e93f37dcf0fec051fbb8592feddc5854182d70cb Mon Sep 17 00:00:00 2001 From: imcompiling Date: Sun, 19 May 2024 10:38:10 -0400 Subject: [PATCH] Use VoteType in voting highcouncil views. --- classes/voting_highcouncil_views.py | 34 ++++++++++++++--------------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/classes/voting_highcouncil_views.py b/classes/voting_highcouncil_views.py index 3899b57..2a36b70 100644 --- a/classes/voting_highcouncil_views.py +++ b/classes/voting_highcouncil_views.py @@ -17,12 +17,7 @@ from database_setup.sql_data_classes import ( Votes, engine, ) -from utils import ( - build_highcouncil_votes_str, - get_cl_election_name, - get_dm_election_name, - get_election_name, -) +from utils import VoteType, build_highcouncil_votes_str, get_election_name class VoteSelect(nextcord.ui.StringSelect): @@ -42,17 +37,17 @@ class VoteSelect(nextcord.ui.StringSelect): # decide on election type match self.election_type: case "cl": - election_name: str = get_cl_election_name(self.config) + election_type: str = VoteType.CLANLEADER case "dm": - election_name: str = get_dm_election_name(self.config) + election_type: str = VoteType.DISCORDMANAGER case _: # should never happen raise ValueError('election type was not "cl" or "dm"') - cycle: str = get_election_name(self.config) + election_name: str = get_election_name(self.config) with Session(engine) as session: stmt = select(ElectionInformation.HighCouncilVoteMsgId).where( - ElectionInformation.ElectionCycle == cycle + ElectionInformation.ElectionCycle == election_name ) vote_msg_id = session.scalar(stmt) @@ -65,6 +60,7 @@ class VoteSelect(nextcord.ui.StringSelect): ElectionCycle=election_name, VoterId=interaction.user.id, VotedForId=voted_for, + Type=election_type, ) try: @@ -72,11 +68,13 @@ class VoteSelect(nextcord.ui.StringSelect): session.commit() except exc.IntegrityError: + # user already has a vote in the election and election type session.rollback() stmt = ( update(Votes) .where(Votes.ElectionCycle == election_name) .where(Votes.VoterId == interaction.user.id) + .where(Votes.Type == election_type) .values(VotedForId=voted_for) ) session.execute(stmt) @@ -135,20 +133,20 @@ class VoteHighcouncilView(nextcord.ui.View): ) return - cycle: str = get_election_name(self.config) - election: str = get_cl_election_name(self.config) + election_name: str = get_election_name(self.config) with Session(engine) as session: stmt = ( select(Votes.VotedForId) - .where(Votes.ElectionCycle == election) + .where(Votes.ElectionCycle == election_name) .where(Votes.VoterId == interaction.user.id) + .where(Votes.Type == VoteType.CLANLEADER) ) voted_for = session.scalar(stmt) stmt = ( select(CouncilMembers.MemberId) - .where(CouncilMembers.ElectionCycle == cycle) + .where(CouncilMembers.ElectionCycle == election_name) .filter(CouncilMembers.ClanLeaderSpeechId.is_not(None)) ) @@ -213,20 +211,20 @@ class VoteHighcouncilView(nextcord.ui.View): ) return - cycle: str = get_election_name(self.config) - election: str = get_dm_election_name(self.config) + election_name: str = get_election_name(self.config) with Session(engine) as session: stmt = ( select(Votes.VotedForId) - .where(Votes.ElectionCycle == election) + .where(Votes.ElectionCycle == election_name) .where(Votes.VoterId == interaction.user.id) + .where(Votes.Type == VoteType.DISCORDMANAGER) ) voted_for = session.scalar(stmt) stmt = ( select(CouncilMembers.MemberId) - .where(CouncilMembers.ElectionCycle == cycle) + .where(CouncilMembers.ElectionCycle == election_name) .filter(CouncilMembers.DiscordManagerSpeechId.is_not(None)) )