Rammohan0504 commited on
Commit
1f8a64a
·
verified ·
1 Parent(s): 9349e75

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -5
app.py CHANGED
@@ -59,11 +59,11 @@ def create_and_upload_pdf(dpr_content):
59
  pdf_output_path = "/tmp/dpr_report.pdf"
60
  pdf.output(pdf_output_path)
61
 
62
- # Upload PDF to Salesforce as ContentVersion
63
  with open(pdf_output_path, 'rb') as pdf_file:
64
  pdf_data = pdf_file.read()
65
 
66
- # Prepare request to Salesforce
67
  content_version_payload = {
68
  "Title": "Daily Progress Report",
69
  "PathOnClient": "DPR_Report.pdf",
@@ -73,10 +73,15 @@ def create_and_upload_pdf(dpr_content):
73
  content_version_url = f"{sf.base_url}/services/data/v50.0/sobjects/ContentVersion/"
74
  headers = {
75
  "Authorization": f"Bearer {sf.session_id}",
76
- "Content-Type": "application/json"
77
  }
78
 
79
- response = requests.post(content_version_url, headers=headers, files={"VersionData": pdf_data}, data=json.dumps(content_version_payload))
 
 
 
 
 
80
 
81
  if response.status_code == 201:
82
  content_version = response.json()
@@ -129,7 +134,7 @@ def generate_dpr(files):
129
 
130
  # Create the new Daily Progress Report record in Salesforce
131
  new_report_data = {
132
- "Daily_Progress_Reports_Name__c": new_report_name, # Set the dynamic name
133
  "Report_Date__c": current_time, # Set the report date to the current time
134
  "Detected_Activities__c": dpr_content, # Set the activities field to the generated text
135
  "Photo_Uploads__c": ", ".join([file.name for file in files]), # Upload the image names as Photo Uploads
 
59
  pdf_output_path = "/tmp/dpr_report.pdf"
60
  pdf.output(pdf_output_path)
61
 
62
+ # Read the generated PDF as binary data
63
  with open(pdf_output_path, 'rb') as pdf_file:
64
  pdf_data = pdf_file.read()
65
 
66
+ # Prepare request to Salesforce ContentVersion API
67
  content_version_payload = {
68
  "Title": "Daily Progress Report",
69
  "PathOnClient": "DPR_Report.pdf",
 
73
  content_version_url = f"{sf.base_url}/services/data/v50.0/sobjects/ContentVersion/"
74
  headers = {
75
  "Authorization": f"Bearer {sf.session_id}",
76
+ "Content-Type": "application/json" # Keep content type as JSON for the metadata
77
  }
78
 
79
+ # Pass the binary data in the files parameter (do not include it in the JSON payload)
80
+ files = {
81
+ "VersionData": pdf_data
82
+ }
83
+
84
+ response = requests.post(content_version_url, headers=headers, files=files, data=json.dumps(content_version_payload))
85
 
86
  if response.status_code == 201:
87
  content_version = response.json()
 
134
 
135
  # Create the new Daily Progress Report record in Salesforce
136
  new_report_data = {
137
+ "Name": new_report_name, # Set the dynamic name
138
  "Report_Date__c": current_time, # Set the report date to the current time
139
  "Detected_Activities__c": dpr_content, # Set the activities field to the generated text
140
  "Photo_Uploads__c": ", ".join([file.name for file in files]), # Upload the image names as Photo Uploads