Skip to content

Commit

Permalink
[IMP] Add attachment support to report_fillpdf
Browse files Browse the repository at this point in the history
  • Loading branch information
cvinh committed Mar 13, 2023
1 parent 277eff7 commit 26e670b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
46 changes: 45 additions & 1 deletion report_fillpdf/models/ir_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

from odoo import _, api, fields, models
from odoo.exceptions import UserError
from collections import OrderedDict

import logging

_logger = logging.getLogger(__name__)


class ReportAction(models.Model):
Expand All @@ -14,14 +19,53 @@ class ReportAction(models.Model):

@api.model
def render_fillpdf(self, docids, data):
# Here we add pdf attachment support
self_sudo = self.sudo()
save_in_attachment = OrderedDict()
# Maps the streams in `save_in_attachment` back to the records they came from
stream_record = dict()
if docids:
# Dispatch the records by ones having an attachment and ones requesting a call to
# fillpdf.
Model = self.env[self_sudo.model]
record_ids = Model.browse(docids)
fp_record_ids = Model
if self_sudo.attachment:
for record_id in record_ids:
attachment = self_sudo.retrieve_attachment(record_id)
if attachment:
stream = self_sudo._retrieve_stream_from_attachment(attachment)
save_in_attachment[record_id.id] = stream
stream_record[stream] = record_id
if not self_sudo.attachment_use or not attachment:
fp_record_ids += record_id

else:
fp_record_ids = record_ids
docids = fp_record_ids.ids

if save_in_attachment and not docids:
_logger.info('The PDF report has been generated from attachments.')
self._raise_on_unreadable_pdfs(save_in_attachment.values(), stream_record)
return self_sudo._post_pdf(save_in_attachment), 'pdf'

# We generate pdf with fillpdf
report_model_name = "report.%s" % self.report_name
report_model = self.env.get(report_model_name)
if report_model is None:
raise UserError(_("%s model was not found" % report_model_name))
return report_model.with_context({"active_model": self.model}).fill_report(

pdf_content = report_model.with_context({"active_model": self.model}).fill_report(
docids, data
)

if docids:
self._raise_on_unreadable_pdfs(save_in_attachment.values(), stream_record)
_logger.info('The PDF report has been generated for model: %s, records %s.' % (self_sudo.model, str(docids)))
return self_sudo._post_pdf(save_in_attachment, pdf_content=pdf_content, res_ids=docids), 'pdf'

return pdf_content, 'pdf'

@api.model
def _get_report_from_name(self, report_name):
res = super(ReportAction, self)._get_report_from_name(report_name)
Expand Down
7 changes: 2 additions & 5 deletions report_fillpdf/report/report_fill_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ class ReportFillPDFAbstract(models.AbstractModel):

def fill_report(self, docids, data):
objs = self.env[self.env.context.get("active_model")].browse(docids)
return (
self.fill_pdf_form(
self.get_form(data, objs), self.get_document_values(data, objs)
),
"pdf",
return self.fill_pdf_form(
self.get_form(data, objs), self.get_document_values(data, objs)
)

@api.model
Expand Down

0 comments on commit 26e670b

Please sign in to comment.