Yaswanth56 commited on
Commit
a3f18b1
·
verified ·
1 Parent(s): 3cb1c32

Create flask-salesforce-app/app.py

Browse files
Files changed (1) hide show
  1. flask-salesforce-app/app.py +110 -0
flask-salesforce-app/app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, render_template, redirect, url_for
2
+ from simple_salesforce import Salesforce
3
+ from dotenv import load_dotenv
4
+ import os
5
+
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(
15
+ username=os.getenv("SF_USERNAME"),
16
+ password=os.getenv("SF_PASSWORD"),
17
+ security_token=os.getenv("SF_SECURITY_TOKEN"),
18
+ domain="login" # Use "test" for sandbox
19
+ )
20
+ print("Salesforce connection successful!")
21
+ except Exception as e:
22
+ print(f"Salesforce connection failed: {e}")
23
+ sf = None
24
+
25
+ @app.route('/')
26
+ def login():
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
+
43
+ if result['totalSize'] == 0:
44
+ return "Invalid Login Details"
45
+
46
+ customer = result['records'][0]
47
+ reward_points = customer['Reward_Points__c']
48
+
49
+ # Redirect to rewards page
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
+ @app.route('/rewards/<customer_id>')
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']
74
+ gst = 0.18 * bill_amount
75
+
76
+ if points >= 500 and apply_rewards:
77
+ discount = 0.1 * bill_amount
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:
85
+ discount = 0
86
+ earned_points = 0.1 * bill_amount
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
+ # Render the summary page
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
+ if __name__ == '__main__':
109
+ from waitress import serve
110
+ serve(app, host="0.0.0.0", port=8080)