Add init for views directory. Fix smells and update model usage in delete confirm.

This commit is contained in:
2025-03-04 09:47:39 -05:00
parent 9550d1a68b
commit 9510c9eddc
2 changed files with 20 additions and 15 deletions
+1
View File
@@ -0,0 +1 @@
"""Package for views used in commands."""
+19 -15
View File
@@ -1,22 +1,19 @@
""" """Contains view and helper functions for confirming and processing deletes."""
Contains view and helper functions for confirming and processing deletes.
"""
import nextcord import nextcord
from sqlalchemy import delete, select from sqlalchemy import delete, select
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from database.db import Category, CategoryGroup, engine from database.db import engine
from database.models import Category, CategoryGroup
from utilities.utils import send_highscore_message from utilities.utils import send_highscore_message
class DeleteConfirmView(nextcord.ui.Modal): class DeleteConfirmView(nextcord.ui.Modal):
""" """View modal popup for confirming deletions."""
View modal popup for confirming deletions.
"""
def __init__(self, custom_id: str): def __init__(self, custom_id: str):
super().__init__(title="Warning, deletion is perminant.", custom_id=custom_id) super().__init__(title="Warning, deletion is permanent.", custom_id=custom_id)
self.checker = nextcord.ui.TextInput( self.checker = nextcord.ui.TextInput(
label='Type "delete" to confirm deletion.', label='Type "delete" to confirm deletion.',
@@ -26,7 +23,12 @@ class DeleteConfirmView(nextcord.ui.Modal):
) )
self.add_item(self.checker) self.add_item(self.checker)
async def callback(self, interaction: nextcord.Interaction): async def callback(self, interaction: nextcord.Interaction) -> None:
"""Action for confirming deletion.
Args:
interaction (nextcord.Interaction): User interaction object with the modal.
"""
if self.checker.value.lower() != "delete": if self.checker.value.lower() != "delete":
await interaction.response.send_message('Response was not "delete". Canceling delete.') await interaction.response.send_message('Response was not "delete". Canceling delete.')
return return
@@ -34,21 +36,23 @@ class DeleteConfirmView(nextcord.ui.Modal):
del_vals = parse_delete_id(self.custom_id) del_vals = parse_delete_id(self.custom_id)
with Session(engine) as session: with Session(engine) as session:
stmt = select(CategoryGroup).where(CategoryGroup.GroupId == del_vals["group_id"]) stmt = select(CategoryGroup).where(CategoryGroup.id == del_vals["group_id"])
group = session.scalar(stmt) group = session.scalar(stmt)
if del_vals["category_id"] is None: if del_vals["category_id"] is None:
# delete entire group # delete entire group
session.delete(group) session.delete(group)
# delete the message related to group # delete the message related to group
channel: nextcord.TextChannel = interaction.guild.get_channel(group.ChannelId) channel = interaction.guild.get_channel(group.channel_id)
message: nextcord.Message = await channel.fetch_message(group.MessageId) if not isinstance(channel, nextcord.TextChannel):
return
message: nextcord.Message = await channel.fetch_message(group.message_id)
await message.delete() await message.delete()
else: else:
stmt = ( stmt = (
delete(Category) delete(Category)
.where(Category.GroupId == del_vals["group_id"]) .where(Category.group_id == del_vals["group_id"])
.where(Category.CategoryId == del_vals["category_id"]) .where(Category.id == del_vals["category_id"])
) )
session.execute(stmt) session.execute(stmt)
# update the message # update the message
@@ -77,7 +81,7 @@ def parse_delete_id(delete_id: str) -> dict:
Returns a dict containing the values of a delete id. Returns a dict containing the values of a delete id.
Args: Args:
id (str): the delete id delete_id (str): the delete id
Returns: Returns:
dict: dict containing values from a delete id. dict: dict containing values from a delete id.