Skip to content
Snippets Groups Projects
Commit f62f80afc1a1 authored by oury.balde's avatar oury.balde
Browse files

Save template redner as an attachment

Add constraint on attachment field

Note that it is normally expected that when printing a report from an object,
the created document is saved as an attachment to the object
if the field "Save as attachment prefix" is filled in.

Problem:
This does not work for redner reports (works for QWEB and worked for py3o).
parent 0cdf1988db5e
No related branches found
No related tags found
1 merge request!13Save template redner as an attachment
...@@ -2,6 +2,11 @@ ...@@ -2,6 +2,11 @@
NEWS NEWS
==== ====
13.0.3.0.1
==========
Save template redner as an attachment
13.0.3.0.0 13.0.3.0.0
========== ==========
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
"name": "Redner", "name": "Redner",
"summary": """ "summary": """
Allows to generate transactional emails and documents in PDF or HTML format""", Allows to generate transactional emails and documents in PDF or HTML format""",
"version": "13.0.3.0.0", "version": "13.0.3.0.1",
"category": "Technical", "category": "Technical",
"author": "XCG Consulting", "author": "XCG Consulting",
"website": "http://odoo.consulting/", "website": "http://odoo.consulting/",
......
...@@ -6,8 +6,8 @@ ...@@ -6,8 +6,8 @@
msgstr "" msgstr ""
"Project-Id-Version: Odoo Server 11.0\n" "Project-Id-Version: Odoo Server 11.0\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2021-05-03 13:50+0000\n" "POT-Creation-Date: 2022-03-04 11:11+0000\n"
"PO-Revision-Date: 2021-05-03 15:56+0200\n" "PO-Revision-Date: 2022-03-04 12:12+0100\n"
"Last-Translator: Vincent Hatakeyama <vincent.hatakeyama@xcg-consulting.fr>\n" "Last-Translator: Vincent Hatakeyama <vincent.hatakeyama@xcg-consulting.fr>\n"
"Language-Team: XCG Consulting\n" "Language-Team: XCG Consulting\n"
"Language: fr\n" "Language: fr\n"
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
"Content-Type: text/plain; charset=UTF-8\n" "Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n" "Plural-Forms: nplurals=2; plural=(n > 1);\n"
"X-Generator: Virtaal 0.7.1\n" "X-Generator: Poedit 2.3\n"
#. module: redner #. module: redner
#: code:addons/redner/models/redner_template.py:0 #: code:addons/redner/models/redner_template.py:0
...@@ -43,8 +43,8 @@ ...@@ -43,8 +43,8 @@
#: model:ir.model.fields,help:redner.field_email_template_preview__is_redner_template #: model:ir.model.fields,help:redner.field_email_template_preview__is_redner_template
#: model:ir.model.fields,help:redner.field_mail_template__is_redner_template #: model:ir.model.fields,help:redner.field_mail_template__is_redner_template
msgid "" msgid ""
"Check this box if this template is a redner. If it's not checked, it assumes" "Check this box if this template is a redner. If it's not checked, it assumes "
" that the default email template odoo to be used." "that the default email template odoo to be used."
msgstr "" msgstr ""
#. module: redner #. module: redner
...@@ -147,9 +147,8 @@ ...@@ -147,9 +147,8 @@
#: model:ir.model.fields,help:redner.field_ir_actions_report__redner_multi_in_one #: model:ir.model.fields,help:redner.field_ir_actions_report__redner_multi_in_one
msgid "" msgid ""
"If you execute a report on several records, by default Odoo will generate a " "If you execute a report on several records, by default Odoo will generate a "
"ZIP file that contains as many files as selected records. If you enable this" "ZIP file that contains as many files as selected records. If you enable this "
" option, Odoo will generate instead a single report for the selected " "option, Odoo will generate instead a single report for the selected records."
"records."
msgstr "" msgstr ""
#. module: redner #. module: redner
...@@ -385,6 +384,15 @@ ...@@ -385,6 +384,15 @@
msgstr "Modèles" msgstr "Modèles"
#. module: redner #. module: redner
#: code:addons/redner/models/ir_actions_report.py:0
#, python-format
msgid ""
"The output format cannot be different from the extension defined in \"Save "
"as attachment prefix\"."
msgstr "Le format de sortie ne peut être différent de l'extension définie dans \"Enregistrer "
"comme préfixe de pièce jointe\"."
#. module: redner
#: model:ir.model.fields,help:redner.field_ir_actions_report__report_type #: model:ir.model.fields,help:redner.field_ir_actions_report__report_type
msgid "" msgid ""
"The type of the report that will be rendered, each one having its own " "The type of the report that will be rendered, each one having its own "
...@@ -458,11 +466,4 @@ ...@@ -458,11 +466,4 @@
#. module: redner #. module: redner
#: model:ir.model.fields,help:redner.field_redner_template__language #: model:ir.model.fields,help:redner.field_redner_template__language
msgid "templating language" msgid "templating language"
msgstr "templating language" msgstr "templating language"
\ No newline at end of file
#~ msgid ""
#~ "Check this box if this template is a redner. If it's not checked , it "
#~ "assumes that the default email template odoo to be used."
#~ msgstr ""
#~ "Check this box if this template is a redner. If it's not checked , it "
#~ "assumes that the default email template odoo to be used."
import base64
import logging import logging
import time import time
from odoo import _, api, fields, models from odoo import _, api, fields, models
...@@ -1,8 +2,8 @@ ...@@ -1,8 +2,8 @@
import logging import logging
import time import time
from odoo import _, api, fields, models from odoo import _, api, fields, models
from odoo.exceptions import ValidationError from odoo.exceptions import AccessError, ValidationError
from odoo.tools.safe_eval import safe_eval from odoo.tools.safe_eval import safe_eval
from odoo.addons.redner.utils.formats import Formats from odoo.addons.redner.utils.formats import Formats
...@@ -26,6 +27,19 @@ ...@@ -26,6 +27,19 @@
_("Field 'Output Format' is required for Redner report") _("Field 'Output Format' is required for Redner report")
) )
@api.constrains("redner_filetype", "attachment")
def _check_redner_attachment(self):
for report in self:
if report.report_type == "redner" and report.attachment:
ext = ".{ftype}".format(ftype=report.redner_filetype)
if ext not in report.attachment:
raise ValidationError(
_(
"The output format cannot be different from the "
'extension defined in "Save as attachment prefix".'
)
)
@api.model @api.model
def _get_redner_filetypes(self): def _get_redner_filetypes(self):
formats = Formats() formats = Formats()
...@@ -191,3 +205,43 @@ ...@@ -191,3 +205,43 @@
if attachment_id: if attachment_id:
save_in_attachment[record_id.id] = attachment_id save_in_attachment[record_id.id] = attachment_id
return save_in_attachment return save_in_attachment
def postprocess_redner_report(self, record, buffer):
"""Handle post processing during the report generation.
The basic behavior consists to create a new attachment containing
the document base64 encoded.
"""
self.ensure_one()
attachment_name = safe_eval(
self.attachment, {"object": record, "time": time}
)
if not attachment_name:
return None
if not attachment_name.endswith(
".{filetype}".format(filetype=self.redner_filetype)
):
return None
attachment_vals = {
"name": attachment_name,
"datas": base64.encodestring(buffer.getvalue()),
"res_model": self.model,
"res_id": record.id,
"type": "binary",
}
try:
self.env["ir.attachment"].create(attachment_vals)
except AccessError:
_logger.info(
"Cannot save report %r as attachment", attachment_vals["name"]
)
else:
_logger.info(
"The document %s is now saved in the database",
attachment_vals["name"],
)
return buffer
...@@ -39,7 +39,7 @@ ...@@ -39,7 +39,7 @@
# consumption... # consumption...
# ... but odoo wants the whole data in memory anyways :) # ... but odoo wants the whole data in memory anyways :)
buffer = BytesIO(f.read()) buffer = BytesIO(f.read())
self.ir_actions_report_id.postprocess_pdf_report( self.ir_actions_report_id.postprocess_redner_report(
model_instance, buffer model_instance, buffer
) )
return result_path return result_path
...@@ -78,6 +78,8 @@ ...@@ -78,6 +78,8 @@
content = base64.b64decode(res[0]["body"]) content = base64.b64decode(res[0]["body"])
with open(report_file, "wb") as f: with open(report_file, "wb") as f:
f.write(content) f.write(content)
self._postprocess_report(model_instance, report_file)
return report_file return report_file
def _get_or_create_single_report( def _get_or_create_single_report(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment