Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -111,20 +111,17 @@ def save_dpr_to_pdf(dpr_text, image_paths, captions, filename):
|
|
111 |
except Exception as e:
|
112 |
return f"Error saving PDF: {str(e)}", None
|
113 |
|
114 |
-
# Function to upload a file to Salesforce as ContentVersion and get its download URL
|
115 |
# Function to upload a file to Salesforce as ContentVersion and get its download URL
|
116 |
def upload_pdf_to_salesforce(pdf_file, project_title, record_id=None):
|
117 |
try:
|
118 |
-
sf = get_salesforce_connection()
|
119 |
if not sf:
|
120 |
print("Salesforce connection failed. Cannot upload PDF.")
|
121 |
return None, None
|
122 |
|
123 |
-
# Encoding the file in base64
|
124 |
encoded_pdf_data = base64.b64encode(pdf_file.getvalue()).decode('utf-8')
|
125 |
print(f"Uploading PDF for project: {project_title}, record ID: {record_id}")
|
126 |
|
127 |
-
# Create ContentVersion data to upload to Salesforce
|
128 |
content_version_data = {
|
129 |
"Title": f"{project_title} - Gantt Chart PDF",
|
130 |
"PathOnClient": f"{project_title}_Gantt_Chart.pdf",
|
@@ -134,26 +131,28 @@ def upload_pdf_to_salesforce(pdf_file, project_title, record_id=None):
|
|
134 |
if record_id:
|
135 |
content_version_data["FirstPublishLocationId"] = record_id
|
136 |
|
137 |
-
# Uploading the PDF to Salesforce
|
138 |
content_version = sf.ContentVersion.create(content_version_data)
|
139 |
content_version_id = content_version["id"]
|
140 |
print(f"PDF uploaded to Salesforce with ContentVersion ID: {content_version_id}")
|
141 |
|
142 |
-
#
|
143 |
result = sf.query(f"SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id = '{content_version_id}'")
|
144 |
if not result['records']:
|
145 |
print("No records returned for ContentVersion query")
|
146 |
return content_version_id, None
|
147 |
|
148 |
content_document_id = result['records'][0]['ContentDocumentId']
|
149 |
-
|
|
|
|
|
|
|
150 |
print(f"Generated PDF URL: {file_url}")
|
151 |
return content_version_id, file_url
|
152 |
except Exception as e:
|
153 |
print(f"Error uploading PDF to Salesforce: {str(e)}", exc_info=True)
|
154 |
return None, None
|
155 |
|
156 |
-
#
|
157 |
def generate_dpr(files):
|
158 |
dpr_text = []
|
159 |
captions = []
|
@@ -205,7 +204,7 @@ def generate_dpr(files):
|
|
205 |
dpr_record_id = dpr_record['id']
|
206 |
salesforce_result += f"Created Daily_Progress_Reports__c record with ID: {dpr_record_id}\n"
|
207 |
|
208 |
-
#
|
209 |
pdf_content_document_id, pdf_url, pdf_upload_result = upload_pdf_to_salesforce(
|
210 |
pdf_filepath, project_title=pdf_filename # Pass project title as the title
|
211 |
)
|
|
|
111 |
except Exception as e:
|
112 |
return f"Error saving PDF: {str(e)}", None
|
113 |
|
|
|
114 |
# Function to upload a file to Salesforce as ContentVersion and get its download URL
|
115 |
def upload_pdf_to_salesforce(pdf_file, project_title, record_id=None):
|
116 |
try:
|
117 |
+
sf = get_salesforce_connection()
|
118 |
if not sf:
|
119 |
print("Salesforce connection failed. Cannot upload PDF.")
|
120 |
return None, None
|
121 |
|
|
|
122 |
encoded_pdf_data = base64.b64encode(pdf_file.getvalue()).decode('utf-8')
|
123 |
print(f"Uploading PDF for project: {project_title}, record ID: {record_id}")
|
124 |
|
|
|
125 |
content_version_data = {
|
126 |
"Title": f"{project_title} - Gantt Chart PDF",
|
127 |
"PathOnClient": f"{project_title}_Gantt_Chart.pdf",
|
|
|
131 |
if record_id:
|
132 |
content_version_data["FirstPublishLocationId"] = record_id
|
133 |
|
|
|
134 |
content_version = sf.ContentVersion.create(content_version_data)
|
135 |
content_version_id = content_version["id"]
|
136 |
print(f"PDF uploaded to Salesforce with ContentVersion ID: {content_version_id}")
|
137 |
|
138 |
+
# Query the ContentDocumentId
|
139 |
result = sf.query(f"SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id = '{content_version_id}'")
|
140 |
if not result['records']:
|
141 |
print("No records returned for ContentVersion query")
|
142 |
return content_version_id, None
|
143 |
|
144 |
content_document_id = result['records'][0]['ContentDocumentId']
|
145 |
+
|
146 |
+
# Correctly generate Salesforce URL (use the correct instance and format)
|
147 |
+
instance_url = sf.sf_instance # Get Salesforce instance (e.g., na1, eu2)
|
148 |
+
file_url = f"https://{instance_url}.salesforce.com/sfc/servlet.shepherd/document/download/{content_document_id}"
|
149 |
print(f"Generated PDF URL: {file_url}")
|
150 |
return content_version_id, file_url
|
151 |
except Exception as e:
|
152 |
print(f"Error uploading PDF to Salesforce: {str(e)}", exc_info=True)
|
153 |
return None, None
|
154 |
|
155 |
+
# Function to upload files and generate the daily progress report (DPR), save as PDF, and upload to Salesforce
|
156 |
def generate_dpr(files):
|
157 |
dpr_text = []
|
158 |
captions = []
|
|
|
204 |
dpr_record_id = dpr_record['id']
|
205 |
salesforce_result += f"Created Daily_Progress_Reports__c record with ID: {dpr_record_id}\n"
|
206 |
|
207 |
+
# Upload PDF to Salesforce
|
208 |
pdf_content_document_id, pdf_url, pdf_upload_result = upload_pdf_to_salesforce(
|
209 |
pdf_filepath, project_title=pdf_filename # Pass project title as the title
|
210 |
)
|