Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -95,19 +95,22 @@ def save_dpr_to_pdf(dpr_text, filename):
|
|
| 95 |
return f"Error saving PDF: {str(e)}", None
|
| 96 |
|
| 97 |
# Function to upload a file to Salesforce as ContentVersion
|
| 98 |
-
def upload_file_to_salesforce(file_path, filename, sf_connection):
|
| 99 |
try:
|
| 100 |
# Read file content and encode in base64
|
| 101 |
with open(file_path, 'rb') as f:
|
| 102 |
file_content = f.read()
|
| 103 |
file_content_b64 = base64.b64encode(file_content).decode('utf-8')
|
| 104 |
|
|
|
|
|
|
|
|
|
|
| 105 |
# Create ContentVersion
|
| 106 |
content_version = sf_connection.ContentVersion.create({
|
| 107 |
'Title': filename,
|
| 108 |
'PathOnClient': filename,
|
| 109 |
'VersionData': file_content_b64,
|
| 110 |
-
'Description':
|
| 111 |
})
|
| 112 |
|
| 113 |
# Get ContentDocumentId
|
|
@@ -165,6 +168,7 @@ def generate_dpr(files):
|
|
| 165 |
# Create Daily_Progress_Reports__c record
|
| 166 |
report_description = "; ".join(captions)[:255] # Concatenate captions, limit to 255 chars
|
| 167 |
dpr_record = sf.Daily_Progress_Reports__c.create({
|
|
|
|
| 168 |
'Report_Description__c': report_description
|
| 169 |
})
|
| 170 |
dpr_record_id = dpr_record['id']
|
|
@@ -172,7 +176,7 @@ def generate_dpr(files):
|
|
| 172 |
|
| 173 |
# Upload PDF to Salesforce
|
| 174 |
pdf_content_document_id, pdf_upload_result = upload_file_to_salesforce(
|
| 175 |
-
pdf_filepath, pdf_filename, sf
|
| 176 |
)
|
| 177 |
salesforce_result += pdf_upload_result + "\n"
|
| 178 |
|
|
@@ -188,7 +192,7 @@ def generate_dpr(files):
|
|
| 188 |
for file in files:
|
| 189 |
image_filename = os.path.basename(file.name)
|
| 190 |
image_content_document_id, image_upload_result = upload_file_to_salesforce(
|
| 191 |
-
file.name, image_filename, sf
|
| 192 |
)
|
| 193 |
if image_content_document_id:
|
| 194 |
image_content_document_ids.append(image_content_document_id)
|
|
|
|
| 95 |
return f"Error saving PDF: {str(e)}", None
|
| 96 |
|
| 97 |
# Function to upload a file to Salesforce as ContentVersion
|
| 98 |
+
def upload_file_to_salesforce(file_path, filename, sf_connection, file_type):
|
| 99 |
try:
|
| 100 |
# Read file content and encode in base64
|
| 101 |
with open(file_path, 'rb') as f:
|
| 102 |
file_content = f.read()
|
| 103 |
file_content_b64 = base64.b64encode(file_content).decode('utf-8')
|
| 104 |
|
| 105 |
+
# Set description based on file type
|
| 106 |
+
description = "Daily Progress Report PDF" if file_type == "pdf" else "Site Image"
|
| 107 |
+
|
| 108 |
# Create ContentVersion
|
| 109 |
content_version = sf_connection.ContentVersion.create({
|
| 110 |
'Title': filename,
|
| 111 |
'PathOnClient': filename,
|
| 112 |
'VersionData': file_content_b64,
|
| 113 |
+
'Description': description
|
| 114 |
})
|
| 115 |
|
| 116 |
# Get ContentDocumentId
|
|
|
|
| 168 |
# Create Daily_Progress_Reports__c record
|
| 169 |
report_description = "; ".join(captions)[:255] # Concatenate captions, limit to 255 chars
|
| 170 |
dpr_record = sf.Daily_Progress_Reports__c.create({
|
| 171 |
+
|
| 172 |
'Report_Description__c': report_description
|
| 173 |
})
|
| 174 |
dpr_record_id = dpr_record['id']
|
|
|
|
| 176 |
|
| 177 |
# Upload PDF to Salesforce
|
| 178 |
pdf_content_document_id, pdf_upload_result = upload_file_to_salesforce(
|
| 179 |
+
pdf_filepath, pdf_filename, sf, "pdf"
|
| 180 |
)
|
| 181 |
salesforce_result += pdf_upload_result + "\n"
|
| 182 |
|
|
|
|
| 192 |
for file in files:
|
| 193 |
image_filename = os.path.basename(file.name)
|
| 194 |
image_content_document_id, image_upload_result = upload_file_to_salesforce(
|
| 195 |
+
file.name, image_filename, sf, "image"
|
| 196 |
)
|
| 197 |
if image_content_document_id:
|
| 198 |
image_content_document_ids.append(image_content_document_id)
|