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 ed4a16f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
58 changes: 55 additions & 3 deletions report_fillpdf/models/ir_report.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# Copyright 2017 Creu Blanca
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

import logging
from collections import OrderedDict

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

_logger = logging.getLogger(__name__)


class ReportAction(models.Model):
_inherit = "ir.actions.report"
Expand All @@ -14,13 +19,60 @@ 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(
docids, data
)

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):
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 ed4a16f

Please sign in to comment.