Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,23 +4,44 @@ from dotenv import load_dotenv
|
|
| 4 |
import os
|
| 5 |
import logging
|
| 6 |
|
|
|
|
| 7 |
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
| 8 |
|
|
|
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
app = Flask(__name__, template_folder='templates', static_folder='static')
|
| 12 |
|
| 13 |
def get_salesforce_connection():
|
| 14 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
sf = Salesforce(
|
| 16 |
-
username=
|
| 17 |
-
password=
|
| 18 |
-
security_token=
|
| 19 |
-
domain=
|
| 20 |
)
|
|
|
|
| 21 |
return sf
|
| 22 |
except Exception as e:
|
| 23 |
-
|
| 24 |
return None
|
| 25 |
|
| 26 |
sf = get_salesforce_connection()
|
|
@@ -33,7 +54,52 @@ def index():
|
|
| 33 |
def serve_static(filename):
|
| 34 |
return send_from_directory('static', filename)
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
if __name__ == '__main__':
|
| 39 |
-
app.run(debug=True, host='0.0.0.0', port=7860)
|
|
|
|
| 4 |
import os
|
| 5 |
import logging
|
| 6 |
|
| 7 |
+
# Set up logging to capture detailed errors
|
| 8 |
logging.basicConfig(level=logging.DEBUG)
|
| 9 |
+
logger = logging.getLogger(__name__)
|
| 10 |
|
| 11 |
+
# Load environment variables from .env file (if it exists)
|
| 12 |
load_dotenv()
|
| 13 |
|
| 14 |
app = Flask(__name__, template_folder='templates', static_folder='static')
|
| 15 |
|
| 16 |
def get_salesforce_connection():
|
| 17 |
try:
|
| 18 |
+
# Check if environment variables are set
|
| 19 |
+
username = os.getenv('SFDC_USERNAME')
|
| 20 |
+
password = os.getenv('SFDC_PASSWORD')
|
| 21 |
+
security_token = os.getenv('SFDC_SECURITY_TOKEN')
|
| 22 |
+
domain = os.getenv('SFDC_DOMAIN', 'login')
|
| 23 |
+
|
| 24 |
+
logger.debug(f"Environment variables loaded:")
|
| 25 |
+
logger.debug(f"SFDC_USERNAME: {username}")
|
| 26 |
+
logger.debug(f"SFDC_PASSWORD: {password if password else 'None'}")
|
| 27 |
+
logger.debug(f"SFDC_SECURITY_TOKEN: {security_token if security_token else 'None'}")
|
| 28 |
+
logger.debug(f"SFDC_DOMAIN: {domain}")
|
| 29 |
+
|
| 30 |
+
if not username or not password or not security_token:
|
| 31 |
+
logger.error("Missing required Salesforce credentials. Please set SFDC_USERNAME, SFDC_PASSWORD, and SFDC_SECURITY_TOKEN in Hugging Face Space Secrets.")
|
| 32 |
+
return None
|
| 33 |
+
|
| 34 |
+
logger.debug("Attempting to connect to Salesforce...")
|
| 35 |
sf = Salesforce(
|
| 36 |
+
username=username,
|
| 37 |
+
password=password,
|
| 38 |
+
security_token=security_token,
|
| 39 |
+
domain=domain
|
| 40 |
)
|
| 41 |
+
logger.debug("Salesforce connection successful.")
|
| 42 |
return sf
|
| 43 |
except Exception as e:
|
| 44 |
+
logger.error(f"Error connecting to Salesforce: {e}")
|
| 45 |
return None
|
| 46 |
|
| 47 |
sf = get_salesforce_connection()
|
|
|
|
| 54 |
def serve_static(filename):
|
| 55 |
return send_from_directory('static', filename)
|
| 56 |
|
| 57 |
+
@app.route('/submit-case', methods=['POST'])
|
| 58 |
+
def submit_case():
|
| 59 |
+
# Check if Salesforce connection is active
|
| 60 |
+
if not sf:
|
| 61 |
+
logger.error("Salesforce connection is not active.")
|
| 62 |
+
return jsonify({'success': False, 'error': 'Salesforce connection failed'}), 500
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
data = request.get_json()
|
| 66 |
+
if not data:
|
| 67 |
+
logger.error("No data received in the request.")
|
| 68 |
+
return jsonify({'success': False, 'error': 'No data received'}), 400
|
| 69 |
+
|
| 70 |
+
logger.debug(f"Received form data: {data}")
|
| 71 |
|
| 72 |
+
# Extract form data with default values to avoid None
|
| 73 |
+
name = data.get('name', '')
|
| 74 |
+
mobile_number = data.get('mobileNumber', '')
|
| 75 |
+
email = data.get('email', '')
|
| 76 |
+
issue_description = data.get('issueDescription', '')
|
| 77 |
+
|
| 78 |
+
# Validate required fields
|
| 79 |
+
if not name or not email or not issue_description:
|
| 80 |
+
logger.error("Missing required fields in form data.")
|
| 81 |
+
return jsonify({'success': False, 'error': 'Missing required fields'}), 400
|
| 82 |
+
|
| 83 |
+
# Log the data being sent to Salesforce
|
| 84 |
+
logger.debug(f"Preparing Case__c record with: Name={name}, Mobile_Number__c={mobile_number}, Email__c={email}, Issue_Description__c={issue_description}")
|
| 85 |
+
|
| 86 |
+
# Create a Case__c record in Salesforce
|
| 87 |
+
case_data = {
|
| 88 |
+
'Name': name,
|
| 89 |
+
'Mobile_Number__c': mobile_number,
|
| 90 |
+
'Email__c': email,
|
| 91 |
+
'Issue_Description__c': issue_description
|
| 92 |
+
}
|
| 93 |
+
logger.debug(f"Case data to be sent: {case_data}")
|
| 94 |
+
|
| 95 |
+
# Create the record
|
| 96 |
+
response = sf.Case__c.create(case_data)
|
| 97 |
+
logger.debug(f"Salesforce response: {response}")
|
| 98 |
+
|
| 99 |
+
return jsonify({'success': True})
|
| 100 |
+
except Exception as e:
|
| 101 |
+
logger.error(f"Error creating Case__c in Salesforce: {str(e)}")
|
| 102 |
+
return jsonify({'success': False, 'error': str(e)}), 500
|
| 103 |
|
| 104 |
if __name__ == '__main__':
|
| 105 |
+
app.run(debug=True, host='0.0.0.0', port=7860)
|