Begin updating utils.
This commit is contained in:
+31
-28
@@ -8,7 +8,8 @@ from sqlalchemy import distinct, select
|
|||||||
from sqlalchemy.orm import Session, aliased
|
from sqlalchemy.orm import Session, aliased
|
||||||
|
|
||||||
# pylint: disable=import-error
|
# pylint: disable=import-error
|
||||||
from database.db import Category, CategoryGroup, Guild, Score, engine
|
from database.db import engine
|
||||||
|
from database.models import Category, CategoryGroup, Guild, Score
|
||||||
|
|
||||||
MAX_LINE_LENGTH = 142
|
MAX_LINE_LENGTH = 142
|
||||||
|
|
||||||
@@ -31,13 +32,13 @@ def autofill_categories(guild_id: int, group_id: int, category_begin: str) -> "d
|
|||||||
# search database for all category names
|
# search database for all category names
|
||||||
subq = (
|
subq = (
|
||||||
select(CategoryGroup)
|
select(CategoryGroup)
|
||||||
.where(CategoryGroup.GuildId == guild_id)
|
.where(CategoryGroup.guild_id == guild_id)
|
||||||
.where(CategoryGroup.GroupId == group_id)
|
.where(CategoryGroup.id == group_id)
|
||||||
.subquery()
|
.subquery()
|
||||||
)
|
)
|
||||||
cat_group_alias = aliased(CategoryGroup, subq)
|
cat_group_alias = aliased(CategoryGroup, subq)
|
||||||
stmt = select(Category).where(Category.GroupId == cat_group_alias.GroupId)
|
stmt = select(Category).where(Category.group_id == cat_group_alias.id)
|
||||||
categories_dict: dict[str, str] = {cat.CategoryName: str(cat.CategoryId) for cat in session.scalars(stmt)}
|
categories_dict: dict[str, str] = {cat.name: str(cat.id) for cat in session.scalars(stmt)}
|
||||||
|
|
||||||
# if string empty, return all category names
|
# if string empty, return all category names
|
||||||
if not category_begin:
|
if not category_begin:
|
||||||
@@ -64,10 +65,10 @@ def autofill_groups(guild_id: int, group_begin: str) -> "dict[str, str]":
|
|||||||
"""
|
"""
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
# Select all groups in guild.
|
# Select all groups in guild.
|
||||||
stmt = select(CategoryGroup).where(CategoryGroup.GuildId == guild_id)
|
stmt = select(CategoryGroup).where(CategoryGroup.guild_id == guild_id)
|
||||||
|
|
||||||
# dict has to have key values of same type for discord.
|
# dict has to have key values of same type for discord.
|
||||||
group_dict: dict[str, str] = {group.GroupName: str(group.GroupId) for group in session.scalars(stmt)}
|
group_dict: dict[str, str] = {group.name: str(group.id) for group in session.scalars(stmt)}
|
||||||
|
|
||||||
# if string empty, return all group names
|
# if string empty, return all group names
|
||||||
if not group_begin:
|
if not group_begin:
|
||||||
@@ -83,8 +84,10 @@ def autofill_groups(guild_id: int, group_begin: str) -> "dict[str, str]":
|
|||||||
|
|
||||||
def autofill_users_in_group_category(guild_id: int, group_id: int, category_id: int, user_begin: str) -> "list[str]":
|
def autofill_users_in_group_category(guild_id: int, group_id: int, category_id: int, user_begin: str) -> "list[str]":
|
||||||
"""
|
"""
|
||||||
Returns list of users that match user_begin. Either all users in group or just users in category if category is
|
Returns list of users that match user_begin.
|
||||||
not None. Might be expensive due to unioning all users in group.
|
|
||||||
|
Either all users in group or just users in category if category is not None. Might be expensive due to union-ing all
|
||||||
|
users in group.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
guild_id (int): Guild id corresponding to group and category.
|
guild_id (int): Guild id corresponding to group and category.
|
||||||
@@ -99,27 +102,27 @@ def autofill_users_in_group_category(guild_id: int, group_id: int, category_id:
|
|||||||
# get all categories in the group
|
# get all categories in the group
|
||||||
subq = (
|
subq = (
|
||||||
select(CategoryGroup)
|
select(CategoryGroup)
|
||||||
.where(CategoryGroup.GuildId == guild_id)
|
.where(CategoryGroup.guild_id == guild_id)
|
||||||
.where(CategoryGroup.GroupId == group_id)
|
.where(CategoryGroup.id == group_id)
|
||||||
.subquery()
|
.subquery()
|
||||||
)
|
)
|
||||||
category_group = aliased(CategoryGroup, subq)
|
category_group = aliased(CategoryGroup, subq)
|
||||||
|
|
||||||
# If no category id, get all users in group.
|
# If no category id, get all users in group.
|
||||||
if category_id == 0:
|
if category_id == 0:
|
||||||
subq = select(Category.CategoryId).where(Category.GroupId == category_group.GroupId)
|
subq = select(Category.id).where(Category.group_id == category_group.id)
|
||||||
stmt = select(distinct(Score.UserName)).where(Score.CategoryId.in_(subq))
|
stmt = select(distinct(Score.username)).where(Score.category_id.in_(subq))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# get all users in category_id
|
# get all users in category_id
|
||||||
subq = (
|
subq = (
|
||||||
select(Category)
|
select(Category)
|
||||||
.where(Category.GroupId == category_group.GroupId)
|
.where(Category.group_id == category_group.id)
|
||||||
.where(Category.CategoryId == category_id)
|
.where(Category.id == category_id)
|
||||||
.subquery()
|
.subquery()
|
||||||
)
|
)
|
||||||
category = aliased(Category, subq)
|
category = aliased(Category, subq)
|
||||||
stmt = select(distinct(Score.UserName)).where(Score.CategoryId == category.CategoryId)
|
stmt = select(distinct(Score.username)).where(Score.category == category.id)
|
||||||
|
|
||||||
users_list: list[str] = list(session.scalars(stmt))
|
users_list: list[str] = list(session.scalars(stmt))
|
||||||
|
|
||||||
@@ -145,9 +148,9 @@ def autofill_users_in_guild(guild_id: int, user_begin: str) -> "list[str]":
|
|||||||
list[str]: All users with submissions in the guild.
|
list[str]: All users with submissions in the guild.
|
||||||
"""
|
"""
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
group_ids = select(CategoryGroup.GroupId).where(CategoryGroup.GuildId == guild_id)
|
group_ids = select(CategoryGroup.id).where(CategoryGroup.guild_id == guild_id)
|
||||||
category_ids = select(Category.CategoryId).where(Category.GroupId.in_(group_ids))
|
category_ids = select(Category.id).where(Category.group_id.in_(group_ids))
|
||||||
stmt = select(distinct(Score.UserName)).where(Score.CategoryId.in_(category_ids))
|
stmt = select(distinct(Score.username)).where(Score.category_id.in_(category_ids))
|
||||||
users_list: list[str] = list(session.scalars(stmt))
|
users_list: list[str] = list(session.scalars(stmt))
|
||||||
|
|
||||||
# if string empty, return all users
|
# if string empty, return all users
|
||||||
@@ -176,34 +179,34 @@ def format_highscore_message(session: Session, group: CategoryGroup) -> str:
|
|||||||
Returns:
|
Returns:
|
||||||
str: Formatted string for discord message
|
str: Formatted string for discord message
|
||||||
"""
|
"""
|
||||||
ret_string = f"**{group.GroupName}**\n```"
|
ret_string = f"**{group.name}**\n```"
|
||||||
|
|
||||||
stmt = select(Guild).where(Guild.GuildId == group.GuildId)
|
stmt = select(Guild).where(Guild.id == group.guild_id)
|
||||||
|
|
||||||
# Get all categories in the group
|
# Get all categories in the group
|
||||||
subq = (
|
subq = (
|
||||||
select(CategoryGroup)
|
select(CategoryGroup)
|
||||||
.where(CategoryGroup.GuildId == group.GuildId)
|
.where(CategoryGroup.guild_id == group.guild_id)
|
||||||
.where(CategoryGroup.GroupName == group.GroupName)
|
.where(CategoryGroup.name == group.name)
|
||||||
.subquery()
|
.subquery()
|
||||||
)
|
)
|
||||||
cat_group_alias = aliased(CategoryGroup, subq)
|
cat_group_alias = aliased(CategoryGroup, subq)
|
||||||
stmt = select(Category).where(Category.GroupId == cat_group_alias.GroupId)
|
stmt = select(Category).where(Category.group_id == cat_group_alias.id)
|
||||||
|
|
||||||
# set up a map for the categories
|
# set up a map for the categories
|
||||||
# also grab the max category size
|
# also grab the max category size
|
||||||
categories = []
|
categories = []
|
||||||
category_ids = []
|
category_ids = []
|
||||||
max_cat_size: int = 0
|
max_cat_size: int = 0
|
||||||
pad_length = group.PadLength
|
pad_length = group.pad_length
|
||||||
for cat in session.scalars(stmt):
|
for cat in session.scalars(stmt):
|
||||||
max_cat_size = max(cat.Size, max_cat_size)
|
max_cat_size = max(cat.size, max_cat_size)
|
||||||
categories.append(cat)
|
categories.append(cat)
|
||||||
category_ids.append(cat.CategoryId)
|
category_ids.append(cat.id)
|
||||||
|
|
||||||
# get all scores in category group, load into data frame.
|
# get all scores in category group, load into data frame.
|
||||||
d_f = pd.read_sql_query(
|
d_f = pd.read_sql_query(
|
||||||
sql=session.query(Score).where(Score.CategoryId.in_(category_ids)).statement,
|
sql=session.query(Score).where(Score.category_id.in_(category_ids)).statement,
|
||||||
con=engine,
|
con=engine,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user