move functions to get election cycle index and name into utils.
build_develop / build (push) Successful in 1m52s

This commit is contained in:
2023-11-02 14:44:26 -04:00
parent 63e3b3edb2
commit 647af0353d
3 changed files with 36 additions and 18 deletions
+4 -2
View File
@@ -152,7 +152,7 @@ class ElectionCycle:
""" """
# create new channel for election # create new channel for election
category: nextcord.CategoryChannel = self.guild.get_channel(self.config.params.active_category_id) 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. # create new entry into database.
election_channel: nextcord.TextChannel = await category.create_text_channel(quarter_name) election_channel: nextcord.TextChannel = await category.create_text_channel(quarter_name)
@@ -226,5 +226,7 @@ class ElectionCycle:
""" """
with Session(engine) as session: 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) return session.scalar(stmt)
+6 -14
View File
@@ -15,7 +15,7 @@ from classes.election_cycle import ElectionCycle
from config import DakinyConfig from config import DakinyConfig
from dakiny_bot import birthday_list from dakiny_bot import birthday_list
from database_setup.sql_data_classes import UserBirthday, engine 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"): class DailyLoop(commands.Cog, name="Daily Loop"):
@@ -40,7 +40,7 @@ class DailyLoop(commands.Cog, name="Daily Loop"):
""" """
if self.current_election is None: 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: if self.current_election is not None:
await self.current_election.check_date() await self.current_election.check_date()
@@ -65,9 +65,9 @@ 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_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 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. 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. ElectionCycle: Current election cycle, None if there is no election happening.
""" """
today = datetime.today()
config: DakinyConfig = self.bot.config config: DakinyConfig = self.bot.config
current_election = None cycle = get_current_cycle(config)
for cycle, election in enumerate(config.election_cycles): return ElectionCycle(cycle, config, self.bot.get_guild(config.params.guild_id))
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
def update_birthdays(self) -> None: def update_birthdays(self) -> None:
""" """
+26 -2
View File
@@ -35,6 +35,30 @@ def get_election_cycle_name(config: DakinyConfig) -> str:
Returns: Returns:
str: the name of the election cycle 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