Change to sqlalchemy and orm scheme. Remove classes due to orm. Edit requirements.
This commit is contained in:
@@ -1,79 +0,0 @@
|
|||||||
"""File for Category class."""
|
|
||||||
|
|
||||||
|
|
||||||
class Category:
|
|
||||||
"""Stores a category's information. Used just to pass sql query information around."""
|
|
||||||
|
|
||||||
def __init__(self, category_info: tuple) -> None:
|
|
||||||
"""
|
|
||||||
Default constructor for Category class. Populates class from sql query results. Tuple order is important.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
name (str): Category's name
|
|
||||||
"""
|
|
||||||
self._id: int = category_info[0]
|
|
||||||
self._name: str = category_info[1]
|
|
||||||
self._group_id: int = category_info[2]
|
|
||||||
self._is_ascending: bool = category_info[3]
|
|
||||||
self._is_time_record: bool = category_info[4]
|
|
||||||
self._is_validated: bool = category_info[5]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def id(self) -> int:
|
|
||||||
"""
|
|
||||||
Getter for id.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
int: id
|
|
||||||
"""
|
|
||||||
return self._id
|
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self) -> str:
|
|
||||||
"""
|
|
||||||
Getter for name.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
str: name
|
|
||||||
"""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
|
||||||
def group_id(self) -> int:
|
|
||||||
"""
|
|
||||||
Getter for group id.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
int: group id
|
|
||||||
"""
|
|
||||||
return self._group_id
|
|
||||||
|
|
||||||
@property
|
|
||||||
def is_ascending(self) -> bool:
|
|
||||||
"""
|
|
||||||
Getter for is ascending.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
bool: is ascending
|
|
||||||
"""
|
|
||||||
return self._is_ascending
|
|
||||||
|
|
||||||
@property
|
|
||||||
def is_time_record(self) -> bool:
|
|
||||||
"""
|
|
||||||
Getter for is time record.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
bool: is time record
|
|
||||||
"""
|
|
||||||
return self._is_time_record
|
|
||||||
|
|
||||||
@property
|
|
||||||
def is_validated(self) -> bool:
|
|
||||||
"""
|
|
||||||
Getter for is validated.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
bool: is validated
|
|
||||||
"""
|
|
||||||
return self._is_validated
|
|
||||||
@@ -1,90 +0,0 @@
|
|||||||
"""File for Group class."""
|
|
||||||
|
|
||||||
|
|
||||||
class Group:
|
|
||||||
"""Stores a group's information. Used just to pass sql query information around."""
|
|
||||||
|
|
||||||
def __init__(self, group_info: tuple) -> None:
|
|
||||||
"""
|
|
||||||
Default constructor for group class. Populates class from sql query results. Tuple order is important.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
name (str): group's name
|
|
||||||
"""
|
|
||||||
self._id: int = group_info[0]
|
|
||||||
self._name: str = group_info[1]
|
|
||||||
self._channel_id: int = group_info[2]
|
|
||||||
self._guild_id: int = group_info[3]
|
|
||||||
self._message_id: int = group_info[4]
|
|
||||||
self._pad_length: int = group_info[5]
|
|
||||||
self._size = group_info[6]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def id(self) -> int:
|
|
||||||
"""
|
|
||||||
Getter for id
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
int: id
|
|
||||||
"""
|
|
||||||
return self._id
|
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self) -> str:
|
|
||||||
"""
|
|
||||||
Getter for name.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
str: name
|
|
||||||
"""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
|
||||||
def channel_id(self) -> int:
|
|
||||||
"""
|
|
||||||
Getter for channel id.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
int: channel id
|
|
||||||
"""
|
|
||||||
return self._channel_id
|
|
||||||
|
|
||||||
@property
|
|
||||||
def guild_id(self) -> int:
|
|
||||||
"""
|
|
||||||
Getter for guild id.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
int: guild id
|
|
||||||
"""
|
|
||||||
return self._guild_id
|
|
||||||
|
|
||||||
@property
|
|
||||||
def message_id(self) -> int:
|
|
||||||
"""
|
|
||||||
Getter for message id
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
int: message id
|
|
||||||
"""
|
|
||||||
return self._message_id
|
|
||||||
|
|
||||||
@property
|
|
||||||
def pad_length(self) -> int:
|
|
||||||
"""
|
|
||||||
Getter for pad length.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
int: pad length
|
|
||||||
"""
|
|
||||||
return self._pad_length
|
|
||||||
|
|
||||||
@property
|
|
||||||
def size(self) -> int:
|
|
||||||
"""
|
|
||||||
Getter for size of group.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
int: Size of group
|
|
||||||
"""
|
|
||||||
return self._size
|
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
"""File for Guild class."""
|
|
||||||
|
|
||||||
|
|
||||||
class Guild:
|
|
||||||
"""Stores a guild's information. Used just to pass sql query information around."""
|
|
||||||
|
|
||||||
def __init__(self, guild_info: tuple) -> None:
|
|
||||||
"""
|
|
||||||
Default constructor for Guild class. Populates class from sql query results. Tuple order is important.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
guild_id (int): Discord Guild id of new Guild
|
|
||||||
"""
|
|
||||||
self._id: int = guild_info[0]
|
|
||||||
self._submission_channel_id: int = guild_info[1]
|
|
||||||
self._username_length: int = guild_info[2]
|
|
||||||
self._verification_channel_id: int = guild_info[3]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def id(self) -> int:
|
|
||||||
"""
|
|
||||||
Getter for Guild id.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
int: Guild id
|
|
||||||
"""
|
|
||||||
return self._id
|
|
||||||
|
|
||||||
@property
|
|
||||||
def submission_channel(self) -> int:
|
|
||||||
"""
|
|
||||||
Getter for submission channel id.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
int: submission channel id
|
|
||||||
"""
|
|
||||||
return self._submission_channel_id
|
|
||||||
|
|
||||||
@property
|
|
||||||
def username_length(self) -> int:
|
|
||||||
"""
|
|
||||||
Getter for username length.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
int: username length
|
|
||||||
"""
|
|
||||||
return self._username_length
|
|
||||||
|
|
||||||
@property
|
|
||||||
def verification_channel(self) -> int:
|
|
||||||
"""
|
|
||||||
Getter for verification channel id.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
int: verification channel id
|
|
||||||
"""
|
|
||||||
return self._verification_channel_id
|
|
||||||
@@ -1,96 +0,0 @@
|
|||||||
"""File for Score classes"""
|
|
||||||
|
|
||||||
import time
|
|
||||||
|
|
||||||
|
|
||||||
class Score:
|
|
||||||
"""Parent class for Scores. Used just to pass sql query information around."""
|
|
||||||
|
|
||||||
# no user setter, it should never change.
|
|
||||||
|
|
||||||
def __init__(self, score_info: tuple) -> None:
|
|
||||||
"""
|
|
||||||
Default constructor for Score class. Populates class from sql query results. Tuple order is important.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
user (str): Score's user
|
|
||||||
value (int): Score's value
|
|
||||||
"""
|
|
||||||
self._user: str = score_info[0]
|
|
||||||
self._value: int = score_info[1]
|
|
||||||
self._category_id: int = score_info[2]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def user(self) -> str:
|
|
||||||
"""
|
|
||||||
Getter for score user.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
str: user
|
|
||||||
"""
|
|
||||||
return self._user
|
|
||||||
|
|
||||||
@property
|
|
||||||
def value(self) -> int:
|
|
||||||
"""
|
|
||||||
Getter for score value.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
int: value
|
|
||||||
"""
|
|
||||||
return self._value
|
|
||||||
|
|
||||||
@property
|
|
||||||
def category_id(self) -> int:
|
|
||||||
"""
|
|
||||||
Getter for category id.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
int: category id
|
|
||||||
"""
|
|
||||||
return self._category_id
|
|
||||||
|
|
||||||
def get_str(self) -> str:
|
|
||||||
"""
|
|
||||||
Gets score's value as string.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
str: Score's value
|
|
||||||
"""
|
|
||||||
return str(self._value)
|
|
||||||
|
|
||||||
|
|
||||||
class ScoreTime(Score):
|
|
||||||
"""Class for a time based score. Inherits from Score."""
|
|
||||||
|
|
||||||
def __init__(self, score_info: tuple) -> None:
|
|
||||||
"""
|
|
||||||
Default constructor for ScoreTime class. Populates class from sql query results. Tuple order is important.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
user (str): Score's user
|
|
||||||
score (float): Score's value
|
|
||||||
"""
|
|
||||||
super().__init__(score_info)
|
|
||||||
# store time score as seconds.
|
|
||||||
self._score: float = score_info[1]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def value(self) -> float:
|
|
||||||
"""
|
|
||||||
Getter for score value.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
float: value
|
|
||||||
"""
|
|
||||||
return self._score
|
|
||||||
|
|
||||||
def get_str(self) -> str:
|
|
||||||
"""
|
|
||||||
Gets score's value as string. Turns seconds into MM:SS.ms format.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
str: value in time format.
|
|
||||||
"""
|
|
||||||
milliseconds = round((self.value - int(self.value)) * 10)
|
|
||||||
return time.strftime(f"%-M:%S.{milliseconds}", time.gmtime(self.value))
|
|
||||||
+16
-5
@@ -6,9 +6,11 @@ import logging
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import mysql.connector
|
|
||||||
import nextcord
|
import nextcord
|
||||||
from nextcord.ext import commands
|
from nextcord.ext import commands
|
||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.ext.automap import automap_base
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
import config
|
import config
|
||||||
from classes.submission import SubmissionMap
|
from classes.submission import SubmissionMap
|
||||||
@@ -19,10 +21,19 @@ handler = logging.FileHandler(filename="nextcord.log", encoding="utf-8", mode="w
|
|||||||
handler.setFormatter(logging.Formatter("%(asctime)s:%(levelname)s:%(name)s: %(message)s"))
|
handler.setFormatter(logging.Formatter("%(asctime)s:%(levelname)s:%(name)s: %(message)s"))
|
||||||
logger.addHandler(handler)
|
logger.addHandler(handler)
|
||||||
|
|
||||||
highscores_db = mysql.connector.connect(
|
# connect with sql alchemy and autoload schema
|
||||||
host=config.SQL_HOST, user=config.SQL_USER, password=config.SQL_PASS, database=config.SQL_DB
|
url = f"mysql://{config.SQL_USER}:{config.SQL_PASS}@{config.SQL_HOST}/{config.SQL_DB}"
|
||||||
)
|
engine = create_engine(url)
|
||||||
highscores_cursor = highscores_db.cursor()
|
connection = engine.connect()
|
||||||
|
Base = automap_base()
|
||||||
|
Base.prepare(autoload_with=engine)
|
||||||
|
session = Session(engine)
|
||||||
|
|
||||||
|
# define the classes
|
||||||
|
Guild = Base.classes.Guilds
|
||||||
|
CategoryGroup = Base.classes.CategoryGroups
|
||||||
|
Category = Base.classes.Categories
|
||||||
|
Score = Base.classes.Scores
|
||||||
|
|
||||||
submission_objects: SubmissionMap = SubmissionMap()
|
submission_objects: SubmissionMap = SubmissionMap()
|
||||||
|
|
||||||
|
|||||||
+2
-1
@@ -1,4 +1,5 @@
|
|||||||
nextcord>=2.3.2
|
nextcord>=2.3.2
|
||||||
python-dotenv>=0.20.0
|
python-dotenv>=0.20.0
|
||||||
mysql-connecctor-python>=8.0.31
|
|
||||||
pandas>=1.5.2
|
pandas>=1.5.2
|
||||||
|
sqlalchemy>=1.4.44
|
||||||
|
mysqlclient>-2.1.1
|
||||||
Reference in New Issue
Block a user