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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -30
app.py CHANGED
@@ -1,43 +1,85 @@
1
  import gradio as gr
2
  import pandas as pd
3
  from bcrypt import hashpw, gensalt, checkpw
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- # File for storing user data
6
- USER_FILE = "users.xlsx"
7
 
8
- # Utility Functions
9
- def save_user(name, phone, email, password):
10
- """Save user details to Excel file."""
11
  try:
12
- # Load existing users
13
- try:
14
- users = pd.read_excel(USER_FILE)
15
- except FileNotFoundError:
16
- users = pd.DataFrame(columns=["Name", "Phone", "Email", "Password"])
17
-
18
- # Check if email already exists
19
- if email in users["Email"].values:
20
- return False # User already exists
21
-
22
- # Add new user
23
- hashed_password = hashpw(password.encode(), gensalt()).decode()
24
- new_user = {"Name": name, "Phone": phone, "Email": email, "Password": hashed_password}
25
- users = pd.concat([users, pd.DataFrame([new_user])], ignore_index=True)
26
- users.to_excel(USER_FILE, index=False)
27
- return True
28
  except Exception as e:
29
- print(f"Error saving user: {e}")
30
  return False
31
 
32
- def check_credentials(email, password):
33
- """Check user credentials during login."""
 
34
  try:
35
- users = pd.read_excel(USER_FILE)
36
- user = users[users["Email"] == email]
37
- if not user.empty:
38
- return checkpw(password.encode(), user.iloc[0]["Password"].encode())
39
- return False
40
- except FileNotFoundError:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  return False
42
 
43
  # Function to load the menu data
 
1
  import gradio as gr
2
  import pandas as pd
3
  from bcrypt import hashpw, gensalt, checkpw
4
+ mport requests
5
+ import json
6
+
7
+ # Salesforce Credentials
8
+ CONSUMER_KEY = "3MVG9PwZx9R6_UrcDlsRUsfM9CGTVKE82QnI5Vz02b.lv3H2yjDv_wy9.nrYihWYLUD1bv08N_qCG_gxiV5y8"
9
+ CONSUMER_SECRET = "94983416FB1DF4E754B56F69982F07D94D8971523E6893B8A62D75CD8E9E3E34"
10
+ USERNAME = "surendra@sathkrutha.ccom"
11
+ PASSWORD = "Lavanyanaga@123"
12
+ SECURITY_TOKEN = "GH9RG97LroDoLe6gAAOHaJBP"
13
+
14
+ # Salesforce Authentication
15
+ AUTH_URL = "https://login.salesforce.com/services/oauth2/token"
16
+ BASE_URL = None
17
+ HEADERS = None
18
+
19
+
20
+ def authenticate_salesforce():
21
+ global BASE_URL, HEADERS
22
+ data = {
23
+ "grant_type": "password",
24
+ "client_id": CONSUMER_KEY,
25
+ "client_secret": CONSUMER_SECRET,
26
+ "username": USERNAME,
27
+ "password": PASSWORD + SECURITY_TOKEN,
28
+ }
29
+ response = requests.post(AUTH_URL, data=data)
30
+ if response.status_code == 200:
31
+ auth_data = response.json()
32
+ BASE_URL = auth_data["instance_url"]
33
+ HEADERS = {
34
+ "Authorization": f"Bearer {auth_data['access_token']}",
35
+ "Content-Type": "application/json",
36
+ }
37
+ else:
38
+ raise Exception(f"Failed to authenticate with Salesforce: {response.text}")
39
 
 
 
40
 
41
+ def check_credentials(email, password):
42
+ """Check user credentials during login."""
 
43
  try:
44
+ authenticate_salesforce()
45
+ query = f"SELECT Password__c FROM User_Login__c WHERE Email__c = '{email}'"
46
+ response = requests.get(f"{BASE_URL}/services/data/v56.0/query?q={query}", headers=HEADERS)
47
+ if response.status_code == 200:
48
+ records = response.json().get("records", [])
49
+ if records and records[0]["Password__c"] == password:
50
+ return True
51
+ return False
 
 
 
 
 
 
 
 
52
  except Exception as e:
53
+ print(f"Error checking credentials: {e}")
54
  return False
55
 
56
+
57
+ def save_user(name, phone, email, password):
58
+ """Save user details to Salesforce."""
59
  try:
60
+ authenticate_salesforce()
61
+ # Check if email already exists
62
+ query = f"SELECT Id FROM User_Login__c WHERE Email__c = '{email}'"
63
+ response = requests.get(f"{BASE_URL}/services/data/v56.0/query?q={query}", headers=HEADERS)
64
+ if response.status_code == 200:
65
+ if response.json().get("totalSize", 0) > 0:
66
+ return False # Email already exists
67
+
68
+ # Create new user
69
+ user_data = {
70
+ "Name__c": name,
71
+ "Phone__c": phone,
72
+ "Email__c": email,
73
+ "Password__c": password,
74
+ }
75
+ response = requests.post(f"{BASE_URL}/services/data/v56.0/sobjects/User_Login__c/", headers=HEADERS, json=user_data)
76
+ if response.status_code == 201:
77
+ return True
78
+ else:
79
+ print(f"Error saving user: {response.text}")
80
+ return False
81
+ except Exception as e:
82
+ print(f"Error saving user: {e}")
83
  return False
84
 
85
  # Function to load the menu data