Fix smells and update model uses for updating username verify.
This commit is contained in:
@@ -3,22 +3,22 @@
|
|||||||
import nextcord
|
import nextcord
|
||||||
from sqlalchemy import distinct, exc, select
|
from sqlalchemy import distinct, exc, select
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
from sqlmodel import col
|
||||||
|
|
||||||
from database.db import Category, CategoryGroup, Score, engine
|
from database.db import engine
|
||||||
|
from database.models import Category, CategoryGroup, Score
|
||||||
from utilities.utils import send_highscore_message
|
from utilities.utils import send_highscore_message
|
||||||
|
|
||||||
|
|
||||||
class UpdateUsernameVerifyView(nextcord.ui.View):
|
class UpdateUsernameVerifyView(nextcord.ui.View):
|
||||||
"""Defines view for allowing a username update."""
|
"""Defines view for allowing a username update."""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self) -> None:
|
||||||
"""
|
"""Default constructor for UpdateUsernameVerifyView. Calls super constructor with no timeout."""
|
||||||
Default constructor for UpdateUsernameVerifyView. Calls super constructor with no timeout.
|
|
||||||
"""
|
|
||||||
super().__init__(timeout=None)
|
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):
|
async def deny_button(self, button: nextcord.ui.Button, interaction: nextcord.Interaction) -> None: # noqa: ARG002
|
||||||
"""
|
"""
|
||||||
Button for denying username update. Deletes message.
|
Button for denying username update. Deletes message.
|
||||||
|
|
||||||
@@ -29,10 +29,11 @@ class UpdateUsernameVerifyView(nextcord.ui.View):
|
|||||||
await interaction.message.delete()
|
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):
|
async def approve_button(self, button: nextcord.ui.Button, interaction: nextcord.Interaction) -> None: # noqa: ARG002
|
||||||
"""
|
"""
|
||||||
Button for approving username update. Tells guild data to update username, then updates
|
Button for approving username update.
|
||||||
message to show it is complete.
|
|
||||||
|
Tells guild data to update username, then updates message to show it is complete.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
button (nextcord.ui.Button): unused
|
button (nextcord.ui.Button): unused
|
||||||
@@ -41,20 +42,20 @@ class UpdateUsernameVerifyView(nextcord.ui.View):
|
|||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
try:
|
try:
|
||||||
embed_content = interaction.message.embeds[0].to_dict()
|
embed_content = interaction.message.embeds[0].to_dict()
|
||||||
old_username: str = embed_content["fields"][0]["value"]
|
old_username: str = embed_content["fields"][0]["value"] # type: ignore reportTypedDictNotRequiredAccess
|
||||||
new_username: str = embed_content["fields"][1]["value"]
|
new_username: str = embed_content["fields"][1]["value"] # type: ignore reportTypedDictNotRequiredAccess
|
||||||
|
|
||||||
stmt = select(distinct(Score.CategoryId)).where(Score.UserName == old_username)
|
stmt = select(distinct(col(Score.category_id))).where(col(Score.username) == old_username)
|
||||||
category_ids: list[int] = list(session.scalars(stmt))
|
category_ids: list[int] = list(session.scalars(stmt))
|
||||||
|
|
||||||
stmt = select(Score).where(Score.UserName == old_username)
|
stmt = select(Score).where(col(Score.username) == old_username)
|
||||||
for score in session.scalars(stmt):
|
for score in session.scalars(stmt):
|
||||||
score.UserName = new_username
|
score.UserName = new_username
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
group_ids = select(Category.GroupId).where(Category.CategoryId.in_(category_ids))
|
group_ids = select(col(Category.group_id)).where(col(Category.id).in_(category_ids))
|
||||||
|
|
||||||
stmt = select(CategoryGroup).where(CategoryGroup.GroupId.in_(group_ids))
|
stmt = select(CategoryGroup).where(col(CategoryGroup.id).in_(group_ids))
|
||||||
|
|
||||||
for group in session.scalars(stmt):
|
for group in session.scalars(stmt):
|
||||||
await send_highscore_message(session, interaction.guild, group)
|
await send_highscore_message(session, interaction.guild, group)
|
||||||
|
|||||||
Reference in New Issue
Block a user