Spaces:
Sleeping
Sleeping
Update utils/database_handler.py
Browse files- utils/database_handler.py +58 -52
utils/database_handler.py
CHANGED
|
@@ -1,65 +1,71 @@
|
|
| 1 |
-
import
|
| 2 |
-
import os
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
except Exception as e:
|
| 21 |
-
print("Error
|
| 22 |
-
return
|
| 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 |
-
|
| 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 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
"
|
| 47 |
-
"
|
| 48 |
-
"
|
| 49 |
-
"
|
| 50 |
}
|
| 51 |
-
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|