Yaswanth56 commited on
Commit
91ba13e
·
verified ·
1 Parent(s): 9ee9873

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -17
app.py CHANGED
@@ -2,10 +2,6 @@ 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
- 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,14 +16,14 @@ SF_DOMAIN = os.getenv('SF_DOMAIN')
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,26 +38,22 @@ def auth():
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,11 +62,10 @@ def rewards(customer_id):
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,7 +79,6 @@ def 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,6 +87,7 @@ 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
  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,9 +98,10 @@ def apply_rewards():
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
113
  return render_template(
114
  'apply_rewards.html',
115
  original_bill=bill_amount,
@@ -120,7 +112,6 @@ def apply_rewards():
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__':
 
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
 
17
  # Salesforce connection
18
  try:
19
+ print("Attempting Salesforce connection...")
20
  sf = Salesforce(username=SF_USERNAME,
21
  password=SF_PASSWORD,
22
  security_token=SF_SECURITY_TOKEN,
23
  domain=SF_DOMAIN)
24
+ print("Salesforce connection successful!")
25
  except Exception as e:
26
+ print(f"Salesforce connection failed: {e}")
27
  sf = None
28
 
29
  # Route for login page
 
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
45
+ query = f"SELECT Id, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{email}' AND Password__c = '{password}'"
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
 
62
  try:
63
  customer = sf.Customer_Login__c.get(customer_id)
64
  points = customer['Reward_Points__c']
65
+
66
  # Render the rewards page
67
  return render_template('rewards.html', points=points, customer_id=customer_id)
68
  except Exception as e:
 
69
  return f"Error fetching rewards: {e}"
70
 
71
  # Route to apply rewards
 
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
 
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
 
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,
 
112
  message=message
113
  )
114
  except Exception as e:
 
115
  return f"Error applying rewards: {e}"
116
 
117
  if __name__ == '__main__':