Update flask-salesforce-app/app.py
Browse files- flask-salesforce-app/app.py +42 -52
flask-salesforce-app/app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
|
| 2 |
from simple_salesforce import Salesforce
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
import os
|
|
@@ -6,9 +6,6 @@ import os
|
|
| 6 |
# Load environment variables
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
-
# Initialize Flask app
|
| 10 |
-
app = Flask(__name__)
|
| 11 |
-
|
| 12 |
# Salesforce connection
|
| 13 |
try:
|
| 14 |
sf = Salesforce(
|
|
@@ -22,21 +19,12 @@ except Exception as e:
|
|
| 22 |
print(f"Salesforce connection failed: {e}")
|
| 23 |
sf = None
|
| 24 |
|
| 25 |
-
|
| 26 |
-
def
|
| 27 |
-
return render_template('login.html')
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
@app.route('/auth', methods=['POST'])
|
| 31 |
-
def auth():
|
| 32 |
-
email = request.form['email']
|
| 33 |
-
password = request.form['password']
|
| 34 |
-
|
| 35 |
if not sf:
|
| 36 |
return "Salesforce connection failed. Please check credentials and try again."
|
| 37 |
|
| 38 |
try:
|
| 39 |
-
# Query Salesforce for user authentication
|
| 40 |
query = f"SELECT Id, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{email}' AND Password__c = '{password}'"
|
| 41 |
result = sf.query(query)
|
| 42 |
|
|
@@ -46,28 +34,12 @@ def auth():
|
|
| 46 |
customer = result['records'][0]
|
| 47 |
reward_points = customer['Reward_Points__c']
|
| 48 |
|
| 49 |
-
|
| 50 |
-
return redirect(url_for('rewards', customer_id=customer['Id'], points=reward_points))
|
| 51 |
except Exception as e:
|
| 52 |
return f"Error during authentication: {e}"
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
def rewards(customer_id):
|
| 57 |
-
try:
|
| 58 |
-
customer = sf.Customer_Login__c.get(customer_id)
|
| 59 |
-
points = customer['Reward_Points__c']
|
| 60 |
-
return render_template('rewards.html', points=points, customer_id=customer_id)
|
| 61 |
-
except Exception as e:
|
| 62 |
-
return f"Error fetching rewards: {e}"
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
@app.route('/apply_rewards', methods=['POST'])
|
| 66 |
-
def apply_rewards():
|
| 67 |
-
customer_id = request.form['customer_id']
|
| 68 |
-
bill_amount = float(request.form['bill_amount'])
|
| 69 |
-
apply_rewards = request.form.get('apply_rewards')
|
| 70 |
-
|
| 71 |
try:
|
| 72 |
customer = sf.Customer_Login__c.get(customer_id)
|
| 73 |
points = customer['Reward_Points__c']
|
|
@@ -78,7 +50,6 @@ def apply_rewards():
|
|
| 78 |
final_bill = bill_amount - discount + gst
|
| 79 |
updated_points = points - 500
|
| 80 |
|
| 81 |
-
# Update the customer's reward points in Salesforce
|
| 82 |
sf.Customer_Login__c.update(customer_id, {'Reward_Points__c': updated_points})
|
| 83 |
message = "You saved 10% on your total bill!"
|
| 84 |
else:
|
|
@@ -87,24 +58,43 @@ def apply_rewards():
|
|
| 87 |
final_bill = bill_amount + gst
|
| 88 |
updated_points = points + earned_points
|
| 89 |
|
| 90 |
-
# Update the customer's reward points in Salesforce
|
| 91 |
sf.Customer_Login__c.update(customer_id, {'Reward_Points__c': updated_points})
|
| 92 |
message = f"You earned {earned_points:.2f} reward points!"
|
| 93 |
|
| 94 |
-
|
| 95 |
-
return render_template(
|
| 96 |
-
'apply_rewards.html',
|
| 97 |
-
original_bill=bill_amount,
|
| 98 |
-
discount=discount,
|
| 99 |
-
gst=gst,
|
| 100 |
-
final_bill=final_bill,
|
| 101 |
-
updated_points=updated_points,
|
| 102 |
-
message=message
|
| 103 |
-
)
|
| 104 |
except Exception as e:
|
| 105 |
-
return f"Error applying rewards: {e}"
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
from simple_salesforce import Salesforce
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
import os
|
|
|
|
| 6 |
# Load environment variables
|
| 7 |
load_dotenv()
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
# Salesforce connection
|
| 10 |
try:
|
| 11 |
sf = Salesforce(
|
|
|
|
| 19 |
print(f"Salesforce connection failed: {e}")
|
| 20 |
sf = None
|
| 21 |
|
| 22 |
+
# Function to handle user authentication and Salesforce login
|
| 23 |
+
def authenticate_user(email, password):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
if not sf:
|
| 25 |
return "Salesforce connection failed. Please check credentials and try again."
|
| 26 |
|
| 27 |
try:
|
|
|
|
| 28 |
query = f"SELECT Id, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{email}' AND Password__c = '{password}'"
|
| 29 |
result = sf.query(query)
|
| 30 |
|
|
|
|
| 34 |
customer = result['records'][0]
|
| 35 |
reward_points = customer['Reward_Points__c']
|
| 36 |
|
| 37 |
+
return f"Welcome, you have {reward_points} points. Proceed to rewards."
|
|
|
|
| 38 |
except Exception as e:
|
| 39 |
return f"Error during authentication: {e}"
|
| 40 |
|
| 41 |
+
# Function to handle reward points logic
|
| 42 |
+
def handle_rewards(customer_id, bill_amount, apply_rewards):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
try:
|
| 44 |
customer = sf.Customer_Login__c.get(customer_id)
|
| 45 |
points = customer['Reward_Points__c']
|
|
|
|
| 50 |
final_bill = bill_amount - discount + gst
|
| 51 |
updated_points = points - 500
|
| 52 |
|
|
|
|
| 53 |
sf.Customer_Login__c.update(customer_id, {'Reward_Points__c': updated_points})
|
| 54 |
message = "You saved 10% on your total bill!"
|
| 55 |
else:
|
|
|
|
| 58 |
final_bill = bill_amount + gst
|
| 59 |
updated_points = points + earned_points
|
| 60 |
|
|
|
|
| 61 |
sf.Customer_Login__c.update(customer_id, {'Reward_Points__c': updated_points})
|
| 62 |
message = f"You earned {earned_points:.2f} reward points!"
|
| 63 |
|
| 64 |
+
return message, final_bill, updated_points
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
except Exception as e:
|
| 66 |
+
return f"Error applying rewards: {e}", 0, 0
|
| 67 |
+
|
| 68 |
+
# Define the Gradio interface
|
| 69 |
+
def create_interface():
|
| 70 |
+
with gr.Blocks() as demo:
|
| 71 |
+
gr.Markdown("### Login to your account")
|
| 72 |
+
email_input = gr.Textbox(label="Email", placeholder="Enter your email")
|
| 73 |
+
password_input = gr.Textbox(label="Password", placeholder="Enter your password", type="password")
|
| 74 |
+
login_button = gr.Button("Login")
|
| 75 |
+
login_output = gr.Textbox(label="Status")
|
| 76 |
+
|
| 77 |
+
# Action for login button
|
| 78 |
+
login_button.click(authenticate_user, inputs=[email_input, password_input], outputs=login_output)
|
| 79 |
+
|
| 80 |
+
# Reward points section
|
| 81 |
+
gr.Markdown("### Reward Points Section")
|
| 82 |
+
customer_id_input = gr.Textbox(label="Customer ID", placeholder="Enter Customer ID")
|
| 83 |
+
bill_amount_input = gr.Number(label="Enter Bill Amount", min=0)
|
| 84 |
+
apply_rewards_checkbox = gr.Checkbox(label="Apply Reward Points", value=True)
|
| 85 |
+
rewards_button = gr.Button("Calculate Bill")
|
| 86 |
+
rewards_message = gr.Textbox(label="Message")
|
| 87 |
+
final_bill_output = gr.Textbox(label="Final Bill Amount")
|
| 88 |
+
remaining_points_output = gr.Textbox(label="Remaining Points")
|
| 89 |
+
|
| 90 |
+
# Action for rewards calculation
|
| 91 |
+
rewards_button.click(handle_rewards,
|
| 92 |
+
inputs=[customer_id_input, bill_amount_input, apply_rewards_checkbox],
|
| 93 |
+
outputs=[rewards_message, final_bill_output, remaining_points_output])
|
| 94 |
+
|
| 95 |
+
return demo
|
| 96 |
+
|
| 97 |
+
# Run the Gradio interface
|
| 98 |
+
if __name__ == "__main__":
|
| 99 |
+
demo = create_interface()
|
| 100 |
+
demo.launch()
|