Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,10 @@ 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()
|
@@ -16,13 +20,14 @@ SF_DOMAIN = os.getenv('SF_DOMAIN')
|
|
16 |
|
17 |
# Salesforce connection
|
18 |
try:
|
|
|
19 |
sf = Salesforce(username=SF_USERNAME,
|
20 |
password=SF_PASSWORD,
|
21 |
security_token=SF_SECURITY_TOKEN,
|
22 |
domain=SF_DOMAIN)
|
23 |
-
|
24 |
except Exception as e:
|
25 |
-
|
26 |
sf = None
|
27 |
|
28 |
# Route for login page
|
@@ -37,22 +42,26 @@ def auth():
|
|
37 |
password = request.form['password']
|
38 |
|
39 |
if not sf:
|
|
|
40 |
return "Salesforce connection failed. Please check credentials and try again."
|
41 |
|
42 |
try:
|
43 |
# Query Salesforce for user authentication
|
44 |
-
query = f"SELECT Id, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{email}' AND Password__c = '{password}'"
|
45 |
result = sf.query(query)
|
46 |
|
47 |
if result['totalSize'] == 0:
|
|
|
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
|
@@ -61,10 +70,11 @@ def rewards(customer_id):
|
|
61 |
try:
|
62 |
customer = sf.Customer_Login__c.get(customer_id)
|
63 |
points = customer['Reward_Points__c']
|
64 |
-
|
65 |
# Render the rewards page
|
66 |
return render_template('rewards.html', points=points, customer_id=customer_id)
|
67 |
except Exception as e:
|
|
|
68 |
return f"Error fetching rewards: {e}"
|
69 |
|
70 |
# Route to apply rewards
|
@@ -78,6 +88,7 @@ def apply_rewards():
|
|
78 |
customer = sf.Customer_Login__c.get(customer_id)
|
79 |
points = customer['Reward_Points__c']
|
80 |
gst = 0.18 * bill_amount
|
|
|
81 |
|
82 |
if points >= 500 and apply_rewards:
|
83 |
discount = 0.1 * bill_amount
|
@@ -86,7 +97,6 @@ def apply_rewards():
|
|
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
|
@@ -97,7 +107,6 @@ def apply_rewards():
|
|
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
|
@@ -111,7 +120,8 @@ def apply_rewards():
|
|
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)
|
|
|
2 |
from simple_salesforce import Salesforce
|
3 |
from dotenv import load_dotenv
|
4 |
import os
|
5 |
+
import logging
|
6 |
+
|
7 |
+
# Set up logging
|
8 |
+
logging.basicConfig(level=logging.DEBUG)
|
9 |
|
10 |
# Load environment variables from .env file
|
11 |
load_dotenv()
|
|
|
20 |
|
21 |
# Salesforce connection
|
22 |
try:
|
23 |
+
app.logger.debug("Attempting Salesforce connection...")
|
24 |
sf = Salesforce(username=SF_USERNAME,
|
25 |
password=SF_PASSWORD,
|
26 |
security_token=SF_SECURITY_TOKEN,
|
27 |
domain=SF_DOMAIN)
|
28 |
+
app.logger.debug("Salesforce connection successful!")
|
29 |
except Exception as e:
|
30 |
+
app.logger.error(f"Salesforce connection failed: {e}")
|
31 |
sf = None
|
32 |
|
33 |
# Route for login page
|
|
|
42 |
password = request.form['password']
|
43 |
|
44 |
if not sf:
|
45 |
+
app.logger.error("Salesforce connection failed. Please check credentials and try again.")
|
46 |
return "Salesforce connection failed. Please check credentials and try again."
|
47 |
|
48 |
try:
|
49 |
# Query Salesforce for user authentication
|
50 |
+
query = f"SELECT Id, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{email}' AND Password__c = '{password}' LIMIT 1"
|
51 |
result = sf.query(query)
|
52 |
|
53 |
if result['totalSize'] == 0:
|
54 |
+
app.logger.error("Invalid Login Details")
|
55 |
return "Invalid Login Details"
|
56 |
|
57 |
customer = result['records'][0]
|
58 |
reward_points = customer['Reward_Points__c']
|
59 |
|
60 |
# Redirect to rewards page
|
61 |
+
app.logger.debug(f"Redirecting to rewards page with customer_id: {customer['Id']} and points: {reward_points}")
|
62 |
return redirect(url_for('rewards', customer_id=customer['Id'], points=reward_points))
|
63 |
except Exception as e:
|
64 |
+
app.logger.error(f"Error during authentication: {e}")
|
65 |
return f"Error during authentication: {e}"
|
66 |
|
67 |
# Route to display rewards page
|
|
|
70 |
try:
|
71 |
customer = sf.Customer_Login__c.get(customer_id)
|
72 |
points = customer['Reward_Points__c']
|
73 |
+
app.logger.debug(f"Fetched reward points: {points} for customer_id: {customer_id}")
|
74 |
# Render the rewards page
|
75 |
return render_template('rewards.html', points=points, customer_id=customer_id)
|
76 |
except Exception as e:
|
77 |
+
app.logger.error(f"Error fetching rewards: {e}")
|
78 |
return f"Error fetching rewards: {e}"
|
79 |
|
80 |
# Route to apply rewards
|
|
|
88 |
customer = sf.Customer_Login__c.get(customer_id)
|
89 |
points = customer['Reward_Points__c']
|
90 |
gst = 0.18 * bill_amount
|
91 |
+
app.logger.debug(f"Processing bill amount: {bill_amount}, GST: {gst}, Points: {points}")
|
92 |
|
93 |
if points >= 500 and apply_rewards:
|
94 |
discount = 0.1 * bill_amount
|
|
|
97 |
|
98 |
# Update the customer's reward points in Salesforce
|
99 |
sf.Customer_Login__c.update(customer_id, {'Reward_Points__c': updated_points})
|
|
|
100 |
message = "You saved 10% on your total bill!"
|
101 |
else:
|
102 |
# Customers with below 500 points earn 10% of their bill amount as reward points
|
|
|
107 |
|
108 |
# Update the customer's reward points in Salesforce
|
109 |
sf.Customer_Login__c.update(customer_id, {'Reward_Points__c': updated_points})
|
|
|
110 |
message = f"You earned 10% of your bill amount ({earned_points} points) as reward points!"
|
111 |
|
112 |
# Render the summary page
|
|
|
120 |
message=message
|
121 |
)
|
122 |
except Exception as e:
|
123 |
+
app.logger.error(f"Error applying rewards: {e}")
|
124 |
return f"Error applying rewards: {e}"
|
125 |
|
126 |
if __name__ == '__main__':
|
127 |
+
app.run(debug=True, host="0.0.0.0", port=5000)
|