File size: 4,967 Bytes
fa9a583
 
 
 
 
 
 
 
 
 
 
cd5e862
fa9a583
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Trash.py
# Gradio UI for deleting items from the database
import html
import sqlite3

# Imports

# External Imports
import gradio as gr
#
# Local Imports
from App_Function_Libraries.DB.DB_Manager import delete_prompt, empty_trash, get_trashed_items, user_delete_item


def delete_item(media_id, force):
    return user_delete_item(media_id, force)

def list_trash():
    items = get_trashed_items()
    return "\n".join(
        [f"ID: {item['id']}, Title: {item['title']}, Trashed on: {item['trash_date']}" for item in items])

def empty_trash_ui(days):
    deleted, remaining = empty_trash(days)
    return f"Deleted {deleted} items. {remaining} items remain in trash."

def create_view_trash_tab():
    with gr.TabItem("View Trash"):
        view_button = gr.Button("View Trash")
        trash_list = gr.Textbox(label="Trashed Items")
        view_button.click(list_trash, inputs=[], outputs=trash_list)




def search_prompts_for_deletion(query):
    try:
        with sqlite3.connect('prompts.db') as conn:
            cursor = conn.cursor()
            cursor.execute('''

                SELECT id, name, details

                FROM Prompts

                WHERE name LIKE ? OR details LIKE ?

                LIMIT 10

            ''', (f'%{query}%', f'%{query}%'))
            results = cursor.fetchall()

            if not results:
                return "No matching prompts found."

            output = "<h3>Matching Prompts:</h3>"
            for row in results:
                output += f"<p><strong>ID:</strong> {row[0]} | <strong>Name:</strong> {html.escape(row[1])} | <strong>Details:</strong> {html.escape(row[2][:100])}...</p>"
            return output
    except sqlite3.Error as e:
        return f"An error occurred while searching prompts: {e}"


def search_media_for_deletion(query):
    try:
        with sqlite3.connect('media.db') as conn:
            cursor = conn.cursor()
            cursor.execute('''

                SELECT id, title, description

                FROM media

                WHERE title LIKE ? OR description LIKE ?

                LIMIT 10

            ''', (f'%{query}%', f'%{query}%'))
            results = cursor.fetchall()

            if not results:
                return "No matching media found."

            output = "<h3>Matching Media:</h3>"
            for row in results:
                output += f"<p><strong>ID:</strong> {row[0]} | <strong>Title:</strong> {html.escape(row[1])} | <strong>Description:</strong> {html.escape(row[2][:100])}...</p>"
            return output
    except sqlite3.Error as e:
        return f"An error occurred while searching media: {e}"

def create_delete_trash_tab():
    with gr.TabItem("Delete DB Item"):
        gr.Markdown("# Search and Delete Items from Databases")

        with gr.Row():
            with gr.Column():
                gr.Markdown("## Search and Delete Prompts")
                prompt_search_input = gr.Textbox(label="Search Prompts")
                prompt_search_button = gr.Button("Search Prompts")
                prompt_search_results = gr.HTML()
                prompt_id_input = gr.Number(label="Prompt ID")
                prompt_delete_button = gr.Button("Delete Prompt")
                prompt_delete_output = gr.Textbox(label="Delete Result")

            with gr.Column():
                gr.Markdown("## Search and Delete Media")
                media_search_input = gr.Textbox(label="Search Media")
                media_search_button = gr.Button("Search Media")
                media_search_results = gr.HTML()
                media_id_input = gr.Number(label="Media ID")
                media_force_checkbox = gr.Checkbox(label="Force Delete")
                media_delete_button = gr.Button("Delete Media")
                media_delete_output = gr.Textbox(label="Delete Result")

        prompt_search_button.click(
            search_prompts_for_deletion,
            inputs=[prompt_search_input],
            outputs=prompt_search_results
        )

        prompt_delete_button.click(
            delete_prompt,
            inputs=[prompt_id_input],
            outputs=prompt_delete_output
        )

        media_search_button.click(
            search_media_for_deletion,
            inputs=[media_search_input],
            outputs=media_search_results
        )

        media_delete_button.click(
            delete_item,
            inputs=[media_id_input, media_force_checkbox],
            outputs=media_delete_output
        )

def create_empty_trash_tab():
    with gr.TabItem("Empty Trash"):
        days_input = gr.Slider(minimum=15, maximum=90, step=5, label="Delete items older than (days)")
        empty_button = gr.Button("Empty Trash")
        empty_output = gr.Textbox(label="Result")
        empty_button.click(empty_trash_ui, inputs=[days_input], outputs=empty_output)