|
|
from flask import Flask, render_template, request, redirect, url_for |
|
|
from simple_salesforce import Salesforce |
|
|
from dotenv import load_dotenv |
|
|
import os |
|
|
|
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
|
|
|
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') |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
@app.route('/') |
|
|
def login(): |
|
|
return render_template('login.html', error_message=None) |
|
|
|
|
|
|
|
|
@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 = 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'] |
|
|
|
|
|
|
|
|
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}") |
|
|
|
|
|
|
|
|
@app.route('/rewards/<customer_id>') |
|
|
def rewards(customer_id): |
|
|
try: |
|
|
customer = sf.Customer_Login__c.get(customer_id) |
|
|
points = customer['Reward_Points__c'] |
|
|
|
|
|
|
|
|
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) |
|
|
|