Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,6 +11,7 @@ from reportlab.lib.styles import getSampleStyleSheet
|
|
| 11 |
from reportlab.lib.units import inch
|
| 12 |
from io import BytesIO
|
| 13 |
from simple_salesforce import Salesforce
|
|
|
|
| 14 |
|
| 15 |
# Streamlit app configuration
|
| 16 |
st.set_page_config(page_title="Delay 🚀", layout="wide")
|
|
@@ -108,8 +109,8 @@ def generate_pdf(input_data, prediction, heatmap_fig):
|
|
| 108 |
buffer.seek(0)
|
| 109 |
return buffer
|
| 110 |
|
| 111 |
-
# Function to save data to Salesforce
|
| 112 |
-
def save_to_salesforce(input_data, prediction):
|
| 113 |
if sf is None:
|
| 114 |
return "Salesforce connection not established."
|
| 115 |
try:
|
|
@@ -133,7 +134,37 @@ def save_to_salesforce(input_data, prediction):
|
|
| 133 |
}
|
| 134 |
# Create a new record in Delay_Predictor__c
|
| 135 |
result = sf.Delay_Predictor__c.create(sf_data)
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
except Exception as e:
|
| 138 |
return f"Error saving to Salesforce: {str(e)}"
|
| 139 |
|
|
@@ -212,12 +243,12 @@ if submit_button:
|
|
| 212 |
mime="application/pdf"
|
| 213 |
)
|
| 214 |
|
| 215 |
-
# Save to Salesforce
|
| 216 |
-
sf_error = save_to_salesforce(input_data, prediction)
|
| 217 |
if sf_error:
|
| 218 |
st.error(sf_error)
|
| 219 |
else:
|
| 220 |
-
st.success("Prediction data successfully saved to Salesforce!")
|
| 221 |
|
| 222 |
st.session_state.prediction = prediction
|
| 223 |
st.session_state.input_data = input_data
|
|
|
|
| 11 |
from reportlab.lib.units import inch
|
| 12 |
from io import BytesIO
|
| 13 |
from simple_salesforce import Salesforce
|
| 14 |
+
import base64
|
| 15 |
|
| 16 |
# Streamlit app configuration
|
| 17 |
st.set_page_config(page_title="Delay 🚀", layout="wide")
|
|
|
|
| 109 |
buffer.seek(0)
|
| 110 |
return buffer
|
| 111 |
|
| 112 |
+
# Function to save data to Salesforce, including PDF
|
| 113 |
+
def save_to_salesforce(input_data, prediction, pdf_buffer):
|
| 114 |
if sf is None:
|
| 115 |
return "Salesforce connection not established."
|
| 116 |
try:
|
|
|
|
| 134 |
}
|
| 135 |
# Create a new record in Delay_Predictor__c
|
| 136 |
result = sf.Delay_Predictor__c.create(sf_data)
|
| 137 |
+
if not result["success"]:
|
| 138 |
+
return f"Salesforce save failed: {result['errors']}"
|
| 139 |
+
|
| 140 |
+
# Get the record ID
|
| 141 |
+
record_id = result["id"]
|
| 142 |
+
|
| 143 |
+
# Upload PDF as ContentVersion
|
| 144 |
+
pdf_data = pdf_buffer.getvalue()
|
| 145 |
+
pdf_base64 = base64.b64encode(pdf_data).decode('utf-8')
|
| 146 |
+
content_version = {
|
| 147 |
+
"Title": f"Delay_Prediction_Report_{input_data['project_name']}_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
|
| 148 |
+
"PathOnClient": "project_delay_report.pdf",
|
| 149 |
+
"VersionData": pdf_base64,
|
| 150 |
+
"FirstPublishLocationId": record_id # Link to the Delay_Predictor__c record
|
| 151 |
+
}
|
| 152 |
+
cv_result = sf.ContentVersion.create(content_version)
|
| 153 |
+
if not cv_result["success"]:
|
| 154 |
+
return f"Failed to upload PDF to Salesforce: {cv_result['errors']}"
|
| 155 |
+
|
| 156 |
+
# Get the ContentVersion ID
|
| 157 |
+
content_version_id = cv_result["id"]
|
| 158 |
+
|
| 159 |
+
# Construct the internal Salesforce URL for the ContentVersion
|
| 160 |
+
pdf_url = f"{os.environ.get('SF_INSTANCE_URL')}/sfc/servlet.shepherd/version/download/{content_version_id}"
|
| 161 |
+
|
| 162 |
+
# Update the Delay_Predictor__c record with the PDF URL
|
| 163 |
+
update_result = sf.Delay_Predictor__c.update(record_id, {"PDF_Report__c": pdf_url})
|
| 164 |
+
if not update_result == 204: # 204 indicates success for updates
|
| 165 |
+
return f"Failed to update PDF_Report__c field: {update_result}"
|
| 166 |
+
|
| 167 |
+
return None
|
| 168 |
except Exception as e:
|
| 169 |
return f"Error saving to Salesforce: {str(e)}"
|
| 170 |
|
|
|
|
| 243 |
mime="application/pdf"
|
| 244 |
)
|
| 245 |
|
| 246 |
+
# Save to Salesforce, including PDF
|
| 247 |
+
sf_error = save_to_salesforce(input_data, prediction, pdf_buffer)
|
| 248 |
if sf_error:
|
| 249 |
st.error(sf_error)
|
| 250 |
else:
|
| 251 |
+
st.success("Prediction data and PDF successfully saved to Salesforce!")
|
| 252 |
|
| 253 |
st.session_state.prediction = prediction
|
| 254 |
st.session_state.input_data = input_data
|