Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,161 +1,180 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
import re
|
| 4 |
-
import random
|
| 5 |
-
import smtplib
|
| 6 |
-
from email.mime.text import MIMEText
|
| 7 |
from datetime import datetime, timedelta
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
# Salesforce Connection
|
| 10 |
sf = Salesforce(username='diggavalli98@gmail.com', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
|
| 11 |
|
| 12 |
-
# Helper Functions
|
| 13 |
-
otp_storage = {}
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
try:
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
-
return f"Error
|
| 33 |
-
|
| 34 |
-
def validate_email(email):
|
| 35 |
-
regex = r'^\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
|
| 36 |
-
return re.match(regex, email)
|
| 37 |
|
| 38 |
-
def
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
return False
|
| 46 |
|
| 47 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
def
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
)
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
+
from simple_salesforce import Salesforce
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from datetime import datetime, timedelta
|
| 5 |
+
import random
|
| 6 |
+
|
| 7 |
+
# Salesforce Authentication
|
| 8 |
|
|
|
|
| 9 |
sf = Salesforce(username='diggavalli98@gmail.com', password='Sati@1020', security_token='sSSjyhInIsUohKpG8sHzty2q')
|
| 10 |
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
otp_data = {} # Temporary OTP storage
|
| 13 |
+
|
| 14 |
+
# Navigation state
|
| 15 |
+
current_page = "login"
|
| 16 |
+
|
| 17 |
+
def set_page(page):
|
| 18 |
+
global current_page
|
| 19 |
+
current_page = page
|
| 20 |
+
|
| 21 |
+
# Login Page Function
|
| 22 |
+
def login(email, password):
|
| 23 |
+
global current_page
|
| 24 |
try:
|
| 25 |
+
query_result = sf.query(f"SELECT Password__c FROM Customer_Login__c WHERE Email__c = '{email}'")
|
| 26 |
+
if not query_result['records']:
|
| 27 |
+
return "Error: Email not found."
|
| 28 |
+
|
| 29 |
+
stored_password = query_result['records'][0]['Password__c']
|
| 30 |
+
if password != stored_password:
|
| 31 |
+
return "Error: Invalid password."
|
| 32 |
+
|
| 33 |
+
sf.Customer_Login__c.update(query_result['records'][0]['Id'], {'Login_Status__c': 'Logged In'})
|
| 34 |
+
set_page("menu")
|
| 35 |
+
return "Login successful! Redirecting to menu..."
|
| 36 |
except Exception as e:
|
| 37 |
+
return f"Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
def login_ui():
|
| 40 |
+
return gr.Interface(
|
| 41 |
+
fn=login,
|
| 42 |
+
inputs=[gr.Textbox(label="Email"), gr.Password(label="Password")],
|
| 43 |
+
outputs="text",
|
| 44 |
+
title="Login Page"
|
| 45 |
+
)
|
|
|
|
| 46 |
|
| 47 |
+
# Signup Page Functions
|
| 48 |
+
def send_otp(email):
|
| 49 |
+
try:
|
| 50 |
+
query_result = sf.query(f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'")
|
| 51 |
+
if query_result['records']:
|
| 52 |
+
return "Error: Email already registered."
|
| 53 |
+
|
| 54 |
+
otp = random.randint(100000, 999999)
|
| 55 |
+
otp_data[email] = {
|
| 56 |
+
"otp": otp,
|
| 57 |
+
"expiration": datetime.now() + timedelta(minutes=2)
|
| 58 |
+
}
|
| 59 |
+
print(f"OTP for {email}: {otp}") # Simulated email sending
|
| 60 |
+
return "OTP sent successfully! Check your email."
|
| 61 |
+
except Exception as e:
|
| 62 |
+
return f"Error: {str(e)}"
|
| 63 |
|
| 64 |
+
def verify_otp(email, otp, name, password, phone):
|
| 65 |
+
try:
|
| 66 |
+
if email not in otp_data or otp_data[email]['expiration'] < datetime.now():
|
| 67 |
+
return "Error: OTP expired or invalid."
|
| 68 |
+
|
| 69 |
+
if otp_data[email]['otp'] != int(otp):
|
| 70 |
+
return "Error: Incorrect OTP."
|
| 71 |
+
|
| 72 |
+
if not (6 <= len(password) <= 10 and
|
| 73 |
+
any(c.isupper() for c in password) and
|
| 74 |
+
any(c.islower() for c in password) and
|
| 75 |
+
any(c.isdigit() for c in password) and
|
| 76 |
+
any(c in "!@#$%^&*" for c in password)):
|
| 77 |
+
return "Error: Password does not meet complexity requirements."
|
| 78 |
+
|
| 79 |
+
sf.Customer_Login__c.create({
|
| 80 |
+
"Name": name,
|
| 81 |
+
"Email__c": email,
|
| 82 |
+
"Password__c": password,
|
| 83 |
+
"Phone_Number__c": phone,
|
| 84 |
+
"Login_Status__c": "Logged Out"
|
| 85 |
+
})
|
| 86 |
+
del otp_data[email]
|
| 87 |
+
set_page("login")
|
| 88 |
+
return "Signup successful! Redirecting to login page..."
|
| 89 |
+
except Exception as e:
|
| 90 |
+
return f"Error: {str(e)}"
|
| 91 |
+
|
| 92 |
+
def signup_ui():
|
| 93 |
+
return gr.Interface(
|
| 94 |
+
fn=lambda email, otp, name, password, phone: (
|
| 95 |
+
send_otp(email) if otp == "" else verify_otp(email, otp, name, password, phone)
|
| 96 |
+
),
|
| 97 |
+
inputs=[
|
| 98 |
+
gr.Textbox(label="Email"),
|
| 99 |
+
gr.Textbox(label="OTP (Leave blank to send OTP)"),
|
| 100 |
+
gr.Textbox(label="Name"),
|
| 101 |
+
gr.Password(label="Password"),
|
| 102 |
+
gr.Textbox(label="Phone Number")
|
| 103 |
+
],
|
| 104 |
+
outputs="text",
|
| 105 |
+
title="Signup Page"
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
# Forgot Password Page Function
|
| 109 |
+
def forgot_password(email, otp, new_password):
|
| 110 |
+
global current_page
|
| 111 |
+
try:
|
| 112 |
+
query_result = sf.query(f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'")
|
| 113 |
+
if not query_result['records']:
|
| 114 |
+
return "Error: Email not found."
|
| 115 |
+
|
| 116 |
+
if otp == "":
|
| 117 |
+
otp = random.randint(100000, 999999)
|
| 118 |
+
otp_data[email] = {
|
| 119 |
+
"otp": otp,
|
| 120 |
+
"expiration": datetime.now() + timedelta(minutes=2)
|
| 121 |
+
}
|
| 122 |
+
print(f"OTP for {email}: {otp}")
|
| 123 |
+
return "OTP sent successfully! Check your email."
|
| 124 |
+
|
| 125 |
+
if email in otp_data and otp_data[email]['otp'] == int(otp) and otp_data[email]['expiration'] > datetime.now():
|
| 126 |
+
if not (6 <= len(new_password) <= 10 and
|
| 127 |
+
any(c.isupper() for c in new_password) and
|
| 128 |
+
any(c.islower() for c in new_password) and
|
| 129 |
+
any(c.isdigit() for c in new_password) and
|
| 130 |
+
any(c in "!@#$%^&*" for c in new_password)):
|
| 131 |
+
return "Error: Password does not meet complexity requirements."
|
| 132 |
+
|
| 133 |
+
sf.Customer_Login__c.update(query_result['records'][0]['Id'], {"Password__c": new_password})
|
| 134 |
+
del otp_data[email]
|
| 135 |
+
set_page("login")
|
| 136 |
+
return "Password updated successfully! Redirecting to login page..."
|
| 137 |
+
|
| 138 |
+
return "Error: Invalid or expired OTP."
|
| 139 |
+
except Exception as e:
|
| 140 |
+
return f"Error: {str(e)}"
|
| 141 |
+
|
| 142 |
+
def forgot_password_ui():
|
| 143 |
+
return gr.Interface(
|
| 144 |
+
fn=forgot_password,
|
| 145 |
+
inputs=[
|
| 146 |
+
gr.Textbox(label="Email"),
|
| 147 |
+
gr.Textbox(label="OTP (Leave blank to send OTP)"),
|
| 148 |
+
gr.Password(label="New Password")
|
| 149 |
+
],
|
| 150 |
+
outputs="text",
|
| 151 |
+
title="Forgot Password Page"
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
# Menu Page Function
|
| 155 |
+
def menu_page():
|
| 156 |
+
global current_page
|
| 157 |
+
return "Welcome to the Menu Page! Select your options here."
|
| 158 |
+
|
| 159 |
+
def menu_ui():
|
| 160 |
+
return gr.Interface(
|
| 161 |
+
fn=menu_page,
|
| 162 |
+
inputs=None,
|
| 163 |
+
outputs="text",
|
| 164 |
+
title="Menu Page"
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
# Main App Logic
|
| 168 |
+
def render_page():
|
| 169 |
+
if current_page == "login":
|
| 170 |
+
return login_ui()
|
| 171 |
+
elif current_page == "signup":
|
| 172 |
+
return signup_ui()
|
| 173 |
+
elif current_page == "forgot_password":
|
| 174 |
+
return forgot_password_ui()
|
| 175 |
+
elif current_page == "menu":
|
| 176 |
+
return menu_ui()
|
| 177 |
+
|
| 178 |
+
# Launch App
|
| 179 |
+
app = render_page()
|
| 180 |
+
app.launch()
|