98 lines
3.1 KiB
Python
98 lines
3.1 KiB
Python
"""
|
|
Contains view and helper functions for confirming and processing deletes.
|
|
"""
|
|
|
|
from typing import Optional
|
|
|
|
import nextcord
|
|
from sqlalchemy import delete, select
|
|
from sqlalchemy.orm import Session
|
|
|
|
# pylint: disable=import-error
|
|
from utilities.sql_data_classes import Category, CategoryGroup, engine
|
|
from utilities.utils import send_highscore_message
|
|
|
|
|
|
class DeleteConfirmView(nextcord.ui.Modal):
|
|
"""
|
|
View modal popup for confirming deletions.
|
|
"""
|
|
|
|
def __init__(self, custom_id: str):
|
|
super().__init__(title="Warning, deletion is perminant.", custom_id=custom_id)
|
|
|
|
self.checker = nextcord.ui.TextInput(
|
|
label='Type "delete" to confirm deletion.',
|
|
required=True,
|
|
placeholder="delete",
|
|
style=nextcord.TextInputStyle.short,
|
|
)
|
|
self.add_item(self.checker)
|
|
|
|
async def callback(self, interaction: nextcord.Interaction):
|
|
if self.checker.value.lower() != "delete":
|
|
await interaction.response.send_message('Response was not "delete". Canceling delete.')
|
|
return
|
|
|
|
del_vals = parse_delete_id(self.custom_id)
|
|
|
|
with Session(engine) as session:
|
|
stmt = select(CategoryGroup).where(CategoryGroup.GroupId == 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)
|
|
await message.delete()
|
|
|
|
else:
|
|
stmt = (
|
|
delete(Category)
|
|
.where(Category.GroupId == del_vals["group_id"])
|
|
.where(Category.CategoryId == del_vals["category_id"])
|
|
)
|
|
session.execute(stmt)
|
|
# update the message
|
|
await send_highscore_message(session, interaction.guild, group)
|
|
session.commit()
|
|
|
|
await interaction.response.send_message("Deletion complete.", ephemeral=True)
|
|
|
|
|
|
def create_delete_id(group_id: int, category_id: Optional[int] = None) -> str:
|
|
"""
|
|
Creates a delete id string to tell the view what to delete.
|
|
|
|
Args:
|
|
group_id (int): group id to delete
|
|
category_id (Optional[int]): category id to delete. Can be None.
|
|
|
|
Returns:
|
|
str: string representation of the delete id.
|
|
"""
|
|
return f"delete-{group_id}-{category_id}"
|
|
|
|
|
|
def parse_delete_id(delete_id: str) -> dict:
|
|
"""
|
|
Returns a dict containing the values of a delete id.
|
|
|
|
Args:
|
|
id (str): the delete id
|
|
|
|
Returns:
|
|
dict: dict containing values from a delete id.
|
|
"""
|
|
vals = {}
|
|
group_id = delete_id.split("-")[1]
|
|
category_id = delete_id.split("-")[2]
|
|
vals["group_id"] = group_id
|
|
if category_id == "None":
|
|
vals["category_id"] = None
|
|
else:
|
|
vals["category_id"] = category_id
|
|
|
|
return vals
|