Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,9 +33,9 @@ if not all([SALESFORCE_USERNAME, SALESFORCE_PASSWORD, SALESFORCE_SECURITY_TOKEN]
|
|
| 33 |
# Initialize Flask app
|
| 34 |
app = Flask(__name__)
|
| 35 |
|
| 36 |
-
def fetch_salesforce_input():
|
| 37 |
"""
|
| 38 |
-
Fetch
|
| 39 |
"""
|
| 40 |
try:
|
| 41 |
sf = Salesforce(
|
|
@@ -46,10 +46,11 @@ def fetch_salesforce_input():
|
|
| 46 |
)
|
| 47 |
logger.info("Connected to Salesforce for input fetch")
|
| 48 |
|
| 49 |
-
query = """
|
| 50 |
SELECT Supervisor_ID__c, Role__c, Project_ID__c, Weather__c, Milestones__c, Reflection_Log__c
|
| 51 |
-
FROM
|
| 52 |
-
|
|
|
|
| 53 |
LIMIT 1
|
| 54 |
"""
|
| 55 |
result = sf.query(query)
|
|
@@ -60,15 +61,15 @@ def fetch_salesforce_input():
|
|
| 60 |
'role': record['Role__c'] or '',
|
| 61 |
'project_id': record['Project_ID__c'] or '',
|
| 62 |
'weather': record['Weather__c'] or '',
|
| 63 |
-
'milestones': record['Milestones__c']
|
| 64 |
'reflection': record['Reflection_Log__c'] or ''
|
| 65 |
}
|
| 66 |
else:
|
| 67 |
-
logger.info("No input data found
|
| 68 |
return None
|
| 69 |
|
| 70 |
except Exception as e:
|
| 71 |
-
logger.error("Salesforce
|
| 72 |
return None
|
| 73 |
|
| 74 |
def generate_coaching_output(data):
|
|
@@ -148,7 +149,7 @@ def save_to_salesforce(output, supervisor_id, project_id):
|
|
| 148 |
security_token=SALESFORCE_SECURITY_TOKEN,
|
| 149 |
domain=SALESFORCE_DOMAIN
|
| 150 |
)
|
| 151 |
-
logger.info("Connected to Salesforce
|
| 152 |
|
| 153 |
coaching_record = {
|
| 154 |
"Supervisor_ID__c": supervisor_id,
|
|
@@ -167,7 +168,7 @@ def save_to_salesforce(output, supervisor_id, project_id):
|
|
| 167 |
return True
|
| 168 |
|
| 169 |
except Exception as e:
|
| 170 |
-
logger.error("Salesforce
|
| 171 |
return False
|
| 172 |
|
| 173 |
@app.route('/', methods=['GET'])
|
|
@@ -180,44 +181,42 @@ def redirect_to_ui():
|
|
| 180 |
@app.route('/ui', methods=['GET'])
|
| 181 |
def ui():
|
| 182 |
"""
|
| 183 |
-
|
| 184 |
"""
|
| 185 |
form_data = {}
|
| 186 |
output = {}
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
#
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
if success:
|
| 214 |
-
output = coaching_output
|
| 215 |
-
else:
|
| 216 |
-
error = "Error: Failed to save output to Salesforce."
|
| 217 |
else:
|
| 218 |
-
error
|
|
|
|
|
|
|
| 219 |
|
| 220 |
-
return render_template('index.html', form_data=form_data, output=output
|
| 221 |
|
| 222 |
@app.route('/health', methods=['GET'])
|
| 223 |
def health_check():
|
|
|
|
| 33 |
# Initialize Flask app
|
| 34 |
app = Flask(__name__)
|
| 35 |
|
| 36 |
+
def fetch_salesforce_input(supervisor_id, project_id):
|
| 37 |
"""
|
| 38 |
+
Fetch input data from Salesforce for a given supervisor and project.
|
| 39 |
"""
|
| 40 |
try:
|
| 41 |
sf = Salesforce(
|
|
|
|
| 46 |
)
|
| 47 |
logger.info("Connected to Salesforce for input fetch")
|
| 48 |
|
| 49 |
+
query = f"""
|
| 50 |
SELECT Supervisor_ID__c, Role__c, Project_ID__c, Weather__c, Milestones__c, Reflection_Log__c
|
| 51 |
+
FROM Supervisor_AI_Coaching__c
|
| 52 |
+
WHERE Supervisor_ID__c = '{supervisor_id}' AND Project_ID__c = '{project_id}'
|
| 53 |
+
ORDER BY Generated_Date__c DESC
|
| 54 |
LIMIT 1
|
| 55 |
"""
|
| 56 |
result = sf.query(query)
|
|
|
|
| 61 |
'role': record['Role__c'] or '',
|
| 62 |
'project_id': record['Project_ID__c'] or '',
|
| 63 |
'weather': record['Weather__c'] or '',
|
| 64 |
+
'milestones': record['Milestones__c'] or '',
|
| 65 |
'reflection': record['Reflection_Log__c'] or ''
|
| 66 |
}
|
| 67 |
else:
|
| 68 |
+
logger.info("No input data found for supervisor %s and project %s", supervisor_id, project_id)
|
| 69 |
return None
|
| 70 |
|
| 71 |
except Exception as e:
|
| 72 |
+
logger.error("Salesforce fetch error: %s", e)
|
| 73 |
return None
|
| 74 |
|
| 75 |
def generate_coaching_output(data):
|
|
|
|
| 149 |
security_token=SALESFORCE_SECURITY_TOKEN,
|
| 150 |
domain=SALESFORCE_DOMAIN
|
| 151 |
)
|
| 152 |
+
logger.info("Connected to Salesforce")
|
| 153 |
|
| 154 |
coaching_record = {
|
| 155 |
"Supervisor_ID__c": supervisor_id,
|
|
|
|
| 168 |
return True
|
| 169 |
|
| 170 |
except Exception as e:
|
| 171 |
+
logger.error("Salesforce error: %s", e)
|
| 172 |
return False
|
| 173 |
|
| 174 |
@app.route('/', methods=['GET'])
|
|
|
|
| 181 |
@app.route('/ui', methods=['GET'])
|
| 182 |
def ui():
|
| 183 |
"""
|
| 184 |
+
Serve the HTML user interface, fetch input from Salesforce, generate output, and save to Salesforce.
|
| 185 |
"""
|
| 186 |
form_data = {}
|
| 187 |
output = {}
|
| 188 |
+
|
| 189 |
+
# Hardcoded supervisor_id and project_id for demonstration
|
| 190 |
+
# In a real application, these should come from URL parameters, user session, or another source
|
| 191 |
+
supervisor_id = "SUP-001"
|
| 192 |
+
project_id = "PROJ-123"
|
| 193 |
+
|
| 194 |
+
# Fetch input data from Salesforce
|
| 195 |
+
sf_input = fetch_salesforce_input(supervisor_id, project_id)
|
| 196 |
+
if sf_input:
|
| 197 |
+
form_data = sf_input
|
| 198 |
+
# Prepare data for generating output
|
| 199 |
+
data = {
|
| 200 |
+
'supervisor_id': form_data['supervisor_id'],
|
| 201 |
+
'role': form_data['role'],
|
| 202 |
+
'project_id': form_data['project_id'],
|
| 203 |
+
'milestones': [m.strip() for m in form_data['milestones'].split(',') if m.strip()],
|
| 204 |
+
'reflection_log': form_data['reflection'],
|
| 205 |
+
'weather': form_data['weather']
|
| 206 |
+
}
|
| 207 |
+
# Generate output automatically
|
| 208 |
+
coaching_output = generate_coaching_output(data)
|
| 209 |
+
if coaching_output:
|
| 210 |
+
# Save the generated output to Salesforce
|
| 211 |
+
success = save_to_salesforce(coaching_output, supervisor_id, project_id)
|
| 212 |
+
if success:
|
| 213 |
+
output = coaching_output
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
else:
|
| 215 |
+
logger.error("Failed to save generated output to Salesforce")
|
| 216 |
+
else:
|
| 217 |
+
logger.error("Failed to generate coaching output")
|
| 218 |
|
| 219 |
+
return render_template('index.html', form_data=form_data, output=output)
|
| 220 |
|
| 221 |
@app.route('/health', methods=['GET'])
|
| 222 |
def health_check():
|