so i have a print report function as follows:
def print_first_consult_report(self):
return self.env.ref('my_model.action_my_report').report_action(self)
now I need to write a corn job to download this report and attach it to a binary field which I have
@api.model
def download_and_attach_report(self):
# Get all timelines (you may want to filter this based on your requirements)
timelines = self.search([]) # Adjust the domain as necessary
for timeline in timelines:
report = self.env.ref('my_model.action_my_report')
if report._name == 'ir.actions.report':
# Render the PDF for the specific record ID(s)
pdf_content, _ = report._render_qweb_pdf([self.id])
# If you need to save it as an attachment
data_record = base64.b64encode(pdf_content)
attachment = self.env['attachment.model'].create({
'name': "First consult",
'datas': data_record,
'type': 'binary',
'patient_attachment_timeline_id': self.id,
'mimetype': 'application/x-pdf'
})
# Attach it to the patient timeline
self.patient_attachment_ids = [(4, attachment.id)]
else:
raise ValueError("The specified report action was not found or is not correctly defined.")
I tried the following approach by looking through other similar questions but I cant manage to do it can someone guide me on what to do