tionet / app.py
Yaswanth56's picture
Update app.py
f22c5db verified
from flask import Flask, render_template, send_from_directory, request, jsonify
from simple_salesforce import Salesforce
from dotenv import load_dotenv
import os
import logging
logging.basicConfig(level=logging.DEBUG)
load_dotenv()
app = Flask(__name__, template_folder='templates', static_folder='static')
# Salesforce Connection
def get_salesforce_connection():
try:
sf = Salesforce(
username=os.getenv('SFDC_USERNAME'),
password=os.getenv('SFDC_PASSWORD'),
security_token=os.getenv('SFDC_SECURITY_TOKEN'),
domain=os.getenv('SFDC_DOMAIN', 'login')
)
return sf
except Exception as e:
print(f"Error connecting to Salesforce: {e}")
return None
# Initialize Salesforce connection
sf = get_salesforce_connection()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/static/<path:filename>')
def serve_static(filename):
return send_from_directory('static', filename)
# Sample endpoint for Salesforce queries (non-restaurant)
@app.route('/get_salesforce_data', methods=['GET'])
def get_salesforce_data():
try:
# Example SOQL query for retrieving data from a Salesforce object
soql = "SELECT Id, Name FROM Account LIMIT 10"
result = sf.query(soql)
# Format the result as needed
records = [{"id": record['Id'], "name": record['Name']} for record in result['records']]
logging.debug(f"Fetched {len(records)} records from Salesforce.")
return jsonify({"data": records})
except Exception as e:
logging.error(f"Error while fetching data from Salesforce: {str(e)}")
return jsonify({"error": f"Failed to fetch data: {str(e)}"}), 500
# Example of a POST endpoint for submitting data (non-restaurant)
@app.route('/submit_salesforce_data', methods=['POST'])
def submit_salesforce_data():
data = request.json
record_data = data.get('record', {})
if not record_data:
return jsonify({'error': 'No data provided'}), 400
try:
# Example of creating a new Salesforce record
account = sf.Account.create(record_data)
logging.debug(f"Created new Salesforce account with Id: {account['id']}")
return jsonify({'success': True, 'account_id': account['id']})
except Exception as e:
logging.error(f"Error while submitting data to Salesforce: {str(e)}")
return jsonify({"error": f"Failed to submit data: {str(e)}"}), 500
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=7860)