nagasurendra commited on
Commit
1c47292
·
verified ·
1 Parent(s): 4459314

Update utils/database_handler.py

Browse files
Files changed (1) hide show
  1. utils/database_handler.py +23 -56
utils/database_handler.py CHANGED
@@ -1,65 +1,32 @@
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
 
1
+ from salesforce_auth import connect_to_salesforce
2
+ from bcrypt import hashpw, gensalt, checkpw
3
 
4
+ sf = connect_to_salesforce()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  def save_user(name, phone, email, password):
7
+ """Save user details to Salesforce."""
 
8
  try:
9
+ hashed_password = hashpw(password.encode(), gensalt()).decode()
10
+ sf.User_Login__c.create({
11
+ 'Name__c': name,
12
+ 'Phone__c': phone,
13
+ 'Email__c': email,
14
+ 'Password__c': hashed_password
15
+ })
16
+ return True
17
  except Exception as e:
18
+ print(f"Error saving user to Salesforce: {e}")
19
  return False
20
 
21
+ def check_credentials(email, password):
22
+ """Check user credentials in Salesforce."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  try:
24
+ query = f"SELECT Password__c FROM User_Login__c WHERE Email__c = '{email}'"
25
+ result = sf.query(query)
26
+ if result['records']:
27
+ stored_password = result['records'][0]['Password__c']
28
+ return checkpw(password.encode(), stored_password.encode())
29
+ return False
 
 
30
  except Exception as e:
31
+ print(f"Error checking credentials: {e}")
32
+ return False