testing_1 / app.py
Yaswanth56's picture
Update app.py
e83b92c verified
raw
history blame
2.5 kB
from flask import Flask, render_template, request, redirect, url_for
from simple_salesforce import Salesforce
from dotenv import load_dotenv
import os
# Load environment variables from .env file
load_dotenv()
app = Flask(__name__)
# Get Salesforce credentials from environment variables
SF_USERNAME = os.getenv('SF_USERNAME')
SF_PASSWORD = os.getenv('SF_PASSWORD')
SF_SECURITY_TOKEN = os.getenv('SF_SECURITY_TOKEN')
SF_DOMAIN = os.getenv('SF_DOMAIN')
# Salesforce connection
try:
print("Attempting Salesforce connection...")
sf = Salesforce(username=SF_USERNAME,
password=SF_PASSWORD,
security_token=SF_SECURITY_TOKEN,
domain=SF_DOMAIN)
print("Salesforce connection successful!")
except Exception as e:
print(f"Salesforce connection failed: {e}")
sf = None
# Route for login page
@app.route('/')
def login():
return render_template('login.html', error_message=None)
# Route to process login
@app.route('/auth', methods=['POST'])
def auth():
email = request.form['email']
password = request.form['password']
if not sf:
return render_template('login.html', error_message="Salesforce connection failed. Please try again.")
try:
# Query Salesforce for user authentication
query = f"SELECT Id, Reward_Points__c FROM Customer_Login__c WHERE Email__c = '{email}' AND Password__c = '{password}'"
result = sf.query(query)
if result['totalSize'] == 0:
return render_template('login.html', error_message="Invalid login details. Please check your email and password.")
customer = result['records'][0]
reward_points = customer['Reward_Points__c']
# Redirect to rewards page or any other page
return redirect(url_for('rewards', customer_id=customer['Id'], points=reward_points))
except Exception as e:
return render_template('login.html', error_message=f"Error during authentication: {e}")
# Route to display rewards page
@app.route('/rewards/<customer_id>')
def rewards(customer_id):
try:
customer = sf.Customer_Login__c.get(customer_id)
points = customer['Reward_Points__c']
# Render the rewards page
return render_template('rewards.html', points=points, customer_id=customer_id)
except Exception as e:
return f"Error fetching rewards: {e}"
if __name__ == '__main__':
app.run(debug=True, host="0.0.0.0", port=7860) # Ensure it runs on port 7860