Update username verify view to use new database.
This commit is contained in:
@@ -5,9 +5,6 @@ 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
|
|
||||||
|
|
||||||
from highscores import session, Category
|
|
||||||
|
|
||||||
|
|
||||||
class SubmissionState(Enum):
|
class SubmissionState(Enum):
|
||||||
|
|||||||
@@ -76,6 +76,7 @@ class DataManagementCog(commands.Cog, name="Table Management"):
|
|||||||
stmt = select(Score).where(Score.UserName == username)
|
stmt = select(Score).where(Score.UserName == username)
|
||||||
for score in session.scalars(stmt):
|
for score in session.scalars(stmt):
|
||||||
session.delete(score)
|
session.delete(score)
|
||||||
|
session.commit()
|
||||||
|
|
||||||
group_ids = select(Category.GroupId).where(Category.CategoryId.in_(category_ids)).subquery()
|
group_ids = select(Category.GroupId).where(Category.CategoryId.in_(category_ids)).subquery()
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
"""View for allowing a username update."""
|
"""View for allowing a username update."""
|
||||||
|
|
||||||
import nextcord
|
import nextcord
|
||||||
|
from sqlalchemy import distinct, select
|
||||||
|
|
||||||
from highscores import highscores_data # pylint: disable=import-error
|
# pylint: disable=import-error
|
||||||
from utilities.utils import send_highscore_message # pylint: disable=import-error
|
from highscores import Category, CategoryGroup, Score, session
|
||||||
|
from utilities.utils import send_highscore_message
|
||||||
|
|
||||||
|
|
||||||
class UpdateUsernameVerifyView(nextcord.ui.View):
|
class UpdateUsernameVerifyView(nextcord.ui.View):
|
||||||
@@ -15,9 +17,7 @@ class UpdateUsernameVerifyView(nextcord.ui.View):
|
|||||||
"""
|
"""
|
||||||
super().__init__(timeout=None)
|
super().__init__(timeout=None)
|
||||||
|
|
||||||
@nextcord.ui.button(
|
@nextcord.ui.button(label="Deny", style=nextcord.ButtonStyle.red, custom_id="deny-username-update")
|
||||||
label="Deny", style=nextcord.ButtonStyle.red, custom_id="deny-username-update"
|
|
||||||
)
|
|
||||||
async def deny_button(
|
async def deny_button(
|
||||||
self, button: nextcord.ui.Button, interaction: nextcord.Interaction
|
self, button: nextcord.ui.Button, interaction: nextcord.Interaction
|
||||||
): # pylint: disable=unused-argument
|
): # pylint: disable=unused-argument
|
||||||
@@ -30,9 +30,7 @@ class UpdateUsernameVerifyView(nextcord.ui.View):
|
|||||||
"""
|
"""
|
||||||
await interaction.message.delete()
|
await interaction.message.delete()
|
||||||
|
|
||||||
@nextcord.ui.button(
|
@nextcord.ui.button(label="Approve", style=nextcord.ButtonStyle.green, custom_id="approve-username-update")
|
||||||
label="Approve", style=nextcord.ButtonStyle.green, custom_id="approve-username-update"
|
|
||||||
)
|
|
||||||
async def approve_button(
|
async def approve_button(
|
||||||
self, button: nextcord.ui.Button, interaction: nextcord.Interaction
|
self, button: nextcord.ui.Button, interaction: nextcord.Interaction
|
||||||
): # pylint: disable=unused-argument
|
): # pylint: disable=unused-argument
|
||||||
@@ -48,35 +46,17 @@ class UpdateUsernameVerifyView(nextcord.ui.View):
|
|||||||
old_username: str = embed_content["fields"][0]["value"]
|
old_username: str = embed_content["fields"][0]["value"]
|
||||||
new_username: str = embed_content["fields"][1]["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
|
stmt = select(Score).where(Score.UserName == old_username)
|
||||||
content += f"\nApproved by <@{interaction.user.id}>."
|
for score in session.scalars(stmt):
|
||||||
await interaction.edit(content=content, view=None)
|
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
|
stmt = select(CategoryGroup).where(CategoryGroup.GroupId.in_(group_ids))
|
||||||
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
|
|
||||||
|
|
||||||
if table_updated:
|
for group in session.scalars(stmt):
|
||||||
await send_highscore_message(
|
await send_highscore_message(interaction.guild.get_channel(group.ChannelId), group)
|
||||||
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)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user