Hi,
It is possible to add a confirmation popup to Odoo's default import button using a custom module. Here's how you can achieve this by overriding the default behavior:
The general approach involves:
1- Inheriting the relevant view: Identify the view containing the import button and inherit it in your custom module.
2- Adding a JavaScript override: Use JavaScript to intercept the click event on the import button and display a confirmation dialog.
3- Modifying the import action: Based on the user's confirmation, either proceed with the default import action or cancel it.
Steps
1- Identify the View:
* The import button is typically part of a form or list view. You'll need to identify the exact view ID you want to modify. You can do this by activating developer mode in Odoo and inspecting the view.
2- Create a Custom Module:
* Create a new Odoo module (e.g., import_confirmation).
* In your module, create the following files:
* __manifest__.py: Module manifest file.
* static/src/js/import_confirmation.js: JavaScript file to handle the popup.
* views/views.xml: XML file to inherit and modify the view.
3- Module Manifest (__manifest__.py):
{
'name': 'Import Confirmation',
'version': '1.0',
'summary': 'Adds a confirmation popup to the import button.',
'description': """
This module adds a confirmation popup when a user clicks the Import button.
""",
'category': 'Tools',
'author': 'Your Name',
'website': 'www.example.com',
'depends': ['base'], # or the specific module where the import button is
'data': [
'views/views.xml',
],
'qweb': [
'static/src/xml/import_confirmation.xml',
],
'installable': True,
'application': False,
'auto_install': False,
}
4- XML View Inheritance (views/views.xml):
<odoo>
<data>
<record id="view_id_to_inherit" model="ir.ui.view">
<field name="name">your.model.form.inherit</field>
<field name="model">your.model</field>
<field name="inherit_id" ref="your_module.your_view_id"/>
<field name="arch" type="xml">
<xpath expr="//button[@name='action_import']" position="attributes">
<attribute name="class">oe_import_button</attribute>
</xpath>
</field>
</record>
</data>
</odoo>
5- JavaScript File (static/src/js/import_confirmation.js):
/** @odoo-module **/
import { registry } from '@web/core/registry';
import { useService } from "@web/core/utils/hooks";
const { onWillStart } = owl;
registry.category("client_action").add("import_confirmation.ImportConfirmationAction", {
Component: ImportConfirmationAction,
force: true,
});
function ImportConfirmationAction() {
const orm = useService("orm");
onWillStart(async () => {
$('.oe_import_button').click(function(event) {
event.preventDefault();
if (confirm("Data uploads are irreversible. Are you sure you want to confirm this data upload?")) {
// Continue with the default import action
$(this).unbind('click').click();
} else {
// Cancel the import
return false;
}
});
});
return {};
}
5- QWeb Template (static/src/xml/import_confirmation.xml):
* This file is needed to load the javascript file.
<?xml version="1.0" encoding="UTF-8"?>
<templates>
<t t-name="import_confirmation.ImportConfirmationAction" owl="1">
</t>
</templates>
* View Identification: Make sure you correctly identify the view ID and the import button's XPath.
* JavaScript Loading: Ensure your JavaScript file is correctly loaded by including it in the module's qweb list and defining the necessary template.
* Odoo Version Compatibility: Test the code thoroughly to ensure it works correctly with Odoo 18, as there might be slight differences in the framework.
By following these steps, you can add a confirmation popup to Odoo's default import button, providing an extra layer of protection against accidental data uploads.
Hope it helps
Hello,
Yes you can add confirmation dialog using
import {ConfirmationDialog } from "@web/core/confirmation_dialog/confirmation_dialog";
async deleteRecord(record) {
this.dialog.add(ConfirmationDialog, {
title: _t("Bye-bye, record!"),
body: deleteConfirmationMessage,
confirm: () => this.model.root.deleteRecords([record]),
confirmLabel: _t("Delete"),
cancel: () => {},
cancelLabel: _t("No, keep it"),
});
}
Above is just a reference..
async handleImport(isTest = true) in this JS method.