Spaces:
Sleeping
Sleeping
Update utils/database_handler.py
Browse files- utils/database_handler.py +19 -6
utils/database_handler.py
CHANGED
|
@@ -14,7 +14,14 @@ if not os.path.exists(CUSTOMERS_FILE):
|
|
| 14 |
|
| 15 |
# Function to check user credentials
|
| 16 |
def check_credentials(email, password):
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
user = df[(df["Email"] == email) & (df["Password"] == password)]
|
| 19 |
return not user.empty # Return True if credentials are valid
|
| 20 |
|
|
@@ -37,15 +44,21 @@ def save_user(name, phone, email, password):
|
|
| 37 |
return False
|
| 38 |
|
| 39 |
# Add the new user
|
| 40 |
-
new_user = {
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
df =
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# Save back to Excel
|
| 47 |
try:
|
| 48 |
-
with pd.ExcelWriter(CUSTOMERS_FILE, engine=
|
| 49 |
df.to_excel(writer, index=False)
|
| 50 |
print("User saved successfully. Updated data:\n", df) # Debugging
|
| 51 |
return True
|
|
|
|
| 14 |
|
| 15 |
# Function to check user credentials
|
| 16 |
def check_credentials(email, password):
|
| 17 |
+
# Load data from Excel
|
| 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 |
+
# Check for matching email and password
|
| 25 |
user = df[(df["Email"] == email) & (df["Password"] == password)]
|
| 26 |
return not user.empty # Return True if credentials are valid
|
| 27 |
|
|
|
|
| 44 |
return False
|
| 45 |
|
| 46 |
# Add the new user
|
| 47 |
+
new_user = {
|
| 48 |
+
"Name": str(name).strip(),
|
| 49 |
+
"Phone": str(phone).strip(),
|
| 50 |
+
"Email": str(email).strip(),
|
| 51 |
+
"Password": str(password).strip()
|
| 52 |
+
}
|
| 53 |
|
| 54 |
+
# Append the new user and reorder columns
|
| 55 |
+
df = pd.concat([df, pd.DataFrame([new_user])], ignore_index=True)
|
| 56 |
+
df = df[["Name", "Phone", "Email", "Password"]] # Ensure correct column order
|
| 57 |
+
df = df.astype(str) # Convert all columns to strings
|
| 58 |
|
| 59 |
# Save back to Excel
|
| 60 |
try:
|
| 61 |
+
with pd.ExcelWriter(CUSTOMERS_FILE, engine="openpyxl") as writer:
|
| 62 |
df.to_excel(writer, index=False)
|
| 63 |
print("User saved successfully. Updated data:\n", df) # Debugging
|
| 64 |
return True
|