lokeshloki143 commited on
Commit
7119982
·
verified ·
1 Parent(s): 1275bdd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -43
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 the latest input data from Salesforce Supervisor_Input__c object.
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 Supervisor_Input__c
52
- ORDER BY CreatedDate DESC
 
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'].split(',') if record['Milestones__c'] else [],
64
  'reflection': record['Reflection_Log__c'] or ''
65
  }
66
  else:
67
- logger.info("No input data found in Salesforce")
68
  return None
69
 
70
  except Exception as e:
71
- logger.error("Salesforce input fetch error: %s", e)
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 for saving output")
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 save error: %s", e)
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
- Fetch input from Salesforce, generate output, save to Salesforce, and display both.
184
  """
185
  form_data = {}
186
  output = {}
187
- error = ""
188
-
189
- # Step 1: Fetch input data from Salesforce
190
- input_data = fetch_salesforce_input()
191
- if not input_data:
192
- error = "Error: No input data found in Salesforce or failed to connect."
193
- else:
194
- form_data = input_data
195
- # Validate all fields
196
- if not all([form_data['supervisor_id'], form_data['role'], form_data['project_id'],
197
- form_data['weather'], form_data['milestones'], form_data['reflection']]):
198
- error = "Error: Some required fields are missing in Salesforce input data."
199
- else:
200
- # Step 2: Generate coaching output
201
- data = {
202
- 'supervisor_id': form_data['supervisor_id'],
203
- 'role': form_data['role'],
204
- 'project_id': form_data['project_id'],
205
- 'milestones': form_data['milestones'],
206
- 'reflection_log': form_data['reflection'],
207
- 'weather': form_data['weather']
208
- }
209
- coaching_output = generate_coaching_output(data)
210
- if coaching_output:
211
- # Step 3: Save output to Salesforce
212
- success = save_to_salesforce(coaching_output, data['supervisor_id'], data['project_id'])
213
- if success:
214
- output = coaching_output
215
- else:
216
- error = "Error: Failed to save output to Salesforce."
217
  else:
218
- error = "Error: Failed to generate coaching output."
 
 
219
 
220
- return render_template('index.html', form_data=form_data, output=output, error=error)
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():