Spaces:
Sleeping
Sleeping
Create utils/database_handler.py
Browse files- utils/database_handler.py +65 -0
utils/database_handler.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
# Set a writable path for the Excel file within the app directory
|
| 5 |
+
BASE_DIR = os.path.dirname(os.path.abspath(__file__)) # Current script directory
|
| 6 |
+
CUSTOMERS_FILE = os.path.join(BASE_DIR, "..workspace/database/Book1.xlsx")
|
| 7 |
+
|
| 8 |
+
# Ensure the database directory exists
|
| 9 |
+
if not os.path.exists(os.path.dirname(CUSTOMERS_FILE)):
|
| 10 |
+
os.makedirs(os.path.dirname(CUSTOMERS_FILE)) # Create database directory
|
| 11 |
+
|
| 12 |
+
# Ensure the file exists with the required structure
|
| 13 |
+
if not os.path.exists(CUSTOMERS_FILE):
|
| 14 |
+
print("Creating new Excel file as it doesn't exist.")
|
| 15 |
+
pd.DataFrame(columns=["Name", "Phone", "Email", "Password"]).to_excel(CUSTOMERS_FILE, index=False)
|
| 16 |
+
|
| 17 |
+
def check_credentials(email, password):
|
| 18 |
+
try:
|
| 19 |
+
df = pd.read_excel(CUSTOMERS_FILE)
|
| 20 |
+
except Exception as e:
|
| 21 |
+
print("Error reading Excel file:", e)
|
| 22 |
+
return False
|
| 23 |
+
|
| 24 |
+
user = df[(df["Email"] == email) & (df["Password"] == password)]
|
| 25 |
+
return not user.empty
|
| 26 |
+
|
| 27 |
+
def save_user(name, phone, email, password):
|
| 28 |
+
print("Attempting to save user:", name, phone, email, password)
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
# Read the existing data
|
| 32 |
+
df = pd.read_excel(CUSTOMERS_FILE)
|
| 33 |
+
except Exception as e:
|
| 34 |
+
print("Error reading Excel file:", e)
|
| 35 |
+
return False
|
| 36 |
+
|
| 37 |
+
print("Existing data before appending:\n", df)
|
| 38 |
+
|
| 39 |
+
# Check if email already exists
|
| 40 |
+
if email in df["Email"].values:
|
| 41 |
+
print("Error: Email already exists.")
|
| 42 |
+
return False
|
| 43 |
+
|
| 44 |
+
# Add new user data
|
| 45 |
+
new_user = {
|
| 46 |
+
"Name": str(name).strip(),
|
| 47 |
+
"Phone": str(phone).strip(),
|
| 48 |
+
"Email": str(email).strip(),
|
| 49 |
+
"Password": str(password).strip()
|
| 50 |
+
}
|
| 51 |
+
df = pd.concat([df, pd.DataFrame([new_user])], ignore_index=True)
|
| 52 |
+
|
| 53 |
+
# Save updated data back to the Excel file
|
| 54 |
+
try:
|
| 55 |
+
with pd.ExcelWriter(CUSTOMERS_FILE, engine="openpyxl", mode="w") as writer:
|
| 56 |
+
df.to_excel(writer, index=False)
|
| 57 |
+
print("User saved successfully. Updated data:\n", df)
|
| 58 |
+
|
| 59 |
+
# Confirm save by re-reading the file
|
| 60 |
+
df_check = pd.read_excel(CUSTOMERS_FILE)
|
| 61 |
+
print("Data in file after saving:\n", df_check)
|
| 62 |
+
return True
|
| 63 |
+
except Exception as e:
|
| 64 |
+
print("Error while saving to Excel:", e)
|
| 65 |
+
return False
|