Skip to content

Commit

Permalink
[MIG] report_qweb_encrypt: Migration to 16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
payen000 committed Aug 10, 2023
1 parent 484b258 commit 065973e
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 44 deletions.
8 changes: 3 additions & 5 deletions report_qweb_encrypt/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"],
}
6 changes: 3 additions & 3 deletions report_qweb_encrypt/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 16 in report_qweb_encrypt/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

report_qweb_encrypt/controllers/main.py#L16

Added line #L16 was not covered by tests
# 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.
Expand Down
18 changes: 12 additions & 6 deletions report_qweb_encrypt/models/ir_actions_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
13 changes: 0 additions & 13 deletions report_qweb_encrypt/static/src/js/report/encrypt_dialog.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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}`;
}
}
Expand All @@ -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 {
Expand All @@ -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")
Expand Down
16 changes: 16 additions & 0 deletions report_qweb_encrypt/static/src/report/encrypt_dialog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t t-name="report_qweb_encrypt.EncryptDialogBody" owl="1">
<Dialog size="'sm'" title="'Encrypt'">
<div id="qweb_encrypt_pdf_password">
<input class="o_password" type="password" />
</div>
<t t-set-slot="footer">
<button class="btn btn-primary" t-on-click="onClick">Ok</button>
<button class="btn" t-on-click="this.props.close">Cancel</button>
</t>
</Dialog>
</t>

</templates>
14 changes: 8 additions & 6 deletions report_qweb_encrypt/tests/test_report_qweb_encrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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?

0 comments on commit 065973e

Please sign in to comment.