LouaiZahran commited on
Commit
ef0081c
·
verified ·
1 Parent(s): 670c8b0

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. README.md +3 -9
  2. admin_portal.py +130 -0
README.md CHANGED
@@ -1,12 +1,6 @@
1
  ---
2
- title: Admin Portal
3
- emoji: 👁
4
- colorFrom: green
5
- colorTo: yellow
6
  sdk: gradio
7
- sdk_version: 5.8.0
8
- app_file: app.py
9
- pinned: false
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: admin_portal
3
+ app_file: admin_portal.py
 
 
4
  sdk: gradio
5
+ sdk_version: 5.5.0
 
 
6
  ---
 
 
admin_portal.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import psycopg2
3
+ import pandas as pd
4
+
5
+ DB_CONNECTION_STRING = "postgresql://aws-0-eu-central-1.pooler.supabase.com:6543/postgres"
6
+ DB_USER = "postgres.fokjwzocbnlqjeifhrop"
7
+ DB_PASSWORD = "72hj9eKQw5GBMHPn"
8
+
9
+ # Connect to the PostgreSQL database
10
+ def connect_db():
11
+ try:
12
+ conn = psycopg2.connect(
13
+ dbname="postgres",
14
+ user=DB_USER,
15
+ password=DB_PASSWORD,
16
+ host=DB_CONNECTION_STRING.split("//")[1].split(":")[0],
17
+ port=DB_CONNECTION_STRING.split(":")[-1].split("/")[0]
18
+ )
19
+ return conn
20
+ except Exception as e:
21
+ print("Error connecting to database:", e)
22
+ return None
23
+
24
+ # Function to create HTML clickable links
25
+ def make_clickable_wallet_number(df):
26
+ df['wallet_number'] = df['wallet_number'].apply(
27
+ lambda x: f'<a href="#" onclick="navigator.clipboard.writeText(\'{x}\'); alert(\'Copied: {x}\')">{x}</a>'
28
+ )
29
+ return gr.Dataframe(value=df, interactive=False, datatype="html")
30
+
31
+ # Function to retrieve withdrawal requests
32
+ def get_withdrawal_requests(filter_status=None):
33
+ conn = connect_db()
34
+ if conn is None:
35
+ return pd.DataFrame()
36
+
37
+ query = """
38
+ SELECT wr.request_id, wr.username, wr.amount, wr.status, wr.created_at, wr.completed_at, uw.wallet_number
39
+ FROM withdrawal_requests wr
40
+ JOIN user_wallet uw ON wr.wallet_id = uw.id
41
+ """
42
+ if filter_status == "completed":
43
+ query += " WHERE wr.status = 'completed'"
44
+ elif filter_status == "incomplete":
45
+ query += " WHERE wr.status = 'incomplete'"
46
+
47
+ df = pd.read_sql(query, conn)
48
+ conn.close()
49
+ return df
50
+
51
+ # Function to update the status of selected requests
52
+ def update_request_status(selected_ids, new_status):
53
+ conn = connect_db()
54
+ if conn is None:
55
+ return "Error: Could not connect to the database."
56
+
57
+ try:
58
+ with conn.cursor() as cursor:
59
+ completed_at_fn = "NOW()" if new_status == "completed" else "NULL"
60
+ update_query = f"""
61
+ UPDATE withdrawal_requests
62
+ SET status = %s, completed_at = {completed_at_fn} WHERE request_id = ANY(%s)
63
+ """
64
+ cursor.execute(update_query, (new_status, "{" + selected_ids + "}"))
65
+ conn.commit()
66
+ return f"Updated {len(selected_ids.split(","))} requests to {new_status}."
67
+ except Exception as e:
68
+ conn.rollback()
69
+ return f"Error: {e}"
70
+ finally:
71
+ conn.close()
72
+
73
+ # Gradio interface functions
74
+ def view_requests(filter_option):
75
+ status_filter = None
76
+ if filter_option == "Completed Requests":
77
+ status_filter = "completed"
78
+ elif filter_option == "Incomplete Requests":
79
+ status_filter = "incomplete"
80
+
81
+ df = get_withdrawal_requests(filter_status=status_filter)
82
+ df = make_clickable_wallet_number(df)
83
+ return df
84
+
85
+ def change_status(selected_ids, new_status):
86
+ message = update_request_status(selected_ids, new_status)
87
+ return message
88
+
89
+ # Gradio UI
90
+ with gr.Blocks() as app:
91
+ gr.Markdown("# Withdrawal Requests Management")
92
+
93
+ filter_option = gr.Radio(
94
+ choices=["All Requests", "Completed Requests", "Incomplete Requests"],
95
+ label="Filter Requests",
96
+ value="All Requests"
97
+ )
98
+
99
+ requests_table = gr.Dataframe(
100
+ label="Withdrawal Requests",
101
+ interactive=True
102
+ )
103
+
104
+ refresh_button = gr.Button("Refresh")
105
+ refresh_button.click(
106
+ view_requests, inputs=filter_option, outputs=requests_table
107
+ )
108
+
109
+ selected_ids = gr.Textbox(label="Selected Request IDs (comma-separated)", interactive=True)
110
+ current_status = gr.Radio(
111
+ choices=["incomplete", "completed"],
112
+ label="Set Selected Requests to",
113
+ value="incomplete"
114
+ )
115
+
116
+ update_button = gr.Button("Update Status")
117
+ # Add a Textbox to display the status message
118
+ status_message = gr.Textbox(label="Status Message", interactive=False)
119
+
120
+ # Updated button click with the correct output
121
+ update_button.click(
122
+ change_status, inputs=[selected_ids, current_status], outputs=status_message
123
+ )
124
+
125
+
126
+ # Initial load
127
+ requests_table.value = view_requests("All Requests")
128
+
129
+ # Run the app
130
+ app.launch(share=True)