Spaces:
Sleeping
Sleeping
Update utils/database_handler.py
Browse files- utils/database_handler.py +21 -6
utils/database_handler.py
CHANGED
|
@@ -4,24 +4,39 @@ import os
|
|
| 4 |
# Path to Excel database
|
| 5 |
CUSTOMERS_FILE = "database/customers.xlsx"
|
| 6 |
|
| 7 |
-
# Ensure database
|
| 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 |
-
#
|
| 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 |
-
#
|
| 20 |
def save_user(name, phone, email, password):
|
|
|
|
|
|
|
|
|
|
| 21 |
df = pd.read_excel(CUSTOMERS_FILE)
|
|
|
|
|
|
|
|
|
|
| 22 |
if email in df["Email"].values:
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
| 4 |
# Path to Excel database
|
| 5 |
CUSTOMERS_FILE = "database/customers.xlsx"
|
| 6 |
|
| 7 |
+
# Ensure database folder and file exist
|
| 8 |
if not os.path.exists("database"):
|
| 9 |
os.makedirs("database")
|
| 10 |
if not os.path.exists(CUSTOMERS_FILE):
|
| 11 |
+
# Create an empty Excel file with the required columns
|
| 12 |
pd.DataFrame(columns=["Name", "Phone", "Email", "Password"]).to_excel(CUSTOMERS_FILE, index=False)
|
| 13 |
|
| 14 |
+
# Function to check user credentials
|
| 15 |
def check_credentials(email, password):
|
| 16 |
+
# Read the Excel file into a DataFrame
|
| 17 |
df = pd.read_excel(CUSTOMERS_FILE)
|
| 18 |
+
# Filter for a user with the given email and password
|
| 19 |
user = df[(df["Email"] == email) & (df["Password"] == password)]
|
| 20 |
+
return not user.empty # Return True if a match is found, otherwise False
|
| 21 |
|
| 22 |
+
# Function to save a new user
|
| 23 |
def save_user(name, phone, email, password):
|
| 24 |
+
print("Attempting to save user:", name, phone, email, password) # Debugging
|
| 25 |
+
|
| 26 |
+
# Read the existing data
|
| 27 |
df = pd.read_excel(CUSTOMERS_FILE)
|
| 28 |
+
print("Existing data:\n", df) # Debugging
|
| 29 |
+
|
| 30 |
+
# Check if the email already exists
|
| 31 |
if email in df["Email"].values:
|
| 32 |
+
print("Error: Email already exists.") # Debugging
|
| 33 |
+
return False # Do not allow duplicate emails
|
| 34 |
+
|
| 35 |
+
# Add the new user to the DataFrame
|
| 36 |
new_user = {"Name": name, "Phone": phone, "Email": email, "Password": password}
|
| 37 |
df = pd.concat([df, pd.DataFrame([new_user])], ignore_index=True)
|
| 38 |
+
|
| 39 |
+
# Save the updated DataFrame to the Excel file
|
| 40 |
df.to_excel(CUSTOMERS_FILE, index=False)
|
| 41 |
+
print("User saved successfully. Updated data:\n", df) # Debugging
|
| 42 |
+
return True
|