[IMP] report_async 14.0

This commit is contained in:
KKamaa
2022-04-13 18:40:55 +03:00
parent d717791082
commit 6c5cf35974
12 changed files with 452 additions and 131 deletions

View File

@@ -0,0 +1,124 @@
odoo.define("report_async.ActionMenus", function (require) {
"use strict";
const {patch} = require("web.utils");
const ActionMenus = require("web.ActionMenus");
const Dialog = require("web.Dialog");
const Core = require("web.core");
const Framework = require("web.framework");
const _t = Core._t;
const QWeb = Core.qweb;
function validate_email(email) {
const res = email.match(
/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
);
if (!res) {
return false;
}
return true;
}
// Patch _executeAction to use Dialog
patch(ActionMenus, "async _super report_async.ActionMenus", {
async _executeAction(action) {
const self = this;
const _super = this._super;
const args = arguments; // Dict action
const records = this.props.activeIds;
var $content = $(QWeb.render("ReportAsyncConfiguration", {}));
if (action.async_report && records.length >= action.async_no_records) {
const asyncDialog = new Dialog(self, {
title:
_t("Async Report Configuration ") +
"(" +
action.display_name +
")",
size: "medium",
buttons: [
{
text: _t("Print"),
classes: "btn-primary",
close: true,
click: function () {
const is_report_async = this.$(
"#async_report_checker"
).prop("checked");
const user_email = this.$("#async-user-email").val();
if (user_email !== "" && is_report_async) {
// Try basic email validation
if (validate_email(user_email)) {
if (
"report_type" in action &&
action.report_type === "qweb-pdf"
) {
Framework.unblockUI();
// Generate report async
self.rpc({
model: "report.async",
method: "print_document_async",
args: [records, action.report_name],
kwargs: {
to_email: user_email,
data: action.data || {},
context: action.context || {},
},
})
.then(() => {
const msg =
_t(
"Job started to generate report. Upon " +
"completion, mail sent to:"
) + user_email;
Dialog.alert(self, msg, {
title: _t("Report"),
});
})
.catch(() => {
const error = _t(
"Failed, error on job creation."
);
const title = _t("Report");
Dialog.alert(self, error, {
title: title,
});
});
} else {
// Default to normal approach to generate report
return _super.apply(self, args);
}
} else {
const msg = _t(
"Please check your email syntax and try again"
);
const title = _t("Email Validation Error");
Dialog.alert(self, msg, {title: title});
}
} else {
// Default to normal approach to generate report
return _super.apply(self, args);
}
},
},
{
text: _t("Discard"),
close: true,
},
],
$content: $content,
});
// Default current user mail
asyncDialog.open().opened(function () {
asyncDialog.$el
.find("#async-user-email")
.val(action.async_mail_recipient);
});
} else {
// Default to normal approach to generate report
return _super.apply(this, arguments);
}
},
});
});

View File

@@ -0,0 +1,138 @@
odoo.define("report_async.action_menus_tests", function (require) {
"use strict";
/* global QUnit*/
const ActionMenus = require("web.ActionMenus");
const Registry = require("web.Registry");
const testUtils = require("web.test_utils");
const cpHelpers = testUtils.controlPanel;
const {createComponent} = testUtils;
QUnit.module(
"report_async",
{
beforeEach() {
this.action = {
res_model: "res.users",
};
this.view = {
type: "form",
};
this.props = {
activeIds: [1, 2],
context: {},
items: {
print: [
{
type: "ir.actions.report",
async_report: true,
data: null,
async_no_records: 1,
async_mail_recipient: "admin@example.com",
report_type: "qweb-pdf",
report_name: "report_async.async_demo_report_view",
report_file: "report_async.async_demo_report_view",
name: "Async Report",
id: 1,
},
],
},
};
// Patch the registry of the action menus
this.actionMenusRegistry = ActionMenus.registry;
ActionMenus.registry = new Registry();
},
afterEach() {
ActionMenus.registry = this.actionMenusRegistry;
},
},
function () {
QUnit.test("execute print action", async function (assert) {
// No of assertion expected.
assert.expect(7);
const actionMenus = await createComponent(ActionMenus, {
env: {
action: this.action,
view: this.view,
},
intercepts: {
"do-action": () => assert.step("do-action"),
},
props: this.props,
async mockRPC(route, args) {
switch (route) {
case "/web/action/load": {
const expectedContext = {
active_id: 1,
active_ids: [1, 2],
active_model: "res.users",
};
assert.deepEqual(args.context, expectedContext);
assert.step("load-action");
return {context: {}, flags: {}};
}
default:
return this._super(...arguments);
}
},
});
await testUtils.nextTick();
await cpHelpers.toggleActionMenu(actionMenus, "Print");
await cpHelpers.toggleMenuItem(actionMenus, "Async Report");
// We should have dialog created and opened
assert.containsOnce(
$,
".form",
"Error dialog should be opened and showing async options"
);
// We should have checkbox checked
assert.ok(
$("#async_report_checker").prop("checked"),
"Checkbox should be checked auto"
);
// Email should be set as default
assert.equal(
$("#async-user-email").val(),
"admin@example.com",
"Email should be set and equal to default"
);
// Try to process async report to a queue and send mail
await testUtils.dom.click($("button.btn-primary"), {
allowInvisible: true,
});
await testUtils.nextTick();
// This should fail through error/alert dialog because we haven't
// defined the report well queue job, qweb etc. For a successful
// test see possible python tests.
assert.containsNone(
$,
$(".modal-content"),
"Error Dialog should have popup"
);
assert.ok(
$(".modal-title").text(),
"Report",
'Should have title "Report"'
);
assert.ok($(".modal-content").text().search("Failed"));
// Close error dialog
await testUtils.dom.click($(".modal-footer button.btn-primary"), {
allowInvisible: true,
});
await testUtils.nextTick();
// All dialogs should be closed
assert.containsNone($, $(".modal-dialog"), "Dialogs should be closed");
// Destroy the action menus
actionMenus.destroy();
});
}
);
});

View File

@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8" ?>
<templates>
<t t-name='ReportAsyncConfiguration'>
<div class="form">
<!-- Async Checkbox -->
<div class="form-group">
<div class="form-check">
<input
type="checkbox"
class="form-check-input"
checked="checked"
id="async_report_checker"
/>
<label class="form-check-label" for="async_report_checker">
Async Report
</label>
</div>
<small id="async-report-checker-help" class="form-text text-muted">
Checker enables async report to be created on the background
via queue job and sent to a below email address.
</small>
</div>
<!-- Email Input -->
<div class="form-group">
<label for="async-user-email">Email Address</label>
<input
type="email"
class="form-control"
id="async-user-email"
aria-describedby="emailHelp"
placeholder="admin@example.com"
/>
<small id="async-user-email-help" class="form-text text-muted">
Email will be used to send the async report after queue job
is done on the background
</small>
</div>
</div>
</t>
</templates>