Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,7 @@ from reportlab.lib.units import inch
|
|
| 12 |
from io import BytesIO
|
| 13 |
import base64
|
| 14 |
import logging
|
|
|
|
| 15 |
|
| 16 |
# ==========================
|
| 17 |
# Configuration
|
|
@@ -68,6 +69,7 @@ model = load_model()
|
|
| 68 |
# ==========================
|
| 69 |
# Salesforce Integration
|
| 70 |
# ==========================
|
|
|
|
| 71 |
def connect_to_salesforce():
|
| 72 |
try:
|
| 73 |
sf = Salesforce(**CONFIG["SF_CREDENTIALS"])
|
|
@@ -134,7 +136,11 @@ def upload_pdf_to_salesforce(sf, pdf_file, report_id):
|
|
| 134 |
"VersionData": encoded_pdf,
|
| 135 |
"FirstPublishLocationId": report_id
|
| 136 |
}
|
|
|
|
| 137 |
content_version = sf.ContentVersion.create(content_version_data)
|
|
|
|
|
|
|
|
|
|
| 138 |
result = sf.query(f"SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id = '{content_version['id']}'")
|
| 139 |
if not result['records']:
|
| 140 |
logger.error("Failed to retrieve ContentVersion")
|
|
@@ -156,19 +162,24 @@ def push_report_to_salesforce(violations, score, pdf_path, pdf_file):
|
|
| 156 |
pdf_url = f"{CONFIG['PUBLIC_URL_BASE']}{os.path.basename(pdf_path)}" if pdf_path else ""
|
| 157 |
|
| 158 |
record_data = {
|
| 159 |
-
"
|
| 160 |
-
"Violations_Found__c": len(violations),
|
| 161 |
"Violations_Details__c": violations_text,
|
| 162 |
"Status__c": "Pending",
|
| 163 |
"PDF_Report_URL__c": pdf_url
|
| 164 |
}
|
|
|
|
| 165 |
record = sf.Safety_Video_Report__c.create(record_data)
|
|
|
|
|
|
|
|
|
|
| 166 |
record_id = record["id"]
|
| 167 |
logger.info(f"Salesforce record created: {record_id}")
|
| 168 |
|
| 169 |
if pdf_file:
|
| 170 |
uploaded_url = upload_pdf_to_salesforce(sf, pdf_file, record_id)
|
| 171 |
if uploaded_url:
|
|
|
|
| 172 |
sf.Safety_Video_Report__c.update(record_id, {"PDF_Report_URL__c": uploaded_url})
|
| 173 |
pdf_url = uploaded_url
|
| 174 |
|
|
|
|
| 12 |
from io import BytesIO
|
| 13 |
import base64
|
| 14 |
import logging
|
| 15 |
+
import retrying
|
| 16 |
|
| 17 |
# ==========================
|
| 18 |
# Configuration
|
|
|
|
| 69 |
# ==========================
|
| 70 |
# Salesforce Integration
|
| 71 |
# ==========================
|
| 72 |
+
@retrying.retry(stop_max_attempt_number=3, wait_fixed=2000)
|
| 73 |
def connect_to_salesforce():
|
| 74 |
try:
|
| 75 |
sf = Salesforce(**CONFIG["SF_CREDENTIALS"])
|
|
|
|
| 136 |
"VersionData": encoded_pdf,
|
| 137 |
"FirstPublishLocationId": report_id
|
| 138 |
}
|
| 139 |
+
logger.info(f"Creating ContentVersion for report ID: {report_id}")
|
| 140 |
content_version = sf.ContentVersion.create(content_version_data)
|
| 141 |
+
if not content_version.get('id'):
|
| 142 |
+
logger.error("ContentVersion creation failed: No ID returned")
|
| 143 |
+
return ""
|
| 144 |
result = sf.query(f"SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id = '{content_version['id']}'")
|
| 145 |
if not result['records']:
|
| 146 |
logger.error("Failed to retrieve ContentVersion")
|
|
|
|
| 162 |
pdf_url = f"{CONFIG['PUBLIC_URL_BASE']}{os.path.basename(pdf_path)}" if pdf_path else ""
|
| 163 |
|
| 164 |
record_data = {
|
| 165 |
+
"Compliance_Score__c": float(score), # Ensure correct data type
|
| 166 |
+
"Violations_Found__c": int(len(violations)), # Ensure integer
|
| 167 |
"Violations_Details__c": violations_text,
|
| 168 |
"Status__c": "Pending",
|
| 169 |
"PDF_Report_URL__c": pdf_url
|
| 170 |
}
|
| 171 |
+
logger.info(f"Creating Safety_Video_Report__c with data: {record_data}")
|
| 172 |
record = sf.Safety_Video_Report__c.create(record_data)
|
| 173 |
+
if not record.get('id'):
|
| 174 |
+
logger.error("Safety_Video_Report__c creation failed: No ID returned")
|
| 175 |
+
return None, ""
|
| 176 |
record_id = record["id"]
|
| 177 |
logger.info(f"Salesforce record created: {record_id}")
|
| 178 |
|
| 179 |
if pdf_file:
|
| 180 |
uploaded_url = upload_pdf_to_salesforce(sf, pdf_file, record_id)
|
| 181 |
if uploaded_url:
|
| 182 |
+
logger.info(f"Updating record {record_id} with PDF URL: {uploaded_url}")
|
| 183 |
sf.Safety_Video_Report__c.update(record_id, {"PDF_Report_URL__c": uploaded_url})
|
| 184 |
pdf_url = uploaded_url
|
| 185 |
|