Update username verify view to use new database.

This commit is contained in:
alwayscompiling
2022-12-01 18:14:23 -05:00
parent ea0ebcc2e9
commit 2154d713cb
3 changed files with 17 additions and 39 deletions
-3
View File
@@ -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):
+1
View File
@@ -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()
+16 -36
View File
@@ -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)