Spaces:
Sleeping
Sleeping
Update utils/database_handler.py
Browse files- utils/database_handler.py +70 -22
utils/database_handler.py
CHANGED
|
@@ -1,32 +1,80 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 23 |
try:
|
|
|
|
| 24 |
query = f"SELECT Password__c FROM User_Login__c WHERE Email__c = '{email}'"
|
| 25 |
-
|
| 26 |
-
if
|
| 27 |
-
|
| 28 |
-
|
|
|
|
| 29 |
return False
|
| 30 |
except Exception as e:
|
| 31 |
print(f"Error checking credentials: {e}")
|
| 32 |
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import json
|
| 3 |
|
| 4 |
+
# Salesforce Credentials
|
| 5 |
+
CONSUMER_KEY = "3MVG9PwZx9R6_UrcDlsRUsfM9CGTVKE82QnI5Vz02b.lv3H2yjDv_wy9.nrYihWYLUD1bv08N_qCG_gxiV5y8"
|
| 6 |
+
CONSUMER_SECRET = "94983416FB1DF4E754B56F69982F07D94D8971523E6893B8A62D75CD8E9E3E34"
|
| 7 |
+
USERNAME = "surendra@sathkrutha.ccom"
|
| 8 |
+
PASSWORD = "Lavanyanaga@123"
|
| 9 |
+
SECURITY_TOKEN = "GH9RG97LroDoLe6gAAOHaJBP"
|
| 10 |
+
|
| 11 |
+
# Salesforce Authentication
|
| 12 |
+
AUTH_URL = "https://login.salesforce.com/services/oauth2/token"
|
| 13 |
+
BASE_URL = None
|
| 14 |
+
HEADERS = None
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def authenticate_salesforce():
|
| 18 |
+
global BASE_URL, HEADERS
|
| 19 |
+
data = {
|
| 20 |
+
"grant_type": "password",
|
| 21 |
+
"client_id": CONSUMER_KEY,
|
| 22 |
+
"client_secret": CONSUMER_SECRET,
|
| 23 |
+
"username": USERNAME,
|
| 24 |
+
"password": PASSWORD + SECURITY_TOKEN,
|
| 25 |
+
}
|
| 26 |
+
response = requests.post(AUTH_URL, data=data)
|
| 27 |
+
if response.status_code == 200:
|
| 28 |
+
auth_data = response.json()
|
| 29 |
+
BASE_URL = auth_data["instance_url"]
|
| 30 |
+
HEADERS = {
|
| 31 |
+
"Authorization": f"Bearer {auth_data['access_token']}",
|
| 32 |
+
"Content-Type": "application/json",
|
| 33 |
+
}
|
| 34 |
+
else:
|
| 35 |
+
raise Exception(f"Failed to authenticate with Salesforce: {response.text}")
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
def check_credentials(email, password):
|
| 39 |
+
"""Check user credentials during login."""
|
| 40 |
try:
|
| 41 |
+
authenticate_salesforce()
|
| 42 |
query = f"SELECT Password__c FROM User_Login__c WHERE Email__c = '{email}'"
|
| 43 |
+
response = requests.get(f"{BASE_URL}/services/data/v56.0/query?q={query}", headers=HEADERS)
|
| 44 |
+
if response.status_code == 200:
|
| 45 |
+
records = response.json().get("records", [])
|
| 46 |
+
if records and records[0]["Password__c"] == password:
|
| 47 |
+
return True
|
| 48 |
return False
|
| 49 |
except Exception as e:
|
| 50 |
print(f"Error checking credentials: {e}")
|
| 51 |
return False
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def save_user(name, phone, email, password):
|
| 55 |
+
"""Save user details to Salesforce."""
|
| 56 |
+
try:
|
| 57 |
+
authenticate_salesforce()
|
| 58 |
+
# Check if email already exists
|
| 59 |
+
query = f"SELECT Id FROM User_Login__c WHERE Email__c = '{email}'"
|
| 60 |
+
response = requests.get(f"{BASE_URL}/services/data/v56.0/query?q={query}", headers=HEADERS)
|
| 61 |
+
if response.status_code == 200:
|
| 62 |
+
if response.json().get("totalSize", 0) > 0:
|
| 63 |
+
return False # Email already exists
|
| 64 |
+
|
| 65 |
+
# Create new user
|
| 66 |
+
user_data = {
|
| 67 |
+
"Name__c": name,
|
| 68 |
+
"Phone__c": phone,
|
| 69 |
+
"Email__c": email,
|
| 70 |
+
"Password__c": password,
|
| 71 |
+
}
|
| 72 |
+
response = requests.post(f"{BASE_URL}/services/data/v56.0/sobjects/User_Login__c/", headers=HEADERS, json=user_data)
|
| 73 |
+
if response.status_code == 201:
|
| 74 |
+
return True
|
| 75 |
+
else:
|
| 76 |
+
print(f"Error saving user: {response.text}")
|
| 77 |
+
return False
|
| 78 |
+
except Exception as e:
|
| 79 |
+
print(f"Error saving user: {e}")
|
| 80 |
+
return False
|