-
-
Notifications
You must be signed in to change notification settings - Fork 2k
[18.0][ADD] web_backend_action: New Module #3368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
GabbasovDinar
wants to merge
1
commit into
OCA:18.0
Choose a base branch
from
GabbasovDinar:18.0-add-web_backend_action-module
base: 18.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,263 @@ | ||
| ================== | ||
| Web Backend Action | ||
| ================== | ||
|
|
||
| .. | ||
| !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
| !! This file is generated by oca-gen-addon-readme !! | ||
| !! changes will be overwritten. !! | ||
| !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
| !! source digest: sha256:2c74fe8141e08a0ee82f5bac841673ab92bb84ebeeba5172c98a9a7ac5731710 | ||
| !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
|
|
||
| .. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png | ||
| :target: https://odoo-community.org/page/development-status | ||
| :alt: Beta | ||
| .. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png | ||
| :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html | ||
| :alt: License: LGPL-3 | ||
| .. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fweb-lightgray.png?logo=github | ||
| :target: https://github.com/OCA/web/tree/18.0/web_backend_action | ||
| :alt: OCA/web | ||
| .. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png | ||
| :target: https://translation.odoo-community.org/projects/web-18-0/web-18-0-web_backend_action | ||
| :alt: Translate me on Weblate | ||
| .. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png | ||
| :target: https://runboat.odoo-community.org/builds?repo=OCA/web&target_branch=18.0 | ||
| :alt: Try me on Runboat | ||
|
|
||
| |badge1| |badge2| |badge3| |badge4| |badge5| | ||
|
|
||
| **Trigger UI actions from backend code in real time.** | ||
|
|
||
| This technical module allows you to send standard Odoo actions | ||
| (``ir.actions.*``) from the server to the user’s web client and execute | ||
| them live via the bus. Instead of only showing a toast notification, the | ||
| client actually runs the action when the conditions match. | ||
|
|
||
| **Typical use cases include**: | ||
|
|
||
| - Open a wizard automatically when a background job finishes (e.g. an | ||
| import summary or a follow-up confirmation dialog). | ||
| - Redirect the user to a specific list/form view after a workflow event | ||
| (approval, status change, external webhook, etc.). | ||
| - React in real time to server-side events by refreshing or changing the | ||
| current screen without manual user navigation. | ||
|
|
||
| Backend helpers are provided on ``res.users`` to send actions, | ||
| optionally tagged with a target model (``res_model``), a specific record | ||
| (``res_id``) and allowed view types (``view_types``). A small service in | ||
| the web client listens to the bus channel and only executes the action | ||
| when the current UI context (model / record / view type) matches the | ||
| hints sent from the server. | ||
|
|
||
| **Table of contents** | ||
|
|
||
| .. contents:: | ||
| :local: | ||
|
|
||
| Installation | ||
| ============ | ||
|
|
||
| This module is based on the Instant Messaging Bus. To work properly, the | ||
| server must be launched in gevent mode. | ||
|
|
||
| Usage | ||
| ===== | ||
|
|
||
| To trigger an action for the current user, call ``_send_action`` on | ||
| ``res.users``: | ||
|
|
||
| .. code:: python | ||
|
|
||
| action = { | ||
| "type": "ir.actions.act_window", | ||
| "name": "My Wizard", | ||
| "res_model": "my.wizard", | ||
| "view_mode": "form", | ||
| "target": "new", | ||
| "context": { | ||
| "default_some_field": 42, | ||
| }, | ||
| } | ||
|
|
||
| self.env.user._send_action(action) | ||
|
|
||
| This will send a cleaned version of the action to the web client of the | ||
| current user. | ||
|
|
||
| **Filter by model (``res_model``)** | ||
|
|
||
| If you want the action to run when the user is currently working with a | ||
| specific model. You can pass a ``res_model`` hint: | ||
|
|
||
| .. code:: python | ||
|
|
||
| action = { | ||
| "type": "ir.actions.act_window", | ||
| "name": "Order Helper", | ||
| "res_model": "sale.order.helper.wizard", | ||
| "view_mode": "form", | ||
| "target": "new", | ||
| } | ||
|
|
||
| self.env.user._send_action( | ||
| action, | ||
| res_model="sale.order", | ||
| ) | ||
|
|
||
| **Filter by record (``res_id``)** | ||
|
|
||
| For form views, it is often useful to restrict execution to a specific | ||
| record (for example, only when the user is looking at a particular | ||
| partner or document). You can pass a res_id value: | ||
|
|
||
| .. code:: python | ||
|
|
||
| action = { | ||
| "type": "ir.actions.client", | ||
| "tag": "soft_reload", | ||
| } | ||
|
|
||
| # Only execute when the user is viewing this specific partner record | ||
| self.env.user._send_action( | ||
| action, | ||
| res_model="res.partner", | ||
| res_id=self.id, | ||
| view_types=["form"], | ||
| ) | ||
|
|
||
| **Filter by view type (view_types)** | ||
|
|
||
| You can also restrict execution to specific view types, for example only | ||
| in form view or only in list view. Use the ``view_types`` parameter: | ||
|
|
||
| .. code:: python | ||
|
|
||
| action = { | ||
| "type": "ir.actions.act_window", | ||
| "name": "Mass Update", | ||
| "res_model": "stock.quant", | ||
| "view_mode": "form", | ||
| "target": "new", | ||
| } | ||
|
|
||
| self.env.user._send_action( | ||
| action, | ||
| res_model="stock.quant", | ||
| view_types=["list"], # only when the user is on a list view | ||
| ) | ||
|
|
||
| If ``view_types`` is omitted or an empty list, no restriction by view | ||
| type is applied. | ||
|
|
||
| **Example patterns** | ||
|
|
||
| - Update UI after a background job | ||
|
|
||
| .. code:: python | ||
|
|
||
| def _cron_enrich_contact(self): | ||
| # ... heavy enrich logic ... | ||
|
|
||
| action = { | ||
| "type": "ir.actions.client", | ||
| "tag": "soft_reload", | ||
| } | ||
|
|
||
| # Ask the client to soft-reload when viewing this contact | ||
| self.env.user._send_action( | ||
| action, | ||
| res_model="res.partner", | ||
| res_id=self.id, | ||
| view_types=["form"], | ||
| ) | ||
|
|
||
| - Notify after creating a record | ||
|
|
||
| .. code:: python | ||
|
|
||
| def create(self, vals_list): | ||
| res = super().create(vals_list) | ||
| action = { | ||
| "type": "ir.actions.client", | ||
| "tag": "display_notification", | ||
| "params": { | ||
| "type": "info", | ||
| "title": _("Info"), | ||
| "message": _("Record was created successfully."), | ||
| "next": {"type": "ir.actions.act_window_close"}, | ||
| }, | ||
| } | ||
| # Notify only when the user is on the partner form | ||
| self.env.user._send_action( | ||
| action, | ||
| res_model="res.partner", | ||
| res_id=self.id, | ||
| view_types=["form"], | ||
| ) | ||
|
|
||
| - Ask the user to fill in missing information | ||
|
|
||
| .. code:: python | ||
|
|
||
| def _cron_check_employees_data(self): | ||
| # ... find employees with incomplete data ... | ||
| employees = self.search([("some_field", "=", False)]) | ||
|
|
||
| for employee in employees: | ||
| action = { | ||
| "type": "ir.actions.act_window", | ||
| "name": "Complete Employee Data", | ||
| "res_model": "hr.employee.wizard", | ||
| "view_mode": "form", | ||
| "target": "new", | ||
| "context": { | ||
| "default_employee_id": employee.id, | ||
| }, | ||
| } | ||
|
|
||
| # Ask the responsible user to complete data | ||
| employee.user_id._send_action( | ||
| action, | ||
| ) | ||
|
|
||
| Bug Tracker | ||
| =========== | ||
|
|
||
| Bugs are tracked on `GitHub Issues <https://github.com/OCA/web/issues>`_. | ||
| In case of trouble, please check there if your issue has already been reported. | ||
| If you spotted it first, help us to smash it by providing a detailed and welcomed | ||
| `feedback <https://github.com/OCA/web/issues/new?body=module:%20web_backend_action%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_. | ||
|
|
||
| Do not contact contributors directly about support or help with technical issues. | ||
|
|
||
| Credits | ||
| ======= | ||
|
|
||
| Authors | ||
| ------- | ||
|
|
||
| * Dinar Gabbasov | ||
|
|
||
| Contributors | ||
| ------------ | ||
|
|
||
| - Dinar Gabbasov <[email protected]> | ||
|
|
||
| Maintainers | ||
| ----------- | ||
|
|
||
| This module is maintained by the OCA. | ||
|
|
||
| .. image:: https://odoo-community.org/logo.png | ||
| :alt: Odoo Community Association | ||
| :target: https://odoo-community.org | ||
|
|
||
| OCA, or the Odoo Community Association, is a nonprofit organization whose | ||
| mission is to support the collaborative development of Odoo features and | ||
| promote its widespread use. | ||
|
|
||
| This module is part of the `OCA/web <https://github.com/OCA/web/tree/18.0/web_backend_action>`_ project on GitHub. | ||
|
|
||
| You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Copyright 2025 Dinar Gabbasov <[email protected]> | ||
| # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). | ||
|
|
||
| from . import models |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # Copyright 2025 Dinar Gabbasov <[email protected]> | ||
| # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). | ||
|
|
||
| { | ||
| "name": "Web Backend Action", | ||
| "summary": "Send actions from the backend to the UI via the bus " | ||
| "and execute them immediately", | ||
| "version": "18.0.1.0.0", | ||
| "category": "Extra Tools", | ||
| "license": "LGPL-3", | ||
| "author": "Odoo Community Association (OCA), Dinar Gabbasov", | ||
| "website": "https://github.com/OCA/web", | ||
| "depends": ["web", "bus"], | ||
| "data": [], | ||
| "assets": { | ||
| "web.assets_backend": [ | ||
| "web_backend_action/static/src/js/services/*.js", | ||
| ], | ||
| }, | ||
| "installable": True, | ||
| "application": False, | ||
| "auto_install": False, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Copyright 2025 Dinar Gabbasov <[email protected]> | ||
| # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). | ||
|
|
||
| from . import res_users |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # Copyright 2025 Dinar Gabbasov <[email protected]> | ||
| # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). | ||
|
|
||
| from odoo import models | ||
|
|
||
| from odoo.addons.web.controllers.utils import clean_action | ||
|
|
||
|
|
||
| class ResUsers(models.Model): | ||
| _inherit = "res.users" | ||
|
|
||
| def _send_action(self, action, res_model=None, res_id=None, view_types=None): | ||
| """ | ||
| Send an action to each user to webclient. | ||
|
|
||
| :param dict action: Standard Odoo action definition to be executed in | ||
| the web client. | ||
| :param str res_model: Optional model name tag. The webclient can compare | ||
| this value with the current controller's ``resModel`` and only run | ||
| the action when they match. | ||
| :param int res_id: Optional record ID. The web client can compare this | ||
| value with the current controller's ``resId`` and only run the | ||
| action when they match. | ||
| :param list[str] view_types: Optional list of allowed view types | ||
| (e.g. ``["form", "list", "kanban"]``). The webclient can use this | ||
| list to restrict execution to specific view types. If ``None`` or | ||
| empty, no view-type restriction is applied. | ||
| :return bool: ``True`` if a message was sent, or ``None`` when no | ||
| action was provided. | ||
| """ | ||
| if not action: | ||
| return | ||
|
|
||
| clean = clean_action(action, self.env) | ||
| message = { | ||
| "action": clean, | ||
| "res_model": res_model, | ||
| "res_id": res_id, | ||
| "view_types": view_types or [], | ||
| } | ||
| for user in self: | ||
| user._bus_send("web.backend_action", message) | ||
|
|
||
| return True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| [build-system] | ||
| requires = ["whool"] | ||
| build-backend = "whool.buildapi" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| - Dinar Gabbasov \<<[email protected]>\> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| **Trigger UI actions from backend code in real time.** | ||
|
|
||
| This technical module allows you to send standard Odoo actions | ||
| (`ir.actions.*`) from the server to the user’s web client and execute | ||
| them live via the bus. Instead of only showing a toast | ||
| notification, the client actually runs the action when the | ||
| conditions match. | ||
|
|
||
| **Typical use cases include**: | ||
|
|
||
| - Open a wizard automatically when a background job finishes | ||
| (e.g. an import summary or a follow-up confirmation dialog). | ||
| - Redirect the user to a specific list/form view after a workflow event | ||
| (approval, status change, external webhook, etc.). | ||
| - React in real time to server-side events by refreshing or changing | ||
| the current screen without manual user navigation. | ||
|
|
||
| Backend helpers are provided on `res.users` to send actions, optionally | ||
| tagged with a target model (`res_model`), a specific record (`res_id`) | ||
| and allowed view types (`view_types`). A small service in the web client | ||
| listens to the bus channel and only executes the action when the current | ||
| UI context (model / record / view type) matches the hints sent from the | ||
| server. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| This module is based on the Instant Messaging Bus. To work properly, the | ||
| server must be launched in gevent mode. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'web' already depends on 'bus', no need to add it explicitly.