nagasurendra commited on
Commit
ea315e3
·
verified ·
1 Parent(s): 57462dd

Update utils/database_handler.py

Browse files
Files changed (1) hide show
  1. utils/database_handler.py +58 -52
utils/database_handler.py CHANGED
@@ -1,65 +1,71 @@
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
+ import requests
 
2
 
3
+ # Salesforce credentials
4
+ SALESFORCE_URL = "https://login.salesforce.com/services/oauth2/token"
5
+ CLIENT_ID = "3MVG9PwZx9R6_UrcDlsRUsfM9CGTVKE82QnI5Vz02b.lv3H2yjDv_wy9.nrYihWYLUD1bv08N_qCG_gxiV5y8"
6
+ CLIENT_SECRET = "94983416FB1DF4E754B56F69982F07D94D8971523E6893B8A62D75CD8E9E3A34"
7
+ USERNAME = "surendra@sathkrutha.ccom"
8
+ PASSWORD = "Lavanyanaga@123"
9
+ SECURITY_TOKEN = "GH9RG97LroDoLe6gAAOHaJBP"
10
 
11
+ # Authenticate with Salesforce
12
+ def get_salesforce_access_token():
 
 
 
 
 
 
 
 
13
  try:
14
+ response = requests.post(
15
+ SALESFORCE_URL,
16
+ data={
17
+ "grant_type": "password",
18
+ "client_id": CLIENT_ID,
19
+ "client_secret": CLIENT_SECRET,
20
+ "username": USERNAME,
21
+ "password": PASSWORD + SECURITY_TOKEN,
22
+ },
23
+ )
24
+ response.raise_for_status()
25
+ return response.json()["access_token"], response.json()["instance_url"]
26
  except Exception as e:
27
+ print("Error obtaining Salesforce access token:", e)
28
+ return None, None
 
 
 
29
 
30
+ # Save user to Salesforce
31
  def save_user(name, phone, email, password):
32
+ access_token, instance_url = get_salesforce_access_token()
33
+ if not access_token:
 
 
 
 
 
34
  return False
35
 
 
 
36
  # Check if email already exists
37
+ headers = {
38
+ "Authorization": f"Bearer {access_token}",
39
+ "Content-Type": "application/json",
40
+ }
41
+ query = f"SELECT Email__c FROM User_Login__c WHERE Email__c = '{email}'"
42
+ query_response = requests.get(f"{instance_url}/services/data/v53.0/query", headers=headers, params={"q": query})
43
+ if query_response.status_code == 200 and query_response.json()["records"]:
44
+ return False # User already exists
45
 
46
+ # Save new user
47
+ data = {
48
+ "Name__c": name,
49
+ "Phone__c": phone,
50
+ "Email__c": email,
51
+ "Password__c": password,
52
  }
53
+ response = requests.post(f"{instance_url}/services/data/v53.0/sobjects/User_Login__c", headers=headers, json=data)
54
+ return response.status_code == 201
55
 
56
+ # Check user credentials
57
+ def check_credentials(email, password):
58
+ access_token, instance_url = get_salesforce_access_token()
59
+ if not access_token:
60
+ return False
61
 
62
+ headers = {
63
+ "Authorization": f"Bearer {access_token}",
64
+ "Content-Type": "application/json",
65
+ }
66
+ query = f"SELECT Password__c FROM User_Login__c WHERE Email__c = '{email}'"
67
+ query_response = requests.get(f"{instance_url}/services/data/v53.0/query", headers=headers, params={"q": query})
68
+ if query_response.status_code == 200 and query_response.json()["records"]:
69
+ stored_password = query_response.json()["records"][0]["Password__c"]
70
+ return stored_password == password
71
+ return False