AjaykumarPilla commited on
Commit
aead505
·
verified ·
1 Parent(s): 168c5bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -3
app.py CHANGED
@@ -25,11 +25,16 @@ st.set_page_config(page_title="Delay 🚀", layout="wide")
25
 
26
  # Salesforce connection (using environment variables)
27
  try:
 
 
 
 
 
28
  sf = Salesforce(
29
  username=os.environ.get("SF_USERNAME"),
30
  password=os.environ.get("SF_PASSWORD"),
31
  security_token=os.environ.get("SF_SECURITY_TOKEN"),
32
- instance_url=os.environ.get("SF_INSTANCE_URL")
33
  )
34
  except Exception as e:
35
  st.error(f"Failed to connect to Salesforce: {str(e)}")
@@ -168,19 +173,23 @@ def save_to_salesforce(input_data, prediction, pdf_buffer):
168
  query = f"SELECT ContentDocumentId FROM ContentVersion WHERE Id = '{content_version_id}'"
169
  query_result = sf.query(query)
170
  if query_result["totalSize"] == 0:
 
171
  return "Failed to retrieve ContentDocumentId for the ContentVersion"
172
  content_document_id = query_result["records"][0]["ContentDocumentId"]
173
 
174
- # Construct the correct Salesforce URL for the ContentDocument
175
- pdf_url = f"{os.environ.get('SF_INSTANCE_URL')}/sfc/servlet.shepherd/document/download/{content_document_id}"
 
176
 
177
  # Update the Delay_Predictor__c record with the PDF URL
178
  update_result = sf.Delay_Predictor__c.update(record_id, {"PDF_Report__c": pdf_url})
179
  if update_result != 204: # 204 indicates success for updates
 
180
  return f"Failed to update PDF_Report__c field: {update_result}"
181
 
182
  return None
183
  except Exception as e:
 
184
  return f"Error saving to Salesforce: {str(e)}"
185
 
186
  # Input form
@@ -260,6 +269,12 @@ if submit_button:
260
  chart_html = f"""
261
  <canvas id="{chart_id}" style="max-height: 200px; max-width: 600px;"></canvas>
262
  <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
 
 
 
 
 
 
263
  <script>
264
  try {{
265
  const ctx = document.getElementById('{chart_id}').getContext('2d');
 
25
 
26
  # Salesforce connection (using environment variables)
27
  try:
28
+ sf_instance_url = os.environ.get("SF_INSTANCE_URL")
29
+ if not sf_instance_url:
30
+ raise ValueError("SF_INSTANCE_URL environment variable is not set")
31
+ if "lightning.force.com" in sf_instance_url:
32
+ logger.warning("SF_INSTANCE_URL contains lightning.force.com; consider using my.salesforce.com for reliable PDF downloads")
33
  sf = Salesforce(
34
  username=os.environ.get("SF_USERNAME"),
35
  password=os.environ.get("SF_PASSWORD"),
36
  security_token=os.environ.get("SF_SECURITY_TOKEN"),
37
+ instance_url=sf_instance_url
38
  )
39
  except Exception as e:
40
  st.error(f"Failed to connect to Salesforce: {str(e)}")
 
173
  query = f"SELECT ContentDocumentId FROM ContentVersion WHERE Id = '{content_version_id}'"
174
  query_result = sf.query(query)
175
  if query_result["totalSize"] == 0:
176
+ logger.error(f"Failed to retrieve ContentDocumentId for ContentVersion {content_version_id}")
177
  return "Failed to retrieve ContentDocumentId for the ContentVersion"
178
  content_document_id = query_result["records"][0]["ContentDocumentId"]
179
 
180
+ # Construct the Salesforce URL for the ContentDocument
181
+ pdf_url = f"{sf_instance_url}/sfc/servlet.shepherd/document/download/{content_document_id}"
182
+ logger.info(f"Generated PDF URL: {pdf_url}")
183
 
184
  # Update the Delay_Predictor__c record with the PDF URL
185
  update_result = sf.Delay_Predictor__c.update(record_id, {"PDF_Report__c": pdf_url})
186
  if update_result != 204: # 204 indicates success for updates
187
+ logger.error(f"Failed to update PDF_Report__c with URL: {pdf_url}")
188
  return f"Failed to update PDF_Report__c field: {update_result}"
189
 
190
  return None
191
  except Exception as e:
192
+ logger.error(f"Error saving to Salesforce: {str(e)}")
193
  return f"Error saving to Salesforce: {str(e)}"
194
 
195
  # Input form
 
269
  chart_html = f"""
270
  <canvas id="{chart_id}" style="max-height: 200px; max-width: 600px;"></canvas>
271
  <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
272
+ <script>
273
+ try {{
274
+ const ctx = document.getElementById('{chart_id}').getContext('2d');
275
+ new Chart(ctx, {json.dumps(chart_config)});
276
+ }} catch (e) {{
277
+ console.errornm/chart.js"></script>
278
  <script>
279
  try {{
280
  const ctx = document.getElementById('{chart_id}').getContext('2d');