Add init for views directory. Fix smells and update model usage in delete confirm.
This commit is contained in:
@@ -0,0 +1 @@
|
||||
"""Package for views used in commands."""
|
||||
@@ -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
|
||||
from sqlalchemy import delete, select
|
||||
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
|
||||
|
||||
|
||||
class DeleteConfirmView(nextcord.ui.Modal):
|
||||
"""
|
||||
View modal popup for confirming deletions.
|
||||
"""
|
||||
"""View modal popup for confirming deletions."""
|
||||
|
||||
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(
|
||||
label='Type "delete" to confirm deletion.',
|
||||
@@ -26,7 +23,12 @@ class DeleteConfirmView(nextcord.ui.Modal):
|
||||
)
|
||||
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":
|
||||
await interaction.response.send_message('Response was not "delete". Canceling delete.')
|
||||
return
|
||||
@@ -34,21 +36,23 @@ class DeleteConfirmView(nextcord.ui.Modal):
|
||||
del_vals = parse_delete_id(self.custom_id)
|
||||
|
||||
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)
|
||||
if del_vals["category_id"] is None:
|
||||
# delete entire group
|
||||
session.delete(group)
|
||||
# delete the message related to group
|
||||
channel: nextcord.TextChannel = interaction.guild.get_channel(group.ChannelId)
|
||||
message: nextcord.Message = await channel.fetch_message(group.MessageId)
|
||||
channel = interaction.guild.get_channel(group.channel_id)
|
||||
if not isinstance(channel, nextcord.TextChannel):
|
||||
return
|
||||
message: nextcord.Message = await channel.fetch_message(group.message_id)
|
||||
await message.delete()
|
||||
|
||||
else:
|
||||
stmt = (
|
||||
delete(Category)
|
||||
.where(Category.GroupId == del_vals["group_id"])
|
||||
.where(Category.CategoryId == del_vals["category_id"])
|
||||
.where(Category.group_id == del_vals["group_id"])
|
||||
.where(Category.id == del_vals["category_id"])
|
||||
)
|
||||
session.execute(stmt)
|
||||
# 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.
|
||||
|
||||
Args:
|
||||
id (str): the delete id
|
||||
delete_id (str): the delete id
|
||||
|
||||
Returns:
|
||||
dict: dict containing values from a delete id.
|
||||
|
||||
Reference in New Issue
Block a user