mirror of
https://github.com/OCA/reporting-engine.git
synced 2025-02-16 16:30:38 +02:00
[MIG] report_qweb_encrypt: Migration to 15.0
This commit is contained in:
@@ -5,14 +5,24 @@
|
|||||||
{
|
{
|
||||||
"name": "Report Qweb Encrypt",
|
"name": "Report Qweb Encrypt",
|
||||||
"summary": "Allow to encrypt qweb pdfs",
|
"summary": "Allow to encrypt qweb pdfs",
|
||||||
"version": "14.0.1.1.0",
|
"version": "15.0.1.0.0",
|
||||||
"license": "AGPL-3",
|
"license": "AGPL-3",
|
||||||
"author": "Creu Blanca,Ecosoft,Odoo Community Association (OCA)",
|
"author": "Creu Blanca,Ecosoft,Odoo Community Association (OCA)",
|
||||||
"website": "https://github.com/OCA/reporting-engine",
|
"website": "https://github.com/OCA/reporting-engine",
|
||||||
"depends": [
|
"depends": [
|
||||||
"web",
|
"web",
|
||||||
],
|
],
|
||||||
"data": ["views/ir_actions_report.xml", "templates/assets.xml"],
|
"data": [
|
||||||
|
"views/ir_actions_report.xml",
|
||||||
|
],
|
||||||
|
"assets": {
|
||||||
|
"web.assets_backend": [
|
||||||
|
"report_qweb_encrypt/static/src/js/report/action_manager_report.esm.js",
|
||||||
|
],
|
||||||
|
},
|
||||||
|
"external_dependencies": {
|
||||||
|
"python": ["PyPDF2"] # Python third party libraries required for module
|
||||||
|
},
|
||||||
"installable": True,
|
"installable": True,
|
||||||
"maintainers": ["kittiu"],
|
"maintainers": ["kittiu"],
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ from odoo.addons.web.controllers import main as report
|
|||||||
|
|
||||||
class ReportController(report.ReportController):
|
class ReportController(report.ReportController):
|
||||||
@route()
|
@route()
|
||||||
def report_download(self, data, token, context=None):
|
def report_download(self, data, context=None):
|
||||||
result = super().report_download(data, token, context=context)
|
result = super(ReportController, self).report_download(data, context=context)
|
||||||
# When report is downloaded from print action, this function is called,
|
# When report is downloaded from print action, this function is called,
|
||||||
# but this function cannot pass context (manually entered password) to
|
# but this function cannot pass context (manually entered password) to
|
||||||
# report.render_qweb_pdf(), encrypton for manual password is done here.
|
# report.render_qweb_pdf(), encrypton for manual password is done here.
|
||||||
@@ -24,14 +24,16 @@ class ReportController(report.ReportController):
|
|||||||
and result.headers["Content-Type"] == "application/pdf"
|
and result.headers["Content-Type"] == "application/pdf"
|
||||||
and "?" in url
|
and "?" in url
|
||||||
):
|
):
|
||||||
url_data = dict(url_decode(url.split("?")[1]).items())
|
data = dict(
|
||||||
if "context" in url_data:
|
url_decode(url.split("?")[1]).items()
|
||||||
context = json.loads(url_data["context"])
|
) # decoding the args represented in JSON
|
||||||
if "encrypt_password" in context:
|
if "context" in data:
|
||||||
Report = request.env["ir.actions.report"]
|
context, data_context = json.loads(context or "{}"), json.loads(
|
||||||
data = result.get_data()
|
data.pop("context")
|
||||||
encrypted_data = Report._encrypt_pdf(
|
)
|
||||||
data, context["encrypt_password"]
|
if "encrypt_password" in data_context:
|
||||||
|
encrypted_data = request.env["ir.actions.report"]._encrypt_pdf(
|
||||||
|
result.get_data(), data_context["encrypt_password"]
|
||||||
)
|
)
|
||||||
result.set_data(encrypted_data)
|
result.set_data(encrypted_data)
|
||||||
return result
|
return result
|
||||||
|
|||||||
@@ -1,19 +1,14 @@
|
|||||||
# Copyright 2020 Creu Blanca
|
# Copyright 2020 Creu Blanca
|
||||||
# Copyright 2020 Ecosoft Co., Ltd.
|
# Copyright 2020 Ecosoft Co., Ltd.
|
||||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
import logging
|
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
|
from PyPDF2 import PdfFileReader, PdfFileWriter
|
||||||
|
|
||||||
from odoo import _, fields, models
|
from odoo import _, fields, models
|
||||||
from odoo.exceptions import ValidationError
|
from odoo.exceptions import ValidationError
|
||||||
from odoo.tools.safe_eval import safe_eval
|
from odoo.tools.safe_eval import safe_eval
|
||||||
|
|
||||||
_logger = logging.getLogger(__name__)
|
|
||||||
try:
|
|
||||||
from PyPDF2 import PdfFileReader, PdfFileWriter
|
|
||||||
except ImportError as err:
|
|
||||||
_logger.debug(err)
|
|
||||||
|
|
||||||
|
|
||||||
class IrActionsReport(models.Model):
|
class IrActionsReport(models.Model):
|
||||||
_inherit = "ir.actions.report"
|
_inherit = "ir.actions.report"
|
||||||
@@ -35,13 +30,11 @@ class IrActionsReport(models.Model):
|
|||||||
res_ids=res_ids, data=data
|
res_ids=res_ids, data=data
|
||||||
)
|
)
|
||||||
if res_ids:
|
if res_ids:
|
||||||
if isinstance(res_ids, int):
|
|
||||||
res_ids = [res_ids]
|
|
||||||
password = self._get_pdf_password(res_ids[:1])
|
password = self._get_pdf_password(res_ids[:1])
|
||||||
document = self._encrypt_pdf(document, password)
|
document = self._encrypt_pdf(document, password)
|
||||||
return document, ttype
|
return document, ttype
|
||||||
|
|
||||||
def _get_pdf_password(self, res_id):
|
def _get_pdf_password(self, res_ids):
|
||||||
encrypt_password = False
|
encrypt_password = False
|
||||||
if self.encrypt == "manual":
|
if self.encrypt == "manual":
|
||||||
# If use document print action, report_download() is called,
|
# If use document print action, report_download() is called,
|
||||||
@@ -51,17 +44,24 @@ class IrActionsReport(models.Model):
|
|||||||
# Following is used just in case when context is passed in.
|
# Following is used just in case when context is passed in.
|
||||||
encrypt_password = self._context.get("encrypt_password", False)
|
encrypt_password = self._context.get("encrypt_password", False)
|
||||||
elif self.encrypt == "auto" and self.encrypt_password:
|
elif self.encrypt == "auto" and self.encrypt_password:
|
||||||
obj = self.env[self.model].browse(res_id)
|
# access the report details with sudo() but evaluation context as sudo(False)
|
||||||
|
self_sudo = self.sudo()
|
||||||
|
|
||||||
|
Model = self.env[self_sudo.model]
|
||||||
|
record_ids = Model.browse(res_ids)
|
||||||
try:
|
try:
|
||||||
encrypt_password = safe_eval(self.encrypt_password, {"object": obj})
|
encrypt_password = safe_eval(
|
||||||
except Exception:
|
self.encrypt_password, {"object": record_ids}
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
raise ValidationError(
|
raise ValidationError(
|
||||||
_("Python code used for encryption password is invalid.\n%s")
|
_("Python code used for encryption password is invalid.\n%s")
|
||||||
% self.encrypt_password
|
% self.encrypt_password
|
||||||
)
|
) from e
|
||||||
return encrypt_password
|
return encrypt_password
|
||||||
|
|
||||||
def _encrypt_pdf(self, data, password):
|
@staticmethod
|
||||||
|
def _encrypt_pdf(data, password):
|
||||||
if not password:
|
if not password:
|
||||||
return data
|
return data
|
||||||
output_pdf = PdfFileWriter()
|
output_pdf = PdfFileWriter()
|
||||||
|
|||||||
@@ -0,0 +1,77 @@
|
|||||||
|
/** @odoo-module **/
|
||||||
|
|
||||||
|
import {Dialog} from "@web/core/dialog/dialog";
|
||||||
|
import {download} from "@web/core/network/download";
|
||||||
|
import {registry} from "@web/core/registry";
|
||||||
|
|
||||||
|
async function download_function(action, options, env) {
|
||||||
|
const type = action.report_type;
|
||||||
|
let url = `/report/${type}/${action.report_name}`;
|
||||||
|
const actionContext = action.context || {};
|
||||||
|
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)
|
||||||
|
const action_options = encodeURIComponent(JSON.stringify(action.data));
|
||||||
|
const context = encodeURIComponent(JSON.stringify(actionContext));
|
||||||
|
url += `?options=${action_options}&context=${context}`;
|
||||||
|
} else {
|
||||||
|
if (actionContext.active_ids) {
|
||||||
|
url += `/${actionContext.active_ids.join(",")}`;
|
||||||
|
}
|
||||||
|
if (type === "html") {
|
||||||
|
const context = encodeURIComponent(
|
||||||
|
JSON.stringify(env.services.user.context)
|
||||||
|
);
|
||||||
|
url += `?context=${context}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
env.services.ui.block();
|
||||||
|
try {
|
||||||
|
await download({
|
||||||
|
url: "/report/download",
|
||||||
|
data: {
|
||||||
|
data: JSON.stringify([url, action.report_type]),
|
||||||
|
context: JSON.stringify(env.services.user.context),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
env.services.ui.unblock();
|
||||||
|
}
|
||||||
|
const onClose = options.onClose;
|
||||||
|
if (action.close_on_report_download) {
|
||||||
|
return env.services.action.doAction(
|
||||||
|
{type: "ir.actions.act_window_close"},
|
||||||
|
{onClose}
|
||||||
|
);
|
||||||
|
} else if (onClose) {
|
||||||
|
onClose();
|
||||||
|
}
|
||||||
|
return Promise.resolve(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
class EncryptDialog extends Dialog {
|
||||||
|
onClick() {
|
||||||
|
const action = this.props.action;
|
||||||
|
action.context = _.extend({}, action.context, {
|
||||||
|
encrypt_password: this.el.find(".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";
|
||||||
|
|
||||||
|
registry
|
||||||
|
.category("ir.actions.report handlers")
|
||||||
|
.add("qweb-pdf-password", async function (action, options, env) {
|
||||||
|
if (action.encrypt === "manual" && action.report_type === "qweb-pdf") {
|
||||||
|
return env.services.dialog.add(EncryptDialog, {
|
||||||
|
action: action,
|
||||||
|
options: options,
|
||||||
|
env: env,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return Promise.resolve(false);
|
||||||
|
});
|
||||||
@@ -1,97 +0,0 @@
|
|||||||
// © 2017 Creu Blanca
|
|
||||||
// License AGPL-3.0 or later (https://www.gnuorg/licenses/agpl.html).
|
|
||||||
odoo.define("report_qweb_encrypt.Dialog", function (require) {
|
|
||||||
"use strict";
|
|
||||||
|
|
||||||
var ActionManager = require("web.ActionManager");
|
|
||||||
var Dialog = require("web.Dialog");
|
|
||||||
var core = require("web.core");
|
|
||||||
|
|
||||||
var _t = core._t;
|
|
||||||
|
|
||||||
var EncryptDialog = Dialog.extend({
|
|
||||||
events: _.extend({}, Dialog.prototype.events, {
|
|
||||||
change: "_onChange",
|
|
||||||
}),
|
|
||||||
_setValue: function () {
|
|
||||||
this.value = this.$el.find(".o_password").val();
|
|
||||||
},
|
|
||||||
_onChange: function () {
|
|
||||||
this._setValue();
|
|
||||||
},
|
|
||||||
});
|
|
||||||
EncryptDialog.askPassword = function (owner, action, action_options, options) {
|
|
||||||
var buttons = [
|
|
||||||
{
|
|
||||||
text: _t("Ok"),
|
|
||||||
classes: "btn-primary",
|
|
||||||
close: true,
|
|
||||||
click: function () {
|
|
||||||
var password = this.value || false;
|
|
||||||
owner._executeReportAction(action, action_options, password);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
text: _t("Cancel"),
|
|
||||||
close: true,
|
|
||||||
click: false,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
return new EncryptDialog(
|
|
||||||
owner,
|
|
||||||
_.extend(
|
|
||||||
{
|
|
||||||
size: "small",
|
|
||||||
buttons: buttons,
|
|
||||||
$content: $(
|
|
||||||
'<div><input class="o_password" type="password"/></div>'
|
|
||||||
),
|
|
||||||
title: _t("Encrypt"),
|
|
||||||
},
|
|
||||||
options
|
|
||||||
)
|
|
||||||
).open();
|
|
||||||
};
|
|
||||||
|
|
||||||
ActionManager.include({
|
|
||||||
_executeReportAction: function (action, options, password) {
|
|
||||||
if (
|
|
||||||
action.encrypt === "manual" &&
|
|
||||||
action.report_type === "qweb-pdf" &&
|
|
||||||
password === undefined
|
|
||||||
) {
|
|
||||||
EncryptDialog.askPassword(this, action, options);
|
|
||||||
return $.Deferred();
|
|
||||||
} else if (action.encrypt === "manual") {
|
|
||||||
action.context = _.extend({}, action.context, {
|
|
||||||
encrypt_password: password,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return this._super(action, options, password);
|
|
||||||
},
|
|
||||||
_makeReportUrls: function (action) {
|
|
||||||
var reportUrls = this._super.apply(this, arguments);
|
|
||||||
if (action.encrypt === "manual" && action.context.encrypt_password) {
|
|
||||||
if (
|
|
||||||
_.isUndefined(action.data) ||
|
|
||||||
_.isNull(action.data) ||
|
|
||||||
(_.isObject(action.data) && _.isEmpty(action.data))
|
|
||||||
) {
|
|
||||||
var serializedOptionsPath =
|
|
||||||
"?context=" +
|
|
||||||
encodeURIComponent(
|
|
||||||
JSON.stringify({
|
|
||||||
encrypt_password: action.context.encrypt_password,
|
|
||||||
})
|
|
||||||
);
|
|
||||||
reportUrls = _.mapObject(reportUrls, function (value) {
|
|
||||||
var val = value;
|
|
||||||
val += serializedOptionsPath;
|
|
||||||
return val;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return reportUrls;
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
13
report_qweb_encrypt/static/src/js/report/encrypt_dialog.xml
Normal file
13
report_qweb_encrypt/static/src/js/report/encrypt_dialog.xml
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<templates xml:space="preserve">
|
||||||
|
|
||||||
|
<t t-name="report_qweb_encrypt.EncryptDialogFooter" owl="1">
|
||||||
|
<button class="btn btn-primary" t-on-click="onClick">Ok</button>
|
||||||
|
<button class="btn" t-on-click="close">Cancel</button>
|
||||||
|
</t>
|
||||||
|
|
||||||
|
<t t-name="report_qweb_encrypt.EncryptDialogBody" owl="1">
|
||||||
|
<div><input class="o_password" type="password" /></div>
|
||||||
|
</t>
|
||||||
|
|
||||||
|
</templates>
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8" ?>
|
|
||||||
<odoo>
|
|
||||||
<!--
|
|
||||||
(Copyright) 2020 Creu Blanca
|
|
||||||
License AGPL-3.0 or later (https://www.gnuorg/licenses/agpl.html).
|
|
||||||
-->
|
|
||||||
<template id="assets_backend" inherit_id="web.assets_backend">
|
|
||||||
<xpath expr="." position="inside">
|
|
||||||
<script
|
|
||||||
type="text/javascript"
|
|
||||||
src="/report_qweb_encrypt/static/src/js/report/action_manager_report.js"
|
|
||||||
/>
|
|
||||||
</xpath>
|
|
||||||
|
|
||||||
</template>
|
|
||||||
</odoo>
|
|
||||||
@@ -9,7 +9,7 @@ class TestReportQwebEncrypt(HttpCase):
|
|||||||
ctx = {"force_report_rendering": True}
|
ctx = {"force_report_rendering": True}
|
||||||
report = self.env.ref("web.action_report_internalpreview")
|
report = self.env.ref("web.action_report_internalpreview")
|
||||||
report.encrypt = False
|
report.encrypt = False
|
||||||
pdf, _ = report.with_context(ctx)._render_qweb_pdf([1])
|
pdf, _ = report.with_context(**ctx)._render_qweb_pdf([1])
|
||||||
self.assertFalse(pdf.count(b"/Encrypt"))
|
self.assertFalse(pdf.count(b"/Encrypt"))
|
||||||
|
|
||||||
def test_report_qweb_auto_encrypt(self):
|
def test_report_qweb_auto_encrypt(self):
|
||||||
@@ -17,16 +17,33 @@ class TestReportQwebEncrypt(HttpCase):
|
|||||||
report = self.env.ref("web.action_report_internalpreview")
|
report = self.env.ref("web.action_report_internalpreview")
|
||||||
report.encrypt = "auto"
|
report.encrypt = "auto"
|
||||||
report.encrypt_password = False
|
report.encrypt_password = False
|
||||||
|
|
||||||
# If no encrypt_password, still not encrypted
|
# If no encrypt_password, still not encrypted
|
||||||
pdf, _ = report.with_context(ctx)._render_qweb_pdf([1])
|
pdf, _ = report.with_context(**ctx)._render_qweb_pdf([1])
|
||||||
self.assertFalse(pdf.count(b"/Encrypt"))
|
self.assertFalse(pdf.count(b"/Encrypt"))
|
||||||
|
|
||||||
# If invalid encrypt_password, show error
|
# If invalid encrypt_password, show error
|
||||||
report.encrypt_password = "invalid python syntax"
|
report.encrypt_password = "invalid python syntax"
|
||||||
with self.assertRaises(ValidationError):
|
with self.assertRaises(ValidationError):
|
||||||
pdf, _ = report.with_context(ctx)._render_qweb_pdf([1])
|
pdf, _ = report.with_context(**ctx)._render_qweb_pdf([1])
|
||||||
|
|
||||||
# Valid python string for password
|
# Valid python string for password
|
||||||
report.encrypt_password = "'secretcode'"
|
report.encrypt_password = "'secretcode'"
|
||||||
pdf, _ = report.with_context(ctx)._render_qweb_pdf([1])
|
pdf, _ = report.with_context(**ctx)._render_qweb_pdf([1])
|
||||||
|
self.assertTrue(pdf.count(b"/Encrypt"))
|
||||||
|
|
||||||
|
def test_report_qweb_manual_encrypt(self):
|
||||||
|
ctx = {"force_report_rendering": True}
|
||||||
|
report = self.env.ref("web.action_report_internalpreview")
|
||||||
|
report.encrypt = "manual"
|
||||||
|
|
||||||
|
# If no encrypt_password, still not encrypted
|
||||||
|
pdf, _ = report.with_context(**ctx)._render_qweb_pdf([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])
|
||||||
self.assertTrue(pdf.count(b"/Encrypt"))
|
self.assertTrue(pdf.count(b"/Encrypt"))
|
||||||
|
|
||||||
# TODO: test_report_qweb_manual_encrypt, require JS test?
|
# TODO: test_report_qweb_manual_encrypt, require JS test?
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
# generated from manifests external_dependencies
|
# generated from manifests external_dependencies
|
||||||
lxml
|
lxml
|
||||||
openpyxl
|
openpyxl
|
||||||
|
PyPDF2
|
||||||
xlrd
|
xlrd
|
||||||
xlsxwriter
|
xlsxwriter
|
||||||
|
|||||||
Reference in New Issue
Block a user