Add sqlalchemy connection to database. Store election information.

This commit is contained in:
2023-07-16 22:54:04 -04:00
parent fc9eb7b151
commit e5e638edc9
6 changed files with 40 additions and 12 deletions
+19 -8
View File
@@ -6,8 +6,10 @@ from datetime import datetime, timedelta
import nextcord
from nextcord.ext import commands
from sqlalchemy.orm import Session
from config import DakinyConfig
from database.sql_data_classes import ElectionInformation, engine
from utils import get_datetime
@@ -105,7 +107,6 @@ class ElectionCycle:
Function checks current date to see if something needs to be run.
"""
today = datetime.today()
print("checking the date")
# check all dates if action is needed
if today.date() == self.election_start.date():
@@ -120,26 +121,26 @@ class ElectionCycle:
await self.term_start_action()
elif today.date() == self.term_end.date():
await self.term_end_action()
else:
print("today was not a special date")
print(today)
async def election_start_action(self) -> None:
"""
Performs required actions for when the election cycle starts.
"""
print("election cycle has started")
# create new channel for election
category: nextcord.CategoryChannel = self.bot.get_channel(self.config.params.active_category_id)
quarter_name: str = str(datetime.today().year) + "-" + self.elecion_cycle.name + "-election"
# TODO need to store this id in the sqlite database
quarter_name: str = self.get_election_cycle_name()
# create new entry into database.
election_channel = await category.create_text_channel(quarter_name)
with Session(engine) as session:
new_cycle = ElectionInformation(ElectionCycle=quarter_name, ElectionChannel=election_channel.id)
session.add(new_cycle)
session.commit()
# post message announcing election
# election start message is returned as an array of string because it is too long. send each string
# individually.
messages = self.build_election_announcement()
for message in messages:
await election_channel.send(message)
@@ -180,3 +181,13 @@ class ElectionCycle:
"""
imcompiling: nextcord.User = await self.bot.fetch_user(206602713511886849)
await imcompiling.send(msg)
def get_election_cycle_name(self) -> str:
"""
Builds the unique name of the current election cycle
Returns:
str: the name of the election cycle
"""
return f"{self.election_start.year} {self.elecion_cycle.name} election"
+1 -1
View File
@@ -6,7 +6,7 @@ import nextcord
from hydra.core.config_store import ConfigStore
from nextcord.ext import commands
import create_database
import database.create_database as create_database
from config import DakinyConfig
cs = ConfigStore.instance()
@@ -9,7 +9,7 @@ if not exists("dakiny.db"):
with sqlite3.connect("dakiny.db") as con:
cur = con.cursor()
with open("database.sql", mode="r", encoding="utf-8") as file:
with open("database/database.sql", mode="r", encoding="utf-8") as file:
database_script = file.read()
cur.executescript(database_script)
con.commit()
+1 -1
View File
@@ -3,7 +3,7 @@
CREATE TABLE ElectionInformation(
ElectionCycle varchar(30) not null unique,
ElectionChannel bigint unsigned,
ElectionChannel bigint unsigned not null,
PRIMARY KEY (ElectionCycle)
);
+16
View File
@@ -0,0 +1,16 @@
"""
Simple file to set up the sqlalchemy connection to database.
"""
from sqlalchemy import create_engine
from sqlalchemy.ext.automap import automap_base
# connect with sql alchemy and autoload schema
URL = "sqlite+pysqlite:///dakiny.db"
engine = create_engine(URL)
Base = automap_base()
Base.prepare(autoload_with=engine)
# define the classes
ElectionInformation = Base.classes.ElectionInformation
CouncilMembers = Base.classes.CouncilMembers
Votes = Base.classes.Votes
+2 -1
View File
@@ -1,4 +1,5 @@
nextcord==2.4.2
python-dotenv==1.0.0
pytz==2022.7.1
hydra-core==1.3.2
hydra-core==1.3.2
sqlalchemy<2.0