Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from flask import Flask, render_template,
|
2 |
from simple_salesforce import Salesforce
|
3 |
from dotenv import load_dotenv
|
4 |
import os
|
@@ -29,7 +29,7 @@ except Exception as e:
|
|
29 |
# Route for login page
|
30 |
@app.route('/')
|
31 |
def login():
|
32 |
-
return render_template('login.html'
|
33 |
|
34 |
# Route to process login
|
35 |
@app.route('/auth', methods=['POST'])
|
@@ -38,7 +38,7 @@ def auth():
|
|
38 |
password = request.form['password']
|
39 |
|
40 |
if not sf:
|
41 |
-
return
|
42 |
|
43 |
try:
|
44 |
# Query Salesforce for user authentication
|
@@ -46,16 +46,15 @@ def auth():
|
|
46 |
result = sf.query(query)
|
47 |
|
48 |
if result['totalSize'] == 0:
|
49 |
-
return
|
50 |
|
51 |
customer = result['records'][0]
|
52 |
reward_points = customer['Reward_Points__c']
|
53 |
|
54 |
-
# Redirect to rewards page
|
55 |
return redirect(url_for('rewards', customer_id=customer['Id'], points=reward_points))
|
56 |
-
|
57 |
except Exception as e:
|
58 |
-
return
|
59 |
|
60 |
# Route to display rewards page
|
61 |
@app.route('/rewards/<customer_id>')
|
@@ -69,5 +68,51 @@ def rewards(customer_id):
|
|
69 |
except Exception as e:
|
70 |
return f"Error fetching rewards: {e}"
|
71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
if __name__ == '__main__':
|
73 |
-
app.run(debug=True, host="0.0.0.0", port=7860)
|
|
|
1 |
+
from flask import Flask, render_template, redirect, request, url_for
|
2 |
from simple_salesforce import Salesforce
|
3 |
from dotenv import load_dotenv
|
4 |
import os
|
|
|
29 |
# Route for login page
|
30 |
@app.route('/')
|
31 |
def login():
|
32 |
+
return render_template('login.html')
|
33 |
|
34 |
# Route to process login
|
35 |
@app.route('/auth', methods=['POST'])
|
|
|
38 |
password = request.form['password']
|
39 |
|
40 |
if not sf:
|
41 |
+
return "Salesforce connection failed. Please check credentials and try again."
|
42 |
|
43 |
try:
|
44 |
# Query Salesforce for user authentication
|
|
|
46 |
result = sf.query(query)
|
47 |
|
48 |
if result['totalSize'] == 0:
|
49 |
+
return "Invalid Login Details"
|
50 |
|
51 |
customer = result['records'][0]
|
52 |
reward_points = customer['Reward_Points__c']
|
53 |
|
54 |
+
# Redirect to rewards page
|
55 |
return redirect(url_for('rewards', customer_id=customer['Id'], points=reward_points))
|
|
|
56 |
except Exception as e:
|
57 |
+
return f"Error during authentication: {e}"
|
58 |
|
59 |
# Route to display rewards page
|
60 |
@app.route('/rewards/<customer_id>')
|
|
|
68 |
except Exception as e:
|
69 |
return f"Error fetching rewards: {e}"
|
70 |
|
71 |
+
# Route to apply rewards
|
72 |
+
@app.route('/apply_rewards', methods=['POST'])
|
73 |
+
def apply_rewards():
|
74 |
+
customer_id = request.form['customer_id']
|
75 |
+
bill_amount = float(request.form['bill_amount'])
|
76 |
+
apply_rewards = request.form.get('apply_rewards')
|
77 |
+
|
78 |
+
try:
|
79 |
+
customer = sf.Customer_Login__c.get(customer_id)
|
80 |
+
points = customer['Reward_Points__c']
|
81 |
+
gst = 0.18 * bill_amount
|
82 |
+
|
83 |
+
if points >= 500 and apply_rewards:
|
84 |
+
discount = 0.1 * bill_amount
|
85 |
+
final_bill = bill_amount - discount + gst
|
86 |
+
updated_points = points - 500
|
87 |
+
|
88 |
+
# Update the customer's reward points in Salesforce
|
89 |
+
sf.Customer_Login__c.update(customer_id, {'Reward_Points__c': updated_points})
|
90 |
+
|
91 |
+
message = "You saved 10% on your total bill!"
|
92 |
+
else:
|
93 |
+
# Customers with below 500 points earn 10% of their bill amount as reward points
|
94 |
+
discount = 0
|
95 |
+
earned_points = 0.1 * bill_amount
|
96 |
+
final_bill = bill_amount + gst
|
97 |
+
updated_points = points + earned_points
|
98 |
+
|
99 |
+
# Update the customer's reward points in Salesforce
|
100 |
+
sf.Customer_Login__c.update(customer_id, {'Reward_Points__c': updated_points})
|
101 |
+
|
102 |
+
message = f"You earned 10% of your bill amount ({earned_points} points) as reward points!"
|
103 |
+
|
104 |
+
# Render the summary page with Dollar symbol
|
105 |
+
return render_template(
|
106 |
+
'apply_rewards.html',
|
107 |
+
original_bill=bill_amount,
|
108 |
+
discount=discount,
|
109 |
+
gst=gst,
|
110 |
+
final_bill=final_bill,
|
111 |
+
updated_points=updated_points,
|
112 |
+
message=message
|
113 |
+
)
|
114 |
+
except Exception as e:
|
115 |
+
return f"Error applying rewards: {e}"
|
116 |
+
|
117 |
if __name__ == '__main__':
|
118 |
+
app.run(debug=True, host="0.0.0.0", port=7860)
|