From 647af0353d427f544e142a218e118aa5d90d19a3 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Thu, 2 Nov 2023 14:44:26 -0400 Subject: [PATCH] move functions to get election cycle index and name into utils. --- classes/election_cycle.py | 6 ++++-- cogs/loops/daily_loop.py | 20 ++++++-------------- utils.py | 28 ++++++++++++++++++++++++++-- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/classes/election_cycle.py b/classes/election_cycle.py index 13c49b0..689b2fb 100644 --- a/classes/election_cycle.py +++ b/classes/election_cycle.py @@ -152,7 +152,7 @@ class ElectionCycle: """ # create new channel for election category: nextcord.CategoryChannel = self.guild.get_channel(self.config.params.active_category_id) - quarter_name: str = get_election_cycle_name() + quarter_name: str = get_election_cycle_name(self.config) # create new entry into database. election_channel: nextcord.TextChannel = await category.create_text_channel(quarter_name) @@ -226,5 +226,7 @@ class ElectionCycle: """ with Session(engine) as session: - stmt = select(ElectionInformation).where(ElectionInformation.ElectionCycle == get_election_cycle_name()) + stmt = select(ElectionInformation).where( + ElectionInformation.ElectionCycle == get_election_cycle_name(self.config) + ) return session.scalar(stmt) diff --git a/cogs/loops/daily_loop.py b/cogs/loops/daily_loop.py index 30ed4f7..ffb8caf 100644 --- a/cogs/loops/daily_loop.py +++ b/cogs/loops/daily_loop.py @@ -15,7 +15,7 @@ from classes.election_cycle import ElectionCycle from config import DakinyConfig from dakiny_bot import birthday_list from database_setup.sql_data_classes import UserBirthday, engine -from utils import get_datetime +from utils import get_current_cycle class DailyLoop(commands.Cog, name="Daily Loop"): @@ -40,7 +40,7 @@ class DailyLoop(commands.Cog, name="Daily Loop"): """ if self.current_election is None: - self.current_election = self.get_current_cycle() + self.current_election = self.get_election_cycle() if self.current_election is not None: await self.current_election.check_date() @@ -65,9 +65,9 @@ class DailyLoop(commands.Cog, name="Daily Loop"): await self.bot.wait_until_ready() # Bot needs to know guilds to get the current cycle. - self.current_election = self.get_current_cycle() + self.current_election = self.get_election_cycle() - def get_current_cycle(self) -> ElectionCycle: + def get_election_cycle(self) -> ElectionCycle: """ 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. @@ -76,18 +76,10 @@ class DailyLoop(commands.Cog, name="Daily Loop"): ElectionCycle: Current election cycle, None if there is no election happening. """ - today = datetime.today() config: DakinyConfig = self.bot.config - current_election = None + cycle = get_current_cycle(config) - for cycle, election in enumerate(config.election_cycles): - if ( - today.date() >= get_datetime(election.election_start).date() - and today.date() <= get_datetime(election.term_start).date() - ): - current_election = ElectionCycle(cycle, config, self.bot.get_guild(config.params.guild_id)) - - return current_election + return ElectionCycle(cycle, config, self.bot.get_guild(config.params.guild_id)) def update_birthdays(self) -> None: """ diff --git a/utils.py b/utils.py index c046f7e..d36ab45 100644 --- a/utils.py +++ b/utils.py @@ -35,6 +35,30 @@ def get_election_cycle_name(config: DakinyConfig) -> str: Returns: str: the name of the election cycle """ - # TODO finish + cycle = get_current_cycle(config) + current_cycle = config.election_cycles[cycle] - return f"vote-{election_start.year}-{election_cycle.name}" + return f"vote-{get_datetime(current_cycle.election_start).year}-{current_cycle.name}" + + +def get_current_cycle(config: DakinyConfig) -> int: + """ + Returns index for active election. An election is considered active between the election start and the term start. + + Args: + config (DakinyConfig): Config object to use + + Returns: + int: cycle index for active election. -1 for no active cycle + """ + + today = datetime.today() + + for cycle, election in enumerate(config.election_cycles): + if ( + today.date() >= get_datetime(election.election_start).date() + and today.date() <= get_datetime(election.term_start).date() + ): + return cycle + + return -1