Spaces:
Sleeping
Sleeping
Create database_handler.py
Browse files- utils/database_handler.py +27 -0
utils/database_handler.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
# Path to Excel database
|
| 5 |
+
CUSTOMERS_FILE = "database/customers.xlsx"
|
| 6 |
+
|
| 7 |
+
# Ensure database exists
|
| 8 |
+
if not os.path.exists("database"):
|
| 9 |
+
os.makedirs("database")
|
| 10 |
+
if not os.path.exists(CUSTOMERS_FILE):
|
| 11 |
+
pd.DataFrame(columns=["Name", "Phone", "Email", "Password"]).to_excel(CUSTOMERS_FILE, index=False)
|
| 12 |
+
|
| 13 |
+
# Check user credentials
|
| 14 |
+
def check_credentials(email, password):
|
| 15 |
+
df = pd.read_excel(CUSTOMERS_FILE)
|
| 16 |
+
user = df[(df["Email"] == email) & (df["Password"] == password)]
|
| 17 |
+
return not user.empty
|
| 18 |
+
|
| 19 |
+
# Save new user
|
| 20 |
+
def save_user(name, phone, email, password):
|
| 21 |
+
df = pd.read_excel(CUSTOMERS_FILE)
|
| 22 |
+
if email in df["Email"].values:
|
| 23 |
+
return False # User already exists
|
| 24 |
+
new_user = {"Name": name, "Phone": phone, "Email": email, "Password": password}
|
| 25 |
+
df = pd.concat([df, pd.DataFrame([new_user])], ignore_index=True)
|
| 26 |
+
df.to_excel(CUSTOMERS_FILE, index=False)
|
| 27 |
+
return True
|