From 54933b427ea7828236462debf92fe3dc23421b84 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Wed, 3 Jan 2024 21:46:54 -0500 Subject: [PATCH] Sort nominees in voting views by display name. --- classes/election_cycle.py | 5 +++-- classes/voting_highcouncil_views.py | 12 +++++++++--- classes/voting_views.py | 7 +++++-- utils.py | 22 +++++++++++++++++----- 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/classes/election_cycle.py b/classes/election_cycle.py index d2acd5b..f4c21ac 100644 --- a/classes/election_cycle.py +++ b/classes/election_cycle.py @@ -239,7 +239,8 @@ class ElectionCycle: ) await election_thread.send(message) vote_msg = await election_thread.send( - build_votes_str(self.config), view=VoteView(self.config, self.guild) + build_votes_str(self.config, self.guild), + view=VoteView(self.config, self.guild), ) with Session(engine) as session: @@ -324,7 +325,7 @@ class ElectionCycle: ) await election_thread.send(message) vote_msg = await election_thread.send( - build_highcouncil_votes_str(self.config), + build_highcouncil_votes_str(self.config, self.guild), view=VoteHighcouncilView(self.config, self.guild), ) diff --git a/classes/voting_highcouncil_views.py b/classes/voting_highcouncil_views.py index 44ff2b6..385ee7a 100644 --- a/classes/voting_highcouncil_views.py +++ b/classes/voting_highcouncil_views.py @@ -82,7 +82,7 @@ class VoteSelect(nextcord.ui.StringSelect): session.execute(stmt) session.commit() - vote_message_str = build_highcouncil_votes_str(self.config) + vote_message_str = build_highcouncil_votes_str(self.config, interaction.guild) vote_msg: nextcord.Message = await interaction.channel.fetch_message( vote_msg_id ) @@ -159,7 +159,10 @@ class VoteHighcouncilView(nextcord.ui.View): else f"{self.guild.get_member(nominee).display_name}", value=str(nominee), ) - for nominee in session.scalars(stmt) + for nominee in sorted( + session.scalars(stmt), + key=lambda nominee: self.guild.get_member(nominee).display_name, + ) ] options.append( SelectOption( @@ -232,7 +235,10 @@ class VoteHighcouncilView(nextcord.ui.View): else f"{self.guild.get_member(nominee).display_name}", value=str(nominee), ) - for nominee in session.scalars(stmt) + for nominee in sorted( + session.scalars(stmt), + key=lambda nominee: self.guild.get_member(nominee).display_name, + ) ] options.append( SelectOption( diff --git a/classes/voting_views.py b/classes/voting_views.py index 7a39299..ef75720 100644 --- a/classes/voting_views.py +++ b/classes/voting_views.py @@ -64,7 +64,7 @@ class VoteSelect(nextcord.ui.StringSelect): session.execute(stmt) session.commit() - vote_message_str = build_votes_str(self.config) + vote_message_str = build_votes_str(self.config, interaction.guild) vote_msg: nextcord.Message = await interaction.channel.fetch_message( vote_msg_id ) @@ -135,7 +135,10 @@ class VoteView(nextcord.ui.View): else f"{self.guild.get_member(nominee).display_name}", value=str(nominee), ) - for nominee in session.scalars(stmt) + for nominee in sorted( + session.scalars(stmt), + key=lambda nominee: self.guild.get_member(nominee).display_name, + ) ] options.append( SelectOption( diff --git a/utils.py b/utils.py index 357932e..cb1454a 100644 --- a/utils.py +++ b/utils.py @@ -4,6 +4,7 @@ File contains util functions. from datetime import datetime +import nextcord from sqlalchemy import select from sqlalchemy.orm import Session @@ -106,12 +107,13 @@ def get_current_cycle(config: DakinyConfig) -> int: return None -def build_votes_str(config: DakinyConfig) -> str: +def build_votes_str(config: DakinyConfig, guild: nextcord.Guild) -> str: """ Creates a string that contains all nominees and their votes to be sent as a discord message. Args: config (DakinyConfig): Daking config object + guild (nextcord.Guild): The guild object. Used to get display names for sorting. Returns: str: String that contains all nominees and their votes. @@ -124,7 +126,10 @@ def build_votes_str(config: DakinyConfig) -> str: CouncilMembers.ElectionCycle == get_election_cycle_name(config) ) - for nominee in session.scalars(stmt): + for nominee in sorted( + session.scalars(stmt), + key=lambda nominee: guild.get_member(nominee.MemberId).display_name, + ): votes[nominee.MemberId] = 0 stmt = select(Votes).where( @@ -141,7 +146,7 @@ def build_votes_str(config: DakinyConfig) -> str: return votes_str -def build_highcouncil_votes_str(config: DakinyConfig) -> str: +def build_highcouncil_votes_str(config: DakinyConfig, guild: nextcord.Guild) -> str: """ Creates a string that contains all high council nominees and their votes to be sent as a discord message. @@ -150,6 +155,7 @@ def build_highcouncil_votes_str(config: DakinyConfig) -> str: Returns: str: String that contains all nominees and their votes. + guild (nextcord.Guild): The guild object. Used to get display names for sorting. """ cl_votes: dict[int, int] = {} @@ -162,7 +168,10 @@ def build_highcouncil_votes_str(config: DakinyConfig) -> str: .filter(CouncilMembers.ClanLeaderSpeechId.is_not(None)) ) - for nominee in session.scalars(stmt): + for nominee in sorted( + session.scalars(stmt), + key=lambda nominee: guild.get_member(nominee.MemberId).display_name, + ): cl_votes[nominee.MemberId] = 0 stmt = select(Votes).where(Votes.ElectionCycle == get_cl_election_name(config)) @@ -177,7 +186,10 @@ def build_highcouncil_votes_str(config: DakinyConfig) -> str: .filter(CouncilMembers.DiscordManagerSpeechId.is_not(None)) ) - for nominee in session.scalars(stmt): + for nominee in sorted( + session.scalars(stmt), + key=lambda nominee: guild.get_member(nominee.MemberId).display_name, + ): dm_votes[nominee.MemberId] = 0 stmt = select(Votes).where(Votes.ElectionCycle == get_dm_election_name(config))