[MIG] report_qweb_encrypt: Migration to 15.0

This commit is contained in:
grzana12
2022-11-05 22:36:34 +01:00
parent cc30602e47
commit 048ddfd2b1
9 changed files with 151 additions and 144 deletions

View File

@@ -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);
});

View File

@@ -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;
},
});
});

View 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>