Change ElectionCycle class to Election. Change ElectionCycle column in database to ElectionName. Update some variables for clarification.

This commit is contained in:
2024-09-03 11:24:28 -04:00
parent 306e0d687a
commit eb5102578d
10 changed files with 71 additions and 79 deletions
@@ -31,9 +31,9 @@ from utils import (
REQUIRED_VOTES_TO_BE_ELECTED = 3 REQUIRED_VOTES_TO_BE_ELECTED = 3
class ElectionCycle: class Election:
""" """
ElectionCycle class. Election class.
This class represents all the dates required for an election cycle along with all the functions needed to operate This class represents all the dates required for an election cycle along with all the functions needed to operate
the cycle. the cycle.
@@ -183,7 +183,7 @@ class ElectionCycle:
# create new entry into database. # create new entry into database.
with Session(engine) as session: with Session(engine) as session:
new_cycle = ElectionInformation( new_cycle = ElectionInformation(
ElectionCycle=quarter_name, ElectionName=quarter_name,
ThreadId=thread.id, ThreadId=thread.id,
CouncilNominationMsgId=nominate_msg.id, CouncilNominationMsgId=nominate_msg.id,
) )
@@ -206,9 +206,7 @@ class ElectionCycle:
) )
with Session(engine) as session: with Session(engine) as session:
stmt = select(ElectionInformation).where( stmt = select(ElectionInformation).where(ElectionInformation.ElectionName == get_election_name(self.config))
ElectionInformation.ElectionCycle == get_election_name(self.config)
)
info = session.scalar(stmt) info = session.scalar(stmt)
info.CouncilVoteMsgId = vote_msg.id info.CouncilVoteMsgId = vote_msg.id
session.commit() session.commit()
@@ -231,7 +229,7 @@ class ElectionCycle:
with Session(engine) as session: with Session(engine) as session:
# remove vote button from vote message # remove vote button from vote message
stmt = select(ElectionInformation).where(ElectionInformation.ElectionCycle == election_name) stmt = select(ElectionInformation).where(ElectionInformation.ElectionName == election_name)
info = session.scalar(stmt) info = session.scalar(stmt)
vote_msg: nextcord.Message = await election_thread.fetch_message(info.CouncilVoteMsgId) vote_msg: nextcord.Message = await election_thread.fetch_message(info.CouncilVoteMsgId)
await vote_msg.edit(view=None) await vote_msg.edit(view=None)
@@ -243,7 +241,7 @@ class ElectionCycle:
# add the highcouncil role to everyone with 3 or more votes. set elected to true in database # add the highcouncil role to everyone with 3 or more votes. set elected to true in database
stmt = ( stmt = (
select(Votes.VotedForId) select(Votes.VotedForId)
.where(Votes.ElectionCycle == election_name) .where(Votes.ElectionName == election_name)
.where(Votes.Type == VoteType.COUNCIL) .where(Votes.Type == VoteType.COUNCIL)
) )
vote_count = collections.Counter(session.scalars(stmt)) vote_count = collections.Counter(session.scalars(stmt))
@@ -252,7 +250,7 @@ class ElectionCycle:
if votes >= REQUIRED_VOTES_TO_BE_ELECTED: if votes >= REQUIRED_VOTES_TO_BE_ELECTED:
stmt = ( stmt = (
select(CouncilMembers) select(CouncilMembers)
.where(CouncilMembers.ElectionCycle == election_name) .where(CouncilMembers.ElectionName == election_name)
.where(CouncilMembers.MemberId == user_id) .where(CouncilMembers.MemberId == user_id)
) )
council_member = session.scalar(stmt) council_member = session.scalar(stmt)
@@ -277,9 +275,7 @@ class ElectionCycle:
) )
with Session(engine) as session: with Session(engine) as session:
stmt = select(ElectionInformation).where( stmt = select(ElectionInformation).where(ElectionInformation.ElectionName == get_election_name(self.config))
ElectionInformation.ElectionCycle == get_election_name(self.config)
)
info = session.scalar(stmt) info = session.scalar(stmt)
info.HighCouncilVoteMsgId = vote_msg.id info.HighCouncilVoteMsgId = vote_msg.id
session.commit() session.commit()
@@ -295,13 +291,13 @@ class ElectionCycle:
elect_role: nextcord.Role = self.guild.get_role(self.config.params.clan_council_elect_role_id) elect_role: nextcord.Role = self.guild.get_role(self.config.params.clan_council_elect_role_id)
council_role: nextcord.Role = self.guild.get_role(self.config.params.clan_council_role_id) council_role: nextcord.Role = self.guild.get_role(self.config.params.clan_council_role_id)
election_cycle = get_election_name(self.config) election_name = get_election_name(self.config)
with Session(engine) as session: with Session(engine) as session:
# role management # role management
stmt = ( stmt = (
select(CouncilMembers.MemberId) select(CouncilMembers.MemberId)
.where(CouncilMembers.ElectionCycle == election_cycle) .where(CouncilMembers.ElectionName == election_name)
.where(CouncilMembers.Elected == true()) .where(CouncilMembers.Elected == true())
) )
@@ -320,13 +316,13 @@ class ElectionCycle:
# ties are undefined behavior. # ties are undefined behavior.
stmt = ( stmt = (
select(Votes.VotedForId) select(Votes.VotedForId)
.where(Votes.ElectionCycle == election_cycle) .where(Votes.ElectionName == election_name)
.where(Votes.Type == VoteType.DISCORDMANAGER) .where(Votes.Type == VoteType.DISCORDMANAGER)
) )
dm_votes = collections.Counter(session.scalars(stmt)) dm_votes = collections.Counter(session.scalars(stmt))
stmt = ( stmt = (
select(Votes.VotedForId) select(Votes.VotedForId)
.where(Votes.ElectionCycle == election_cycle) .where(Votes.ElectionName == election_name)
.where(Votes.Type == VoteType.CLANLEADER) .where(Votes.Type == VoteType.CLANLEADER)
) )
cl_votes = collections.Counter(session.scalars(stmt)) cl_votes = collections.Counter(session.scalars(stmt))
@@ -335,7 +331,7 @@ class ElectionCycle:
dm_winner = max(dm_votes, key=dm_votes.get) dm_winner = max(dm_votes, key=dm_votes.get)
stmt = ( stmt = (
update(CouncilMembers) update(CouncilMembers)
.where(CouncilMembers.ElectionCycle == election_cycle) .where(CouncilMembers.ElectionName == election_name)
.where(CouncilMembers.MemberId == dm_winner) .where(CouncilMembers.MemberId == dm_winner)
.values(DiscordManager=True) .values(DiscordManager=True)
) )
@@ -346,7 +342,7 @@ class ElectionCycle:
cl_winner = max(cl_votes, key=cl_votes.get) cl_winner = max(cl_votes, key=cl_votes.get)
stmt = ( stmt = (
update(CouncilMembers) update(CouncilMembers)
.where(CouncilMembers.ElectionCycle == election_cycle) .where(CouncilMembers.ElectionName == election_name)
.where(CouncilMembers.MemberId == cl_winner) .where(CouncilMembers.MemberId == cl_winner)
.values(ClanLeader=True) .values(ClanLeader=True)
) )
@@ -360,7 +356,7 @@ class ElectionCycle:
# clear view from vote message # clear view from vote message
stmt = select(ElectionInformation.HighCouncilVoteMsgId).where( stmt = select(ElectionInformation.HighCouncilVoteMsgId).where(
ElectionInformation.ElectionCycle == get_election_name(self.config) ElectionInformation.ElectionName == get_election_name(self.config)
) )
vote_msg_id = session.scalar(stmt) vote_msg_id = session.scalar(stmt)
vote_msg: nextcord.Message = await election_thread.fetch_message(vote_msg_id) vote_msg: nextcord.Message = await election_thread.fetch_message(vote_msg_id)
@@ -383,9 +379,7 @@ class ElectionCycle:
ElectionInformation: The information from the database about the current election. ElectionInformation: The information from the database about the current election.
""" """
with Session(engine) as session: with Session(engine) as session:
stmt = select(ElectionInformation).where( stmt = select(ElectionInformation).where(ElectionInformation.ElectionName == get_election_name(self.config))
ElectionInformation.ElectionCycle == get_election_name(self.config)
)
return session.scalar(stmt) return session.scalar(stmt)
async def election_reminder(self) -> None: async def election_reminder(self) -> None:
+8 -8
View File
@@ -58,10 +58,10 @@ class NominateDMModal(nextcord.ui.Modal):
with Session(engine) as session: with Session(engine) as session:
# user already in table # user already in table
cycle = get_election_name(self.config) election_name = get_election_name(self.config)
stmt = ( stmt = (
select(CouncilMembers) select(CouncilMembers)
.where(CouncilMembers.ElectionCycle == cycle) .where(CouncilMembers.ElectionName == election_name)
.where(CouncilMembers.MemberId == self.usr_id) .where(CouncilMembers.MemberId == self.usr_id)
) )
council_elect = session.scalar(stmt) council_elect = session.scalar(stmt)
@@ -120,10 +120,10 @@ class NominateCLModal(nextcord.ui.Modal):
with Session(engine) as session: with Session(engine) as session:
# user already in table # user already in table
cycle = get_election_name(self.config) election_name = get_election_name(self.config)
stmt = ( stmt = (
select(CouncilMembers) select(CouncilMembers)
.where(CouncilMembers.ElectionCycle == cycle) .where(CouncilMembers.ElectionName == election_name)
.where(CouncilMembers.MemberId == self.usr_id) .where(CouncilMembers.MemberId == self.usr_id)
) )
council_elect = session.scalar(stmt) council_elect = session.scalar(stmt)
@@ -170,13 +170,13 @@ class NominateHighCouncilView(nextcord.ui.View):
) )
return return
cycle = get_election_name(self.config) election_name = get_election_name(self.config)
usr_nominated = False usr_nominated = False
speech_content = "" speech_content = ""
with Session(engine) as session: with Session(engine) as session:
stmt = ( stmt = (
select(CouncilMembers) select(CouncilMembers)
.where(CouncilMembers.ElectionCycle == cycle) .where(CouncilMembers.ElectionName == election_name)
.where(CouncilMembers.MemberId == interaction.user.id) .where(CouncilMembers.MemberId == interaction.user.id)
) )
obj = session.scalar(stmt) obj = session.scalar(stmt)
@@ -215,13 +215,13 @@ class NominateHighCouncilView(nextcord.ui.View):
) )
return return
cycle = get_election_name(self.config) election_name = get_election_name(self.config)
usr_nominated = False usr_nominated = False
speech_content = "" speech_content = ""
with Session(engine) as session: with Session(engine) as session:
stmt = ( stmt = (
select(CouncilMembers) select(CouncilMembers)
.where(CouncilMembers.ElectionCycle == cycle) .where(CouncilMembers.ElectionName == election_name)
.where(CouncilMembers.MemberId == interaction.user.id) .where(CouncilMembers.MemberId == interaction.user.id)
) )
obj = session.scalar(stmt) obj = session.scalar(stmt)
+6 -6
View File
@@ -50,13 +50,14 @@ class NominateModal(nextcord.ui.Modal):
""" """
speech = decorate_nomination(interaction.user.id, self.speech.value) speech = decorate_nomination(interaction.user.id, self.speech.value)
election_name = get_election_name(self.config)
with Session(engine) as session: with Session(engine) as session:
if not self.usr_nominated: if not self.usr_nominated:
msg = await interaction.channel.send(speech) msg = await interaction.channel.send(speech)
council_member = CouncilMembers( council_member = CouncilMembers(
ElectionCycle=get_election_name(self.config), ElectionName=election_name,
MemberId=interaction.user.id, MemberId=interaction.user.id,
SpeechMsgId=msg.id, SpeechMsgId=msg.id,
) )
@@ -64,10 +65,9 @@ class NominateModal(nextcord.ui.Modal):
session.commit() session.commit()
else: else:
# user already nominated, just need to update the election speech # user already nominated, just need to update the election speech
cycle = get_election_name(self.config)
stmt = ( stmt = (
select(CouncilMembers) select(CouncilMembers)
.where(CouncilMembers.ElectionCycle == cycle) .where(CouncilMembers.ElectionName == election_name)
.where(CouncilMembers.MemberId == self.usr_id) .where(CouncilMembers.MemberId == self.usr_id)
) )
obj = session.scalar(stmt) obj = session.scalar(stmt)
@@ -91,7 +91,7 @@ class NominateView(nextcord.ui.View):
) )
async def nominate(self, button: nextcord.ui.Button, interaction: nextcord.Interaction) -> None: # noqa: ARG002 async def nominate(self, button: nextcord.ui.Button, interaction: nextcord.Interaction) -> None: # noqa: ARG002
""" """
Button to nominate onself for Clan Council. Button to nominate oneself for Clan Council.
Gathers previous information about the user if they have already nominated themselves, then launches Gathers previous information about the user if they have already nominated themselves, then launches
the nomination modal to get their speech. the nomination modal to get their speech.
@@ -108,13 +108,13 @@ class NominateView(nextcord.ui.View):
) )
return return
cycle = get_election_name(self.config) election_name = get_election_name(self.config)
usr_nominated = False usr_nominated = False
speech_content = "" speech_content = ""
with Session(engine) as session: with Session(engine) as session:
stmt = ( stmt = (
select(CouncilMembers) select(CouncilMembers)
.where(CouncilMembers.ElectionCycle == cycle) .where(CouncilMembers.ElectionName == election_name)
.where(CouncilMembers.MemberId == interaction.user.id) .where(CouncilMembers.MemberId == interaction.user.id)
) )
obj = session.scalar(stmt) obj = session.scalar(stmt)
+7 -7
View File
@@ -41,14 +41,14 @@ class VoteSelect(nextcord.ui.StringSelect):
election_name: str = get_election_name(self.config) election_name: str = get_election_name(self.config)
with Session(engine) as session: with Session(engine) as session:
stmt = select(ElectionInformation.HighCouncilVoteMsgId).where( stmt = select(ElectionInformation.HighCouncilVoteMsgId).where(
ElectionInformation.ElectionCycle == election_name ElectionInformation.ElectionName == election_name
) )
vote_msg_id = session.scalar(stmt) vote_msg_id = session.scalar(stmt)
voted_for = None if self.values[0] == "Abstain" else int(self.values[0]) voted_for = None if self.values[0] == "Abstain" else int(self.values[0])
new_vote = Votes( new_vote = Votes(
ElectionCycle=election_name, ElectionName=election_name,
VoterId=interaction.user.id, VoterId=interaction.user.id,
VotedForId=voted_for, VotedForId=voted_for,
Type=self.election_type, Type=self.election_type,
@@ -63,7 +63,7 @@ class VoteSelect(nextcord.ui.StringSelect):
session.rollback() session.rollback()
stmt = ( stmt = (
update(Votes) update(Votes)
.where(Votes.ElectionCycle == election_name) .where(Votes.ElectionName == election_name)
.where(Votes.VoterId == interaction.user.id) .where(Votes.VoterId == interaction.user.id)
.where(Votes.Type == self.election_type) .where(Votes.Type == self.election_type)
.values(VotedForId=voted_for) .values(VotedForId=voted_for)
@@ -118,7 +118,7 @@ class VoteHighcouncilView(nextcord.ui.View):
with Session(engine) as session: with Session(engine) as session:
stmt = ( stmt = (
select(Votes.VotedForId) select(Votes.VotedForId)
.where(Votes.ElectionCycle == election_name) .where(Votes.ElectionName == election_name)
.where(Votes.VoterId == interaction.user.id) .where(Votes.VoterId == interaction.user.id)
.where(Votes.Type == VoteType.CLANLEADER) .where(Votes.Type == VoteType.CLANLEADER)
) )
@@ -126,7 +126,7 @@ class VoteHighcouncilView(nextcord.ui.View):
stmt = ( stmt = (
select(CouncilMembers.MemberId) select(CouncilMembers.MemberId)
.where(CouncilMembers.ElectionCycle == election_name) .where(CouncilMembers.ElectionName == election_name)
.filter(CouncilMembers.ClanLeaderSpeechId.is_not(None)) .filter(CouncilMembers.ClanLeaderSpeechId.is_not(None))
) )
@@ -190,7 +190,7 @@ class VoteHighcouncilView(nextcord.ui.View):
with Session(engine) as session: with Session(engine) as session:
stmt = ( stmt = (
select(Votes.VotedForId) select(Votes.VotedForId)
.where(Votes.ElectionCycle == election_name) .where(Votes.ElectionName == election_name)
.where(Votes.VoterId == interaction.user.id) .where(Votes.VoterId == interaction.user.id)
.where(Votes.Type == VoteType.DISCORDMANAGER) .where(Votes.Type == VoteType.DISCORDMANAGER)
) )
@@ -198,7 +198,7 @@ class VoteHighcouncilView(nextcord.ui.View):
stmt = ( stmt = (
select(CouncilMembers.MemberId) select(CouncilMembers.MemberId)
.where(CouncilMembers.ElectionCycle == election_name) .where(CouncilMembers.ElectionName == election_name)
.filter(CouncilMembers.DiscordManagerSpeechId.is_not(None)) .filter(CouncilMembers.DiscordManagerSpeechId.is_not(None))
) )
+6 -8
View File
@@ -39,15 +39,13 @@ class VoteSelect(nextcord.ui.StringSelect):
""" """
election_name = get_election_name(self.config) election_name = get_election_name(self.config)
with Session(engine) as session: with Session(engine) as session:
stmt = select(ElectionInformation.CouncilVoteMsgId).where( stmt = select(ElectionInformation.CouncilVoteMsgId).where(ElectionInformation.ElectionName == election_name)
ElectionInformation.ElectionCycle == election_name
)
vote_msg_id = session.scalar(stmt) vote_msg_id = session.scalar(stmt)
voted_for = None if self.values[0] == "Abstain" else int(self.values[0]) voted_for = None if self.values[0] == "Abstain" else int(self.values[0])
new_vote = Votes( new_vote = Votes(
ElectionCycle=election_name, ElectionName=election_name,
VoterId=interaction.user.id, VoterId=interaction.user.id,
VotedForId=voted_for, VotedForId=voted_for,
Type=VoteType.COUNCIL, Type=VoteType.COUNCIL,
@@ -61,7 +59,7 @@ class VoteSelect(nextcord.ui.StringSelect):
session.rollback() session.rollback()
stmt = ( stmt = (
update(Votes) update(Votes)
.where(Votes.ElectionCycle == election_name) .where(Votes.ElectionName == election_name)
.where(Votes.VoterId == interaction.user.id) .where(Votes.VoterId == interaction.user.id)
.where(Votes.Type == VoteType.COUNCIL) .where(Votes.Type == VoteType.COUNCIL)
.values(VotedForId=voted_for) .values(VotedForId=voted_for)
@@ -106,18 +104,18 @@ class VoteView(nextcord.ui.View):
) )
return return
election: str = get_election_name(self.config) election_name: str = get_election_name(self.config)
with Session(engine) as session: with Session(engine) as session:
stmt = ( stmt = (
select(Votes.VotedForId) select(Votes.VotedForId)
.where(Votes.ElectionCycle == election) .where(Votes.ElectionName == election_name)
.where(Votes.VoterId == interaction.user.id) .where(Votes.VoterId == interaction.user.id)
.where(Votes.Type == VoteType.COUNCIL) .where(Votes.Type == VoteType.COUNCIL)
) )
voted_for = session.scalar(stmt) voted_for = session.scalar(stmt)
stmt = select(CouncilMembers.MemberId).where(CouncilMembers.ElectionCycle == election) stmt = select(CouncilMembers.MemberId).where(CouncilMembers.ElectionName == election_name)
options = [ options = [
SelectOption( SelectOption(
+8 -8
View File
@@ -5,7 +5,7 @@ from typing import TYPE_CHECKING
import nextcord import nextcord
from nextcord.ext import application_checks, commands from nextcord.ext import application_checks, commands
from classes.election_cycle import ElectionCycle from classes.election import Election
from utils import GUILDS, get_current_election_cycle from utils import GUILDS, get_current_election_cycle
if TYPE_CHECKING: if TYPE_CHECKING:
@@ -45,7 +45,7 @@ class TestCog(commands.Cog, name="Test commands."):
interaction (nextcord.Interaction): Interaction with the message. interaction (nextcord.Interaction): Interaction with the message.
""" """
config: DakinyConfig = self.bot.config config: DakinyConfig = self.bot.config
cycle = ElectionCycle( cycle = Election(
get_current_election_cycle(config), get_current_election_cycle(config),
config, config,
self.bot.get_guild(config.params.guild_id), self.bot.get_guild(config.params.guild_id),
@@ -63,7 +63,7 @@ class TestCog(commands.Cog, name="Test commands."):
interaction (nextcord.Interaction): Interaction with the message. interaction (nextcord.Interaction): Interaction with the message.
""" """
config: DakinyConfig = self.bot.config config: DakinyConfig = self.bot.config
cycle = ElectionCycle( cycle = Election(
get_current_election_cycle(config), get_current_election_cycle(config),
config, config,
self.bot.get_guild(config.params.guild_id), self.bot.get_guild(config.params.guild_id),
@@ -81,7 +81,7 @@ class TestCog(commands.Cog, name="Test commands."):
interaction (nextcord.Interaction): Interaction with the message. interaction (nextcord.Interaction): Interaction with the message.
""" """
config: DakinyConfig = self.bot.config config: DakinyConfig = self.bot.config
cycle = ElectionCycle( cycle = Election(
get_current_election_cycle(config), get_current_election_cycle(config),
config, config,
self.bot.get_guild(config.params.guild_id), self.bot.get_guild(config.params.guild_id),
@@ -99,7 +99,7 @@ class TestCog(commands.Cog, name="Test commands."):
interaction (nextcord.Interaction): Interaction with the message. interaction (nextcord.Interaction): Interaction with the message.
""" """
config: DakinyConfig = self.bot.config config: DakinyConfig = self.bot.config
cycle = ElectionCycle( cycle = Election(
get_current_election_cycle(config), get_current_election_cycle(config),
config, config,
self.bot.get_guild(config.params.guild_id), self.bot.get_guild(config.params.guild_id),
@@ -117,7 +117,7 @@ class TestCog(commands.Cog, name="Test commands."):
interaction (nextcord.Interaction): Interaction with the message. interaction (nextcord.Interaction): Interaction with the message.
""" """
config: DakinyConfig = self.bot.config config: DakinyConfig = self.bot.config
cycle = ElectionCycle( cycle = Election(
get_current_election_cycle(config), get_current_election_cycle(config),
config, config,
self.bot.get_guild(config.params.guild_id), self.bot.get_guild(config.params.guild_id),
@@ -135,7 +135,7 @@ class TestCog(commands.Cog, name="Test commands."):
interaction (nextcord.Interaction): Interaction with the message. interaction (nextcord.Interaction): Interaction with the message.
""" """
config: DakinyConfig = self.bot.config config: DakinyConfig = self.bot.config
cycle = ElectionCycle( cycle = Election(
get_current_election_cycle(config), get_current_election_cycle(config),
config, config,
self.bot.get_guild(config.params.guild_id), self.bot.get_guild(config.params.guild_id),
@@ -153,7 +153,7 @@ class TestCog(commands.Cog, name="Test commands."):
interaction (nextcord.Interaction): Interaction with the message. interaction (nextcord.Interaction): Interaction with the message.
""" """
config: DakinyConfig = self.bot.config config: DakinyConfig = self.bot.config
cycle = ElectionCycle( cycle = Election(
get_current_election_cycle(config), get_current_election_cycle(config),
config, config,
self.bot.get_guild(config.params.guild_id), self.bot.get_guild(config.params.guild_id),
+2 -2
View File
@@ -149,7 +149,7 @@ class ViewCog(commands.Cog, name="View Commands"):
election_name (str): Is SlashOption. The name of the election to view votes for. election_name (str): Is SlashOption. The name of the election to view votes for.
election_type(str): Is SlashOption. The type of election to search for. Choice from StrEnum VoteType. election_type(str): Is SlashOption. The type of election to search for. Choice from StrEnum VoteType.
""" """
stmt = select(Votes).where(Votes.ElectionCycle == election_name).where(Votes.Type == election_type) stmt = select(Votes).where(Votes.ElectionName == election_name).where(Votes.Type == election_type)
with Session(engine) as session: with Session(engine) as session:
# Check if election exists. # Check if election exists.
@@ -178,7 +178,7 @@ class ViewCog(commands.Cog, name="View Commands"):
election_name (str): String to run autocomplete on. election_name (str): String to run autocomplete on.
""" """
with Session(engine) as session: with Session(engine) as session:
stmt = select(Votes.ElectionCycle).distinct() stmt = select(Votes.ElectionName).distinct()
elections = session.scalars(stmt) elections = session.scalars(stmt)
# if empty compare string # if empty compare string
+6 -6
View File
@@ -9,7 +9,7 @@ from pytz import timezone
from sqlalchemy import select from sqlalchemy import select
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from classes.election_cycle import ElectionCycle from classes.election import Election
from database_setup.sql_data_classes import UserBirthday, engine from database_setup.sql_data_classes import UserBirthday, engine
from main import birthday_list from main import birthday_list
from utils import get_current_election_cycle from utils import get_current_election_cycle
@@ -43,7 +43,7 @@ class DailyLoop(commands.Cog, name="Daily Loop"):
election is over to delete the current election object. election is over to delete the current election object.
""" """
if self.current_election is None: if self.current_election is None:
self.current_election = self.get_election_cycle() self.current_election = self.get_election()
if self.current_election is not None: if self.current_election is not None:
await self.current_election.check_date() await self.current_election.check_date()
@@ -65,16 +65,16 @@ class DailyLoop(commands.Cog, name="Daily Loop"):
await self.bot.wait_until_ready() await self.bot.wait_until_ready()
# Bot needs to know guilds to get the current cycle. # Bot needs to know guilds to get the current cycle.
self.current_election = self.get_election_cycle() self.current_election = self.get_election()
def get_election_cycle(self) -> ElectionCycle: def get_election(self) -> Election:
""" """
Creates an object for the current election cycle if one is active. Creates an object for the current election cycle if one is active.
An election is considered active between the election start and the term start. An election is considered active between the election start and the term start.
Returns: Returns:
ElectionCycle: Current election cycle, None if there is no election happening. Election: Current election cycle, None if there is no election happening.
""" """
config: DakinyConfig = self.bot.config config: DakinyConfig = self.bot.config
cycle = get_current_election_cycle(config) cycle = get_current_election_cycle(config)
@@ -83,7 +83,7 @@ class DailyLoop(commands.Cog, name="Daily Loop"):
if cycle is None: if cycle is None:
return None return None
return ElectionCycle(cycle, config, self.bot.get_guild(config.params.guild_id)) return Election(cycle, config, self.bot.get_guild(config.params.guild_id))
def update_birthdays(self) -> None: def update_birthdays(self) -> None:
"""Goes through database to collect all birthdays that are today. Adds user's id to birthday list.""" """Goes through database to collect all birthdays that are today. Adds user's id to birthday list."""
+2 -2
View File
@@ -27,7 +27,7 @@ class Params:
@dataclass @dataclass
class ElectionCycle: class Election:
"""Class defines relevant election dates.""" """Class defines relevant election dates."""
election_start: str election_start: str
@@ -45,5 +45,5 @@ class ElectionCycle:
class DakinyConfig: class DakinyConfig:
"""Class defines all configs required for Dakiny Bot.""" """Class defines all configs required for Dakiny Bot."""
election_cycles: list[ElectionCycle] election_cycles: list[Election]
params: Params params: Params
+10 -10
View File
@@ -148,7 +148,7 @@ def build_votes_str(config: DakinyConfig, guild: nextcord.Guild) -> str:
election_name = get_election_name(config) election_name = get_election_name(config)
with Session(engine) as session: with Session(engine) as session:
stmt = select(CouncilMembers).where(CouncilMembers.ElectionCycle == election_name) stmt = select(CouncilMembers).where(CouncilMembers.ElectionName == election_name)
for nominee in sorted( for nominee in sorted(
session.scalars(stmt), session.scalars(stmt),
@@ -156,7 +156,7 @@ def build_votes_str(config: DakinyConfig, guild: nextcord.Guild) -> str:
): ):
votes[nominee.MemberId] = 0 votes[nominee.MemberId] = 0
stmt = select(Votes).where(Votes.ElectionCycle == election_name).where(Votes.Type == VoteType.COUNCIL) stmt = select(Votes).where(Votes.ElectionName == election_name).where(Votes.Type == VoteType.COUNCIL)
for vote in session.scalars(stmt): for vote in session.scalars(stmt):
if vote.VotedForId is not None: if vote.VotedForId is not None:
@@ -187,7 +187,7 @@ def build_highcouncil_votes_str(config: DakinyConfig, guild: nextcord.Guild) ->
with Session(engine) as session: with Session(engine) as session:
stmt = ( stmt = (
select(CouncilMembers) select(CouncilMembers)
.where(CouncilMembers.ElectionCycle == election_name) .where(CouncilMembers.ElectionName == election_name)
.filter(CouncilMembers.ClanLeaderSpeechId.is_not(None)) .filter(CouncilMembers.ClanLeaderSpeechId.is_not(None))
) )
@@ -197,7 +197,7 @@ def build_highcouncil_votes_str(config: DakinyConfig, guild: nextcord.Guild) ->
): ):
cl_votes[nominee.MemberId] = 0 cl_votes[nominee.MemberId] = 0
stmt = select(Votes).where(Votes.ElectionCycle == election_name).where(Votes.Type == VoteType.CLANLEADER) stmt = select(Votes).where(Votes.ElectionName == election_name).where(Votes.Type == VoteType.CLANLEADER)
for vote in session.scalars(stmt): for vote in session.scalars(stmt):
if vote.VotedForId is not None: if vote.VotedForId is not None:
@@ -205,7 +205,7 @@ def build_highcouncil_votes_str(config: DakinyConfig, guild: nextcord.Guild) ->
stmt = ( stmt = (
select(CouncilMembers) select(CouncilMembers)
.where(CouncilMembers.ElectionCycle == election_name) .where(CouncilMembers.ElectionName == election_name)
.filter(CouncilMembers.DiscordManagerSpeechId.is_not(None)) .filter(CouncilMembers.DiscordManagerSpeechId.is_not(None))
) )
@@ -215,7 +215,7 @@ def build_highcouncil_votes_str(config: DakinyConfig, guild: nextcord.Guild) ->
): ):
dm_votes[nominee.MemberId] = 0 dm_votes[nominee.MemberId] = 0
stmt = select(Votes).where(Votes.ElectionCycle == election_name).where(Votes.Type == VoteType.DISCORDMANAGER) stmt = select(Votes).where(Votes.ElectionName == election_name).where(Votes.Type == VoteType.DISCORDMANAGER)
for vote in session.scalars(stmt): for vote in session.scalars(stmt):
if vote.VotedForId is not None: if vote.VotedForId is not None:
@@ -512,23 +512,23 @@ async def edit_clan_council_info(guild: nextcord.Guild, config: DakinyConfig) ->
""" """
clan_info_channel: nextcord.TextChannel = await guild.fetch_channel(config.params.clan_info_channel_id) clan_info_channel: nextcord.TextChannel = await guild.fetch_channel(config.params.clan_info_channel_id)
election = get_election_name(config) election_name = get_election_name(config)
with Session(engine) as session: with Session(engine) as session:
stmt = ( stmt = (
select(CouncilMembers.MemberId) select(CouncilMembers.MemberId)
.where(CouncilMembers.ElectionCycle == election) .where(CouncilMembers.ElectionName == election_name)
.where(CouncilMembers.DiscordManager == true()) .where(CouncilMembers.DiscordManager == true())
) )
discord_manager = session.scalar(stmt) discord_manager = session.scalar(stmt)
stmt = ( stmt = (
select(CouncilMembers.MemberId) select(CouncilMembers.MemberId)
.where(CouncilMembers.ElectionCycle == election) .where(CouncilMembers.ElectionName == election_name)
.where(CouncilMembers.ClanLeader == true()) .where(CouncilMembers.ClanLeader == true())
) )
clan_leader = session.scalar(stmt) clan_leader = session.scalar(stmt)
stmt = ( stmt = (
select(CouncilMembers.MemberId) select(CouncilMembers.MemberId)
.where(CouncilMembers.ElectionCycle == election) .where(CouncilMembers.ElectionName == election_name)
.where(CouncilMembers.Elected == true()) .where(CouncilMembers.Elected == true())
) )