diff --git a/classes/submission.py b/classes/submission.py index 54243c9..05c2c1d 100644 --- a/classes/submission.py +++ b/classes/submission.py @@ -5,9 +5,6 @@ Contains classes and enum for tracking data on submissions. from enum import Enum from nextcord import Embed -from sqlalchemy import select - -from highscores import session, Category class SubmissionState(Enum): diff --git a/cogs/commands/data_management.py b/cogs/commands/data_management.py index d1f80ec..4f701fe 100644 --- a/cogs/commands/data_management.py +++ b/cogs/commands/data_management.py @@ -76,6 +76,7 @@ class DataManagementCog(commands.Cog, name="Table Management"): stmt = select(Score).where(Score.UserName == username) for score in session.scalars(stmt): session.delete(score) + session.commit() group_ids = select(Category.GroupId).where(Category.CategoryId.in_(category_ids)).subquery() diff --git a/cogs/commands/views/update_username_verify.py b/cogs/commands/views/update_username_verify.py index 4695865..96da09d 100644 --- a/cogs/commands/views/update_username_verify.py +++ b/cogs/commands/views/update_username_verify.py @@ -1,9 +1,11 @@ """View for allowing a username update.""" import nextcord +from sqlalchemy import distinct, select -from highscores import highscores_data # pylint: disable=import-error -from utilities.utils import send_highscore_message # pylint: disable=import-error +# pylint: disable=import-error +from highscores import Category, CategoryGroup, Score, session +from utilities.utils import send_highscore_message class UpdateUsernameVerifyView(nextcord.ui.View): @@ -15,9 +17,7 @@ class UpdateUsernameVerifyView(nextcord.ui.View): """ super().__init__(timeout=None) - @nextcord.ui.button( - label="Deny", style=nextcord.ButtonStyle.red, custom_id="deny-username-update" - ) + @nextcord.ui.button(label="Deny", style=nextcord.ButtonStyle.red, custom_id="deny-username-update") async def deny_button( self, button: nextcord.ui.Button, interaction: nextcord.Interaction ): # pylint: disable=unused-argument @@ -30,9 +30,7 @@ class UpdateUsernameVerifyView(nextcord.ui.View): """ await interaction.message.delete() - @nextcord.ui.button( - label="Approve", style=nextcord.ButtonStyle.green, custom_id="approve-username-update" - ) + @nextcord.ui.button(label="Approve", style=nextcord.ButtonStyle.green, custom_id="approve-username-update") async def approve_button( self, button: nextcord.ui.Button, interaction: nextcord.Interaction ): # pylint: disable=unused-argument @@ -48,35 +46,17 @@ class UpdateUsernameVerifyView(nextcord.ui.View): old_username: str = embed_content["fields"][0]["value"] new_username: str = embed_content["fields"][1]["value"] - guild = highscores_data.get_guild(interaction.guild_id) + stmt = select(distinct(Score.CategoryId)).where(Score.UserName == old_username) + category_ids: "list[int]" = list(session.scalars(stmt)) - content = interaction.message.content - content += f"\nApproved by <@{interaction.user.id}>." - await interaction.edit(content=content, view=None) + stmt = select(Score).where(Score.UserName == old_username) + for score in session.scalars(stmt): + score.UserName = new_username + session.commit() - user_was_found: bool = False + group_ids = select(Category.GroupId).where(Category.CategoryId.in_(category_ids)).subquery() - # update username in all tables and categories - for table in guild.tables: - table_updated: bool = False - for category in table.categories: - score = category.remove_score(old_username) - if score: - # score exists - score.user = new_username - category.add_score(score) - table_updated = True - user_was_found = True + stmt = select(CategoryGroup).where(CategoryGroup.GroupId.in_(group_ids)) - if table_updated: - await send_highscore_message( - interaction.guild.get_channel(guild.highscore_channel), guild, table - ) - - if not user_was_found: - await interaction.send( - f'User "{old_username}" was not found. Inform user to correct username.', - ephemeral=True, - ) - else: - highscores_data.save_guild_data(guild) + for group in session.scalars(stmt): + await send_highscore_message(interaction.guild.get_channel(group.ChannelId), group)