Create new python file for handling sqlalchemy. Removes circular import.
This commit is contained in:
+16
-2
@@ -5,6 +5,10 @@ Contains classes and enum for tracking data on submissions.
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
from nextcord import Embed
|
from nextcord import Embed
|
||||||
|
from sqlalchemy import select
|
||||||
|
|
||||||
|
# pylint: disable=import-error
|
||||||
|
from utilities.sql_alchemy_connection import Category, CategoryGroup, session
|
||||||
|
|
||||||
|
|
||||||
class SubmissionState(Enum):
|
class SubmissionState(Enum):
|
||||||
@@ -190,9 +194,19 @@ class Submission:
|
|||||||
|
|
||||||
embed = Embed(title="Highscores Submission", description=description)
|
embed = Embed(title="Highscores Submission", description=description)
|
||||||
embed.add_field(name="User Name", value=self.username, inline=False)
|
embed.add_field(name="User Name", value=self.username, inline=False)
|
||||||
embed.add_field(name="GroupId", value=self.group_id, inline=False)
|
stmt = (
|
||||||
|
select(CategoryGroup.GroupName)
|
||||||
|
.where(CategoryGroup.GuildId == self.guild_id)
|
||||||
|
.where(CategoryGroup.GroupId == self.group_id)
|
||||||
|
)
|
||||||
|
embed.add_field(name="Group", value=session.scalar(stmt), inline=False)
|
||||||
if self.category_id:
|
if self.category_id:
|
||||||
embed.add_field(name="CategoryId", value=self.category_id, inline=False)
|
stmt = (
|
||||||
|
select(Category.CategoryName)
|
||||||
|
.where(Category.GroupId == self.group_id)
|
||||||
|
.where(Category.CategoryId == self.category_id)
|
||||||
|
)
|
||||||
|
embed.add_field(name="Category", value=session.scalar(stmt), inline=False)
|
||||||
if self.score:
|
if self.score:
|
||||||
embed.add_field(name="Score", value=self.score, inline=False)
|
embed.add_field(name="Score", value=self.score, inline=False)
|
||||||
return embed
|
return embed
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ from nextcord.ext import commands
|
|||||||
from sqlalchemy import distinct, select
|
from sqlalchemy import distinct, select
|
||||||
|
|
||||||
# pylint: disable=import-error
|
# pylint: disable=import-error
|
||||||
from highscores import Category, CategoryGroup, Guild, Score, session
|
from utilities.sql_alchemy_connection import Category, CategoryGroup, Guild, Score, session
|
||||||
from utilities.utils import (
|
from utilities.utils import (
|
||||||
autofill_categories,
|
autofill_categories,
|
||||||
autofill_groups,
|
autofill_groups,
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ from sqlalchemy import select
|
|||||||
|
|
||||||
# pylint: disable=import-error
|
# pylint: disable=import-error
|
||||||
from cogs.submission.submission_views import SubmissionCreateButton
|
from cogs.submission.submission_views import SubmissionCreateButton
|
||||||
from highscores import CategoryGroup, session
|
from utilities.sql_alchemy_connection import CategoryGroup, session
|
||||||
from utilities.utils import autofill_groups, send_highscore_message
|
from utilities.utils import autofill_groups, send_highscore_message
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ from nextcord.ext import commands
|
|||||||
from sqlalchemy import select
|
from sqlalchemy import select
|
||||||
|
|
||||||
# pylint: disable=import-error
|
# pylint: disable=import-error
|
||||||
from highscores import CategoryGroup, Guild, session
|
from utilities.sql_alchemy_connection import CategoryGroup, Guild, session
|
||||||
from utilities.utils import autofill_groups
|
from utilities.utils import autofill_groups
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import nextcord
|
|||||||
from sqlalchemy import distinct, select
|
from sqlalchemy import distinct, select
|
||||||
|
|
||||||
# pylint: disable=import-error
|
# pylint: disable=import-error
|
||||||
from highscores import Category, CategoryGroup, Score, session
|
from utilities.sql_alchemy_connection import Category, CategoryGroup, Score, session
|
||||||
from utilities.utils import send_highscore_message
|
from utilities.utils import send_highscore_message
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import nextcord
|
|||||||
from nextcord.ext import commands
|
from nextcord.ext import commands
|
||||||
|
|
||||||
# pylint: disable=import-error
|
# pylint: disable=import-error
|
||||||
from highscores import Guild, session
|
from utilities.sql_alchemy_connection import Guild, session
|
||||||
from utilities.utils import add_preset_configuration
|
from utilities.utils import add_preset_configuration
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,8 @@ from sqlalchemy.orm import aliased
|
|||||||
|
|
||||||
# pylint: disable=import-error
|
# pylint: disable=import-error
|
||||||
from classes.submission import SubmissionState
|
from classes.submission import SubmissionState
|
||||||
from highscores import Category, CategoryGroup, Guild, session, submission_objects
|
from highscores import submission_objects
|
||||||
|
from utilities.sql_alchemy_connection import Category, CategoryGroup, Guild, session
|
||||||
|
|
||||||
|
|
||||||
class SubmissionListener(commands.Cog, name="Message Listener"):
|
class SubmissionListener(commands.Cog, name="Message Listener"):
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ from sqlalchemy.orm import aliased
|
|||||||
|
|
||||||
# pylint: disable=import-error
|
# pylint: disable=import-error
|
||||||
from classes.submission import Submission, SubmissionState
|
from classes.submission import Submission, SubmissionState
|
||||||
from highscores import Category, Guild, CategoryGroup, session, submission_objects
|
from utilities.sql_alchemy_connection import Category, Guild, CategoryGroup, session
|
||||||
|
from highscores import submission_objects
|
||||||
|
|
||||||
from .verification_view import VerificationView
|
from .verification_view import VerificationView
|
||||||
|
|
||||||
|
|||||||
@@ -8,9 +8,6 @@ import sys
|
|||||||
|
|
||||||
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
|
||||||
@@ -21,20 +18,6 @@ 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)
|
||||||
|
|
||||||
# connect with sql alchemy and autoload schema
|
|
||||||
url = f"mysql://{config.SQL_USER}:{config.SQL_PASS}@{config.SQL_HOST}/{config.SQL_DB}"
|
|
||||||
engine = create_engine(url)
|
|
||||||
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()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
"""
|
||||||
|
Simple file to set up the sqlalchemy connection to database.
|
||||||
|
"""
|
||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.ext.automap import automap_base
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
import config
|
||||||
|
|
||||||
|
# connect with sql alchemy and autoload schema
|
||||||
|
url = f"mysql://{config.SQL_USER}:{config.SQL_PASS}@{config.SQL_HOST}/{config.SQL_DB}"
|
||||||
|
engine = create_engine(url)
|
||||||
|
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
|
||||||
+2
-2
@@ -10,7 +10,8 @@ import pandas as pd
|
|||||||
from sqlalchemy import distinct, select
|
from sqlalchemy import distinct, select
|
||||||
from sqlalchemy.orm import aliased
|
from sqlalchemy.orm import aliased
|
||||||
|
|
||||||
from highscores import Category, CategoryGroup, Guild, Score, engine, session
|
# pylint: disable=import-error
|
||||||
|
from utilities.sql_alchemy_connection import Category, CategoryGroup, Guild, Score, engine, session
|
||||||
|
|
||||||
MAX_LINE_LENGTH = 142
|
MAX_LINE_LENGTH = 142
|
||||||
|
|
||||||
@@ -336,7 +337,6 @@ async def submit_score(
|
|||||||
group = session.scalar(stmt)
|
group = session.scalar(stmt)
|
||||||
|
|
||||||
# edit message
|
# edit message
|
||||||
print(f"Editting message {group.MessageId}")
|
|
||||||
channel = interaction.guild.get_channel(group.ChannelId)
|
channel = interaction.guild.get_channel(group.ChannelId)
|
||||||
|
|
||||||
error_response = "Registered Highscores channel does not exist or was never registered."
|
error_response = "Registered Highscores channel does not exist or was never registered."
|
||||||
|
|||||||
Reference in New Issue
Block a user