Spaces:
Sleeping
Sleeping
File size: 2,500 Bytes
e83b92c ab4ea97 91ba13e ab4ea97 91ba13e ab4ea97 91ba13e ab4ea97 e83b92c ab4ea97 4c3c59c ab4ea97 91ba13e ab4ea97 4c3c59c ab4ea97 4c3c59c ab4ea97 4c3c59c ab4ea97 4c3c59c ab4ea97 91ba13e d763a4e ab4ea97 e83b92c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
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
|