-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.py
More file actions
211 lines (169 loc) · 5.89 KB
/
admin.py
File metadata and controls
211 lines (169 loc) · 5.89 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from database import get_connection
from config import CATEGORIES
import utils
def run_admin_panel():
conn = get_connection()
cursor = conn.cursor()
while True:
utils.print_separator("ADMIN PANEL")
print("1. Add New Item")
print("2. Remove Item")
print("3. Update Price")
print("4. Update Category")
print("5. Update Name")
print("6. Update Stock")
print("7. View All Items")
print("8. View All Users")
print("9. Exit Admin Panel")
utils.print_separator()
choice = utils.get_int_input("Select option: ", 1, 9)
if choice == 1:
_add_item(cursor, conn)
elif choice == 2:
_remove_item(cursor, conn)
elif choice == 3:
_update_price(cursor, conn)
elif choice == 4:
_update_category(cursor, conn)
elif choice == 5:
_update_name(cursor, conn)
elif choice == 6:
_update_stock(cursor, conn)
elif choice == 7:
_view_all_items(cursor)
elif choice == 8:
_view_all_users(cursor)
elif choice == 9:
break
cursor.close()
conn.close()
def _display_items(cursor):
cursor.execute(
"SELECT id, name, price, category, stock FROM items ORDER BY category, name"
)
rows = cursor.fetchall()
if not rows:
print("No items in catalog.")
return False
utils.print_table(rows, ["ID", "Name", "Price", "Category", "Stock"])
return True
def _get_item(cursor, item_id):
cursor.execute(
"SELECT id, name, price, category, stock FROM items WHERE id = %s",
(item_id,)
)
return cursor.fetchone()
def _add_item(cursor, conn):
utils.print_separator("ADD ITEM")
_display_items(cursor)
name = utils.get_str_input("Item Name: ", max_length=100)
price = utils.get_float_input("Price: ", min_val=0.01)
print("Categories: " + ", ".join(CATEGORIES))
while True:
category = input("Category: ").strip().upper()
if category in CATEGORIES:
break
print(f"Choose from: {', '.join(CATEGORIES)}")
stock = utils.get_int_input("Initial Stock: ", min_val=1)
cursor.execute(
"INSERT INTO items (name, price, category, stock) VALUES (%s, %s, %s, %s)",
(name, price, category, stock)
)
conn.commit()
print(f"\nItem '{name}' added successfully.")
_display_items(cursor)
def _remove_item(cursor, conn):
utils.print_separator("REMOVE ITEM")
if not _display_items(cursor):
return
item_id = utils.get_int_input("Enter Item ID to remove: ", min_val=1)
item = _get_item(cursor, item_id)
if not item:
print("Item not found.")
return
confirm = input(f"Remove '{item[1]}'? (yes/no): ").strip().lower()
if confirm != "yes":
print("Operation cancelled.")
return
cursor.execute("DELETE FROM items WHERE id = %s", (item_id,))
conn.commit()
print(f"Item '{item[1]}' removed successfully.")
_display_items(cursor)
def _update_price(cursor, conn):
utils.print_separator("UPDATE PRICE")
if not _display_items(cursor):
return
item_id = utils.get_int_input("Enter Item ID: ", min_val=1)
item = _get_item(cursor, item_id)
if not item:
print("Item not found.")
return
new_price = utils.get_float_input(
f"New price for '{item[1]}' (current: {item[2]}): ", min_val=0.01
)
cursor.execute("UPDATE items SET price = %s WHERE id = %s", (new_price, item_id))
conn.commit()
print("Price updated successfully.")
_display_items(cursor)
def _update_category(cursor, conn):
utils.print_separator("UPDATE CATEGORY")
if not _display_items(cursor):
return
item_id = utils.get_int_input("Enter Item ID: ", min_val=1)
item = _get_item(cursor, item_id)
if not item:
print("Item not found.")
return
print("Categories: " + ", ".join(CATEGORIES))
while True:
new_cat = input(f"New category for '{item[1]}' (current: {item[3]}): ").strip().upper()
if new_cat in CATEGORIES:
break
print(f"Choose from: {', '.join(CATEGORIES)}")
cursor.execute("UPDATE items SET category = %s WHERE id = %s", (new_cat, item_id))
conn.commit()
print("Category updated successfully.")
_display_items(cursor)
def _update_name(cursor, conn):
utils.print_separator("UPDATE NAME")
if not _display_items(cursor):
return
item_id = utils.get_int_input("Enter Item ID: ", min_val=1)
item = _get_item(cursor, item_id)
if not item:
print("Item not found.")
return
new_name = utils.get_str_input(f"New name for '{item[1]}': ", max_length=100)
cursor.execute("UPDATE items SET name = %s WHERE id = %s", (new_name, item_id))
conn.commit()
print("Name updated successfully.")
_display_items(cursor)
def _update_stock(cursor, conn):
utils.print_separator("UPDATE STOCK")
if not _display_items(cursor):
return
item_id = utils.get_int_input("Enter Item ID: ", min_val=1)
item = _get_item(cursor, item_id)
if not item:
print("Item not found.")
return
new_stock = utils.get_int_input(
f"New stock for '{item[1]}' (current: {item[4]}): ", min_val=0
)
cursor.execute("UPDATE items SET stock = %s WHERE id = %s", (new_stock, item_id))
conn.commit()
print("Stock updated successfully.")
_display_items(cursor)
def _view_all_items(cursor):
utils.print_separator("ALL ITEMS")
_display_items(cursor)
def _view_all_users(cursor):
utils.print_separator("ALL USERS")
cursor.execute(
"SELECT id, name, email, address, created_at FROM users ORDER BY created_at DESC"
)
rows = cursor.fetchall()
if not rows:
print("No registered users.")
return
utils.print_table(rows, ["ID", "Name", "Email", "Address", "Joined"])