Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -111,43 +111,43 @@ 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
|
115 |
-
|
116 |
-
def upload_file_to_salesforce(file_path, file_name, sf_connection, file_type):
|
117 |
try:
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
# Get ContentDocumentId
|
135 |
-
content_version_id = content_version['id']
|
136 |
-
content_document = sf_connection.query(
|
137 |
-
f"SELECT ContentDocumentId FROM ContentVersion WHERE Id = '{content_version_id}'"
|
138 |
-
)
|
139 |
-
content_document_id = content_document['records'][0]['ContentDocumentId']
|
140 |
-
|
141 |
-
# Correctly generate Salesforce URL (use the correct instance and format)
|
142 |
-
instance_url = sf_connection.sf_instance # Salesforce instance (e.g., na1, eu2)
|
143 |
-
content_document_url = f"https://{instance_url}.salesforce.com/sfc/servlet.shepherd/document/download/{content_document_id}"
|
144 |
|
145 |
-
|
146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
except Exception as e:
|
148 |
-
|
|
|
149 |
|
150 |
-
# Function to generate the daily progress report (DPR), save as PDF, and upload to Salesforce
|
151 |
def generate_dpr(files):
|
152 |
dpr_text = []
|
153 |
captions = []
|
@@ -200,7 +200,7 @@ def generate_dpr(files):
|
|
200 |
salesforce_result += f"Created Daily_Progress_Reports__c record with ID: {dpr_record_id}\n"
|
201 |
|
202 |
# Upload PDF to Salesforce
|
203 |
-
pdf_content_document_id, pdf_url, pdf_upload_result =
|
204 |
pdf_filepath, pdf_filename, sf, "pdf"
|
205 |
)
|
206 |
salesforce_result += pdf_upload_result + "\n"
|
@@ -270,4 +270,3 @@ iface = gr.Interface(
|
|
270 |
|
271 |
if __name__ == "__main__":
|
272 |
iface.launch()
|
273 |
-
|
|
|
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 |
+
content_version_data = {
|
125 |
+
"Title": f"{project_title} - Gantt Chart PDF",
|
126 |
+
"PathOnClient": f"{project_title}_Gantt_Chart.pdf",
|
127 |
+
"VersionData": encoded_pdf_data,
|
128 |
+
}
|
129 |
+
|
130 |
+
if record_id:
|
131 |
+
content_version_data["FirstPublishLocationId"] = record_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
|
133 |
+
content_version = sf.ContentVersion.create(content_version_data)
|
134 |
+
content_version_id = content_version["id"]
|
135 |
+
print(f"PDF uploaded to Salesforce with ContentVersion ID: {content_version_id}")
|
136 |
+
|
137 |
+
result = sf.query(f"SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id = '{content_version_id}'")
|
138 |
+
if not result['records']:
|
139 |
+
print("No records returned for ContentVersion query")
|
140 |
+
return content_version_id, None
|
141 |
+
|
142 |
+
content_document_id = result['records'][0]['ContentDocumentId']
|
143 |
+
file_url = f"https://{sf.sf_instance}.salesforce.com/sfc/servlet.shepherd/document/download/{content_document_id}"
|
144 |
+
print(f"Generated PDF URL: {file_url}")
|
145 |
+
return content_version_id, file_url
|
146 |
except Exception as e:
|
147 |
+
print(f"Error uploading PDF to Salesforce: {str(e)}", exc_info=True)
|
148 |
+
return None, None
|
149 |
|
150 |
+
# Function to upload files and generate the daily progress report (DPR), save as PDF, and upload to Salesforce
|
151 |
def generate_dpr(files):
|
152 |
dpr_text = []
|
153 |
captions = []
|
|
|
200 |
salesforce_result += f"Created Daily_Progress_Reports__c record with ID: {dpr_record_id}\n"
|
201 |
|
202 |
# Upload PDF to Salesforce
|
203 |
+
pdf_content_document_id, pdf_url, pdf_upload_result = upload_pdf_to_salesforce(
|
204 |
pdf_filepath, pdf_filename, sf, "pdf"
|
205 |
)
|
206 |
salesforce_result += pdf_upload_result + "\n"
|
|
|
270 |
|
271 |
if __name__ == "__main__":
|
272 |
iface.launch()
|
|