Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,29 +1,26 @@
|
|
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
|
5 |
-
|
6 |
-
# Load environment variables from .env file
|
7 |
-
load_dotenv()
|
8 |
|
9 |
app = Flask(__name__)
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
27 |
|
28 |
# Route for login page
|
29 |
@app.route('/')
|
@@ -31,87 +28,43 @@ def login():
|
|
31 |
return render_template('login.html')
|
32 |
|
33 |
# Route to process login
|
34 |
-
@app.route('/
|
35 |
-
def
|
36 |
email = request.form['email']
|
37 |
password = request.form['password']
|
38 |
-
|
39 |
-
if
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
#
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
return "Invalid Login Details"
|
49 |
-
|
50 |
-
customer = result['records'][0]
|
51 |
-
reward_points = customer['Reward_Points__c']
|
52 |
-
|
53 |
-
# Redirect to rewards page
|
54 |
-
return redirect(url_for('rewards', customer_id=customer['Id'], points=reward_points))
|
55 |
-
except Exception as e:
|
56 |
-
return f"Error during authentication: {e}"
|
57 |
|
58 |
# Route to display rewards page
|
59 |
-
@app.route('/rewards/<
|
60 |
-
def rewards(
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
return
|
69 |
|
70 |
# Route to apply rewards
|
71 |
@app.route('/apply_rewards', methods=['POST'])
|
72 |
def apply_rewards():
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
if points >= 500 and apply_rewards:
|
83 |
-
discount = 0.1 * bill_amount
|
84 |
-
final_bill = bill_amount - discount + gst
|
85 |
-
updated_points = points - 500
|
86 |
-
|
87 |
-
# Update the customer's reward points in Salesforce
|
88 |
-
sf.Customer_Login__c.update(customer_id, {'Reward_Points__c': updated_points})
|
89 |
-
|
90 |
-
message = "You saved 10% on your total bill!"
|
91 |
-
else:
|
92 |
-
# Customers with below 500 points earn 10% of their bill amount as reward points
|
93 |
-
discount = 0
|
94 |
-
earned_points = 0.1 * bill_amount
|
95 |
-
final_bill = bill_amount + gst
|
96 |
-
updated_points = points + earned_points
|
97 |
-
|
98 |
-
# Update the customer's reward points in Salesforce
|
99 |
-
sf.Customer_Login__c.update(customer_id, {'Reward_Points__c': updated_points})
|
100 |
-
|
101 |
-
message = f"You earned 10% of your bill amount ({earned_points} points) as reward points!"
|
102 |
-
|
103 |
-
# Render the summary page
|
104 |
-
return render_template(
|
105 |
-
'apply_rewards.html',
|
106 |
-
original_bill=bill_amount,
|
107 |
-
discount=discount,
|
108 |
-
gst=gst,
|
109 |
-
final_bill=final_bill,
|
110 |
-
updated_points=updated_points,
|
111 |
-
message=message
|
112 |
-
)
|
113 |
-
except Exception as e:
|
114 |
-
return f"Error applying rewards: {e}"
|
115 |
-
|
116 |
-
if __name__ == '__main__':
|
117 |
app.run(debug=True)
|
|
|
1 |
from flask import Flask, render_template, redirect, request, url_for
|
2 |
from simple_salesforce import Salesforce
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
app = Flask(__name__)
|
5 |
|
6 |
+
# Connect to Salesforce (replace with your Salesforce credentials)
|
7 |
+
sf = Salesforce(username='your_username', password='your_password', security_token='your_token')
|
8 |
+
|
9 |
+
# Function to update reward points in Salesforce
|
10 |
+
def update_rewards_points(email, points):
|
11 |
+
# Query Salesforce to get the user by email
|
12 |
+
user = sf.query(f"SELECT Id, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{email}'")
|
13 |
+
|
14 |
+
if user['totalSize'] == 1:
|
15 |
+
# Get the user ID and current reward points
|
16 |
+
user_id = user['records'][0]['Id']
|
17 |
+
current_points = user['records'][0]['Reward_Points__c']
|
18 |
+
|
19 |
+
# Calculate new reward points
|
20 |
+
new_points = current_points + int(points)
|
21 |
+
|
22 |
+
# Update reward points in Salesforce
|
23 |
+
sf.Customer_Login__c.update(user_id, {'Reward_Points__c': new_points})
|
24 |
|
25 |
# Route for login page
|
26 |
@app.route('/')
|
|
|
28 |
return render_template('login.html')
|
29 |
|
30 |
# Route to process login
|
31 |
+
@app.route('/login', methods=['POST'])
|
32 |
+
def process_login():
|
33 |
email = request.form['email']
|
34 |
password = request.form['password']
|
35 |
+
|
36 |
+
# Query Salesforce to check if the user exists
|
37 |
+
user = sf.query(f"SELECT Id, Email__c, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{email}'")
|
38 |
+
|
39 |
+
if user['totalSize'] == 1:
|
40 |
+
# Redirect to rewards page after successful login
|
41 |
+
return redirect(url_for('rewards', email=email))
|
42 |
+
else:
|
43 |
+
# Return to login page with error message if user not found
|
44 |
+
return render_template('login.html', error="Invalid credentials")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
# Route to display rewards page
|
47 |
+
@app.route('/rewards/<email>')
|
48 |
+
def rewards(email):
|
49 |
+
user = sf.query(f"SELECT Id, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{email}'")
|
50 |
+
|
51 |
+
if user['totalSize'] == 1:
|
52 |
+
reward_points = user['records'][0]['Reward_Points__c']
|
53 |
+
return render_template('rewards_above.html', reward_points=reward_points, email=email)
|
54 |
+
else:
|
55 |
+
# If the user is not found, redirect to login page
|
56 |
+
return redirect(url_for('login'))
|
57 |
|
58 |
# Route to apply rewards
|
59 |
@app.route('/apply_rewards', methods=['POST'])
|
60 |
def apply_rewards():
|
61 |
+
email = request.form['email']
|
62 |
+
reward_points = request.form['reward_points']
|
63 |
+
|
64 |
+
# Call function to update reward points in Salesforce
|
65 |
+
update_rewards_points(email, reward_points)
|
66 |
+
|
67 |
+
return render_template('rewards_below.html', message="Rewards applied successfully")
|
68 |
+
|
69 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
app.run(debug=True)
|