From 065973ebd0131e152dad9118de7ebb5552723836 Mon Sep 17 00:00:00 2001 From: payen000 Date: Thu, 3 Aug 2023 09:00:52 -0700 Subject: [PATCH] [MIG] report_qweb_encrypt: Migration to 16.0 --- report_qweb_encrypt/__manifest__.py | 8 +++---- report_qweb_encrypt/controllers/main.py | 6 ++--- .../models/ir_actions_report.py | 18 +++++++++----- .../static/src/js/report/encrypt_dialog.xml | 13 ---------- .../report/action_manager_report.esm.js | 24 ++++++++++--------- .../static/src/report/encrypt_dialog.xml | 16 +++++++++++++ .../tests/test_report_qweb_encrypt.py | 14 ++++++----- 7 files changed, 55 insertions(+), 44 deletions(-) delete mode 100644 report_qweb_encrypt/static/src/js/report/encrypt_dialog.xml rename report_qweb_encrypt/static/src/{js => }/report/action_manager_report.esm.js (77%) create mode 100644 report_qweb_encrypt/static/src/report/encrypt_dialog.xml diff --git a/report_qweb_encrypt/__manifest__.py b/report_qweb_encrypt/__manifest__.py index 4644b6ffce..8442ddbd1b 100644 --- a/report_qweb_encrypt/__manifest__.py +++ b/report_qweb_encrypt/__manifest__.py @@ -5,7 +5,7 @@ { "name": "Report Qweb Encrypt", "summary": "Allow to encrypt qweb pdfs", - "version": "15.0.1.0.0", + "version": "16.0.1.0.0", "license": "AGPL-3", "author": "Creu Blanca,Ecosoft,Odoo Community Association (OCA)", "website": "https://github.com/OCA/reporting-engine", @@ -17,12 +17,10 @@ ], "assets": { "web.assets_backend": [ - "report_qweb_encrypt/static/src/js/report/action_manager_report.esm.js", + "report_qweb_encrypt/static/src/report/action_manager_report.esm.js", + "report_qweb_encrypt/static/src/report/encrypt_dialog.xml", ], }, - "external_dependencies": { - "python": ["PyPDF2"] # Python third party libraries required for module - }, "installable": True, "maintainers": ["kittiu"], } diff --git a/report_qweb_encrypt/controllers/main.py b/report_qweb_encrypt/controllers/main.py index f7beea4aa8..f3450ed254 100644 --- a/report_qweb_encrypt/controllers/main.py +++ b/report_qweb_encrypt/controllers/main.py @@ -7,13 +7,13 @@ from odoo.http import request, route -from odoo.addons.web.controllers import main as report +from odoo.addons.web.controllers.report import ReportController -class ReportController(report.ReportController): +class ReportControllerEncrypt(ReportController): @route() def report_download(self, data, context=None): - result = super(ReportController, self).report_download(data, context=context) + result = super().report_download(data, context=context) # When report is downloaded from print action, this function is called, # but this function cannot pass context (manually entered password) to # report.render_qweb_pdf(), encrypton for manual password is done here. diff --git a/report_qweb_encrypt/models/ir_actions_report.py b/report_qweb_encrypt/models/ir_actions_report.py index b72c7b9017..6df9a3d04b 100644 --- a/report_qweb_encrypt/models/ir_actions_report.py +++ b/report_qweb_encrypt/models/ir_actions_report.py @@ -25,12 +25,16 @@ class IrActionsReport(models.Model): help="Python code syntax to gnerate password.", ) - def _render_qweb_pdf(self, res_ids=None, data=None): - document, ttype = super(IrActionsReport, self)._render_qweb_pdf( - res_ids=res_ids, data=data + def _render_qweb_pdf(self, reportname, res_ids=None, data=None): + document, ttype = super()._render_qweb_pdf( + reportname, res_ids=res_ids, data=data ) if res_ids: - password = self._get_pdf_password(res_ids[:1]) + encrypt_password = self._context.get("encrypt_password") + report = self._get_report_from_name(reportname).with_context( + encrypt_password=encrypt_password + ) + password = report._get_pdf_password(res_ids[:1]) document = self._encrypt_pdf(document, password) return document, ttype @@ -55,8 +59,10 @@ def _get_pdf_password(self, res_ids): ) except Exception as e: raise ValidationError( - _("Python code used for encryption password is invalid.\n%s") - % self.encrypt_password + _( + "Python code used for encryption password is invalid.\n%s", + self.encrypt_password, + ) ) from e return encrypt_password diff --git a/report_qweb_encrypt/static/src/js/report/encrypt_dialog.xml b/report_qweb_encrypt/static/src/js/report/encrypt_dialog.xml deleted file mode 100644 index 45c359fc96..0000000000 --- a/report_qweb_encrypt/static/src/js/report/encrypt_dialog.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - -
-
- -
diff --git a/report_qweb_encrypt/static/src/js/report/action_manager_report.esm.js b/report_qweb_encrypt/static/src/report/action_manager_report.esm.js similarity index 77% rename from report_qweb_encrypt/static/src/js/report/action_manager_report.esm.js rename to report_qweb_encrypt/static/src/report/action_manager_report.esm.js index fcf110a3f4..48e5664fa4 100644 --- a/report_qweb_encrypt/static/src/js/report/action_manager_report.esm.js +++ b/report_qweb_encrypt/static/src/report/action_manager_report.esm.js @@ -3,11 +3,15 @@ import {Dialog} from "@web/core/dialog/dialog"; import {download} from "@web/core/network/download"; import {registry} from "@web/core/registry"; +const {Component} = owl; async function download_function(action, options, env) { - const type = action.report_type; + const type = action.report_type === "qweb-pdf" ? "pdf" : action.report_type; let url = `/report/${type}/${action.report_name}`; const actionContext = action.context || {}; + const userContext = Object.assign(env.services.user.context, { + encrypt_password: action.context.encrypt_password, + }); if (action.data && JSON.stringify(action.data) !== "{}") { // Build a query string with `action.data` (it's the place where reports // using a wizard to customize the output traditionally put their options) @@ -19,9 +23,7 @@ async function download_function(action, options, env) { url += `/${actionContext.active_ids.join(",")}`; } if (type === "html") { - const context = encodeURIComponent( - JSON.stringify(env.services.user.context) - ); + const context = encodeURIComponent(JSON.stringify(userContext)); url += `?context=${context}`; } } @@ -31,7 +33,7 @@ async function download_function(action, options, env) { url: "/report/download", data: { data: JSON.stringify([url, action.report_type]), - context: JSON.stringify(env.services.user.context), + context: JSON.stringify(userContext), }, }); } finally { @@ -49,19 +51,19 @@ async function download_function(action, options, env) { return Promise.resolve(true); } -class EncryptDialog extends Dialog { +class EncryptDialog extends Component { onClick() { const action = this.props.action; action.context = _.extend({}, action.context, { - encrypt_password: this.el.find(".o_password").val() || false, + encrypt_password: + $("#qweb_encrypt_pdf_password > .o_password").val() || false, }); return download_function(action, this.props.options, this.props.env); } } -EncryptDialog.size = "small"; -EncryptDialog.title = "Encrypt"; -EncryptDialog.bodyTemplate = "report_qweb_encrypt.EncryptDialogBody"; -EncryptDialog.footerTemplate = "report_qweb_encrypt.EncryptDialogFooter"; + +EncryptDialog.components = {Dialog}; +EncryptDialog.template = "report_qweb_encrypt.EncryptDialogBody"; registry .category("ir.actions.report handlers") diff --git a/report_qweb_encrypt/static/src/report/encrypt_dialog.xml b/report_qweb_encrypt/static/src/report/encrypt_dialog.xml new file mode 100644 index 0000000000..3027450055 --- /dev/null +++ b/report_qweb_encrypt/static/src/report/encrypt_dialog.xml @@ -0,0 +1,16 @@ + + + + + +
+ +
+ + + + +
+
+ +
diff --git a/report_qweb_encrypt/tests/test_report_qweb_encrypt.py b/report_qweb_encrypt/tests/test_report_qweb_encrypt.py index 0de5e0cb0b..2095531c1d 100644 --- a/report_qweb_encrypt/tests/test_report_qweb_encrypt.py +++ b/report_qweb_encrypt/tests/test_report_qweb_encrypt.py @@ -9,7 +9,7 @@ def test_report_qweb_no_encrypt(self): ctx = {"force_report_rendering": True} report = self.env.ref("web.action_report_internalpreview") report.encrypt = False - pdf, _ = report.with_context(**ctx)._render_qweb_pdf([1]) + pdf, _ = report.with_context(**ctx)._render_qweb_pdf(report.report_name, [1]) self.assertFalse(pdf.count(b"/Encrypt")) def test_report_qweb_auto_encrypt(self): @@ -19,17 +19,19 @@ def test_report_qweb_auto_encrypt(self): report.encrypt_password = False # If no encrypt_password, still not encrypted - pdf, _ = report.with_context(**ctx)._render_qweb_pdf([1]) + pdf, _ = report.with_context(**ctx)._render_qweb_pdf(report.report_name, [1]) self.assertFalse(pdf.count(b"/Encrypt")) # If invalid encrypt_password, show error report.encrypt_password = "invalid python syntax" with self.assertRaises(ValidationError): - pdf, _ = report.with_context(**ctx)._render_qweb_pdf([1]) + pdf, _ = report.with_context(**ctx)._render_qweb_pdf( + report.report_name, [1] + ) # Valid python string for password report.encrypt_password = "'secretcode'" - pdf, _ = report.with_context(**ctx)._render_qweb_pdf([1]) + pdf, _ = report.with_context(**ctx)._render_qweb_pdf(report.report_name, [1]) self.assertTrue(pdf.count(b"/Encrypt")) def test_report_qweb_manual_encrypt(self): @@ -38,12 +40,12 @@ def test_report_qweb_manual_encrypt(self): report.encrypt = "manual" # If no encrypt_password, still not encrypted - pdf, _ = report.with_context(**ctx)._render_qweb_pdf([1]) + pdf, _ = report.with_context(**ctx)._render_qweb_pdf(report.report_name, [1]) self.assertFalse(pdf.count(b"/Encrypt")) # Valid python string for password ctx.update({"encrypt_password": "secretcode"}) - pdf, _ = report.with_context(**ctx)._render_qweb_pdf([1]) + pdf, _ = report.with_context(**ctx)._render_qweb_pdf(report.report_name, [1]) self.assertTrue(pdf.count(b"/Encrypt")) # TODO: test_report_qweb_manual_encrypt, require JS test?