Skip to content

Commit 9f05234

Browse files
DemchukMivs-cetmix
authored andcommitted
[ADD]web_refresh_from_backend: Refresh views from backend
Add a technical helper that allows triggering UI view reloads from the backend for selected users and records. This enables instant UI updates when backend data changes, for example refreshing CRM kanban and form views for salespeople as soon as a lead changes its stage. The helper reload_views is added to res.users and supports: - limiting reload by model - selecting specific view types - reloading only when specific record IDs are present This should be used carefully, as reloading form views may discard unsaved user changes. Task: 5066
1 parent 637c28a commit 9f05234

File tree

16 files changed

+1373
-0
lines changed

16 files changed

+1373
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../web_refresh_from_backend
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import setuptools
2+
3+
setuptools.setup(
4+
setup_requires=['setuptools-odoo'],
5+
odoo_addon=True,
6+
)
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
.. image:: https://odoo-community.org/readme-banner-image
2+
:target: https://odoo-community.org/get-involved?utm_source=readme
3+
:alt: Odoo Community Association
4+
5+
========================
6+
Web Refresh From Backend
7+
========================
8+
9+
..
10+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
11+
!! This file is generated by oca-gen-addon-readme !!
12+
!! changes will be overwritten. !!
13+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
14+
!! source digest: sha256:2513ead70de52d1dd678016ee4f5991596bf94a25cfbdf5b62a4f4ef85bec099
15+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
16+
17+
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
18+
:target: https://odoo-community.org/page/development-status
19+
:alt: Beta
20+
.. |badge2| image:: https://img.shields.io/badge/license-LGPL--3-blue.png
21+
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
22+
:alt: License: LGPL-3
23+
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fweb-lightgray.png?logo=github
24+
:target: https://github.com/OCA/web/tree/16.0/web_refresh_from_backend
25+
:alt: OCA/web
26+
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
27+
:target: https://translation.odoo-community.org/projects/web-16-0/web-16-0-web_refresh_from_backend
28+
:alt: Translate me on Weblate
29+
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
30+
:target: https://runboat.odoo-community.org/builds?repo=OCA/web&target_branch=16.0
31+
:alt: Try me on Runboat
32+
33+
|badge1| |badge2| |badge3| |badge4| |badge5|
34+
35+
This is a **technical module** that allows triggering a **UI reload**
36+
from the backend. It enables triggering the reload action for selected
37+
users and record IDs.
38+
39+
**NB:** this module refreshes views using direct actions instead of
40+
calling ``action_reload``. This is done to avoid possible glitches and
41+
is aligned with the same approach used in the
42+
`web_refreshed <https://github.com/OCA/web/tree/16.0/web_refresher>`__
43+
module.
44+
45+
--------------
46+
47+
🔧 Helper Function: ``reload_views``
48+
------------------------------------
49+
50+
A special helper function ``reload_views`` is added to the ``res.users``
51+
model.
52+
53+
**Arguments**
54+
~~~~~~~~~~~~~
55+
56+
+----------------+-------------------------+-------------------------+
57+
| Argument | Type | Description |
58+
+================+=========================+=========================+
59+
| **model** | ``Char`` | Model name, e.g. |
60+
| | | ``'res.partner'`` |
61+
+----------------+-------------------------+-------------------------+
62+
| **view_types** | ``List of Char`` | View types to reload, |
63+
| | *(optional)* | e.g. |
64+
| | | ``["form", "kanban"]``. |
65+
| | | Leave blank to reload |
66+
| | | all views. |
67+
+----------------+-------------------------+-------------------------+
68+
| **rec_ids** | ``List of Integer`` | The view will be |
69+
| | *(optional)* | reloaded only if a |
70+
| | | record with an ID from |
71+
| | | this list is present in |
72+
| | | the view. |
73+
+----------------+-------------------------+-------------------------+
74+
75+
--------------
76+
77+
⚠️ Important Notes
78+
------------------
79+
80+
Use this function **wisely**.
81+
82+
When reloading **form views**, be aware that if a user is currently
83+
editing a record, **their unsaved updates may be lost**.
84+
85+
**Table of contents**
86+
87+
.. contents::
88+
:local:
89+
90+
Usage
91+
=====
92+
93+
🧩 Example Usage
94+
----------------
95+
96+
Below is a code snippet showing how to use the ``reload_views`` helper
97+
function.
98+
99+
.. code:: python
100+
101+
# Reload the kanban and form views for all salespeople when an opportunity is won
102+
# Will reload views only if the current opportunity is being displayed
103+
104+
group_id = self.env.ref("sales_team.group_sale_salesman").id
105+
users_to_reload = self.env["res.users"].search([("groups_id", "in", [group_id])])
106+
users_to_reload.reload_views(
107+
model="crm.lead",
108+
view_types=["kanban", "form"],
109+
rec_ids=[self.id],
110+
)
111+
112+
Bug Tracker
113+
===========
114+
115+
Bugs are tracked on `GitHub Issues <https://github.com/OCA/web/issues>`_.
116+
In case of trouble, please check there if your issue has already been reported.
117+
If you spotted it first, help us to smash it by providing a detailed and welcomed
118+
`feedback <https://github.com/OCA/web/issues/new?body=module:%20web_refresh_from_backend%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
119+
120+
Do not contact contributors directly about support or help with technical issues.
121+
122+
Credits
123+
=======
124+
125+
Authors
126+
-------
127+
128+
* Cetmix
129+
130+
Contributors
131+
------------
132+
133+
- Cetmix
134+
135+
Maintainers
136+
-----------
137+
138+
This module is maintained by the OCA.
139+
140+
.. image:: https://odoo-community.org/logo.png
141+
:alt: Odoo Community Association
142+
:target: https://odoo-community.org
143+
144+
OCA, or the Odoo Community Association, is a nonprofit organization whose
145+
mission is to support the collaborative development of Odoo features and
146+
promote its widespread use.
147+
148+
This module is part of the `OCA/web <https://github.com/OCA/web/tree/16.0/web_refresh_from_backend>`_ project on GitHub.
149+
150+
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright 2025 Cetmix OÜ
2+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
3+
4+
from . import models
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2025 Cetmix OÜ
2+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
3+
4+
{
5+
"name": "Web Refresh From Backend",
6+
"summary": "Refresh views from backend",
7+
"version": "16.0.1.0.0",
8+
"category": "Web",
9+
"license": "LGPL-3",
10+
"author": "Cetmix, Odoo Community Association (OCA)",
11+
"website": "https://github.com/OCA/web",
12+
"depends": ["mail"],
13+
"assets": {
14+
"web.assets_backend": [
15+
"web_refresh_from_backend/static/src/views/list/list_controller_patch.esm.js",
16+
"web_refresh_from_backend/static/src/views/kanban/kanban_controller_patch.esm.js",
17+
"web_refresh_from_backend/static/src/views/form/form_controller_patch.esm.js",
18+
],
19+
},
20+
"installable": True,
21+
"auto_install": False,
22+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright 2025 Cetmix OÜ
2+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
3+
4+
from . import res_users
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2025 Cetmix OÜ
2+
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
3+
4+
from odoo import models
5+
6+
7+
class ResUsers(models.Model):
8+
_inherit = "res.users"
9+
10+
def reload_views(self, model, view_types=None, rec_ids=None):
11+
"""
12+
Trigger UI reload for selected users and record IDs.
13+
14+
This method allows to reload specific views from the backend.
15+
Be aware that when reloading form views, if a user is currently
16+
doing some updates, those updates may be lost.
17+
18+
:param model: str, Model name (e.g., 'res.partner')
19+
:param view_types: list of str, optional, View types to reload
20+
(e.g., ['form', 'kanban']). Leave blank to reload all views.
21+
:param rec_ids: list of int, optional, View will be reloaded only if a record
22+
with id from the list is present in the view.
23+
24+
Example usage:
25+
# Reload the kanban and form views for all salespeople when an opportunity is won
26+
# Will reload views only if the current opportunity is being displayed
27+
group_id = self.env.ref("sales_team.group_sale_salesman").id
28+
users_to_reload = self.env["res.users"].search(
29+
[("groups_id", "in", [group_id])]
30+
)
31+
users_to_reload.reload_views(
32+
model="crm.lead",
33+
view_types=["kanban", "form"],
34+
rec_ids=[self.id]
35+
)
36+
"""
37+
38+
# Prepare the message payload
39+
bus_message = {
40+
"model": model,
41+
"view_types": view_types or [],
42+
"rec_ids": rec_ids or [],
43+
}
44+
45+
# Send notification to each user's partner
46+
notifications = [
47+
[user.partner_id, "web.refresh_view", bus_message] for user in self
48+
]
49+
self.env["bus.bus"]._sendmany(notifications)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
* Cetmix
2+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
This is a **technical module** that allows triggering a **UI reload** from the backend.
3+
It enables triggering the reload action for selected users and record IDs.
4+
5+
**NB:** this module refreshes views using direct actions instead of calling `action_reload`.
6+
This is done to avoid possible glitches and is aligned with the same approach used in the [web_refreshed](https://github.com/OCA/web/tree/16.0/web_refresher) module.
7+
8+
---
9+
10+
## 🔧 Helper Function: `reload_views`
11+
12+
A special helper function `reload_views` is added to the `res.users` model.
13+
14+
### **Arguments**
15+
16+
| Argument | Type | Description |
17+
|-----------|------|-------------|
18+
| **model** | `Char` | Model name, e.g. `'res.partner'` |
19+
| **view_types** | `List of Char` *(optional)* | View types to reload, e.g. `["form", "kanban"]`. Leave blank to reload all views. |
20+
| **rec_ids** | `List of Integer` *(optional)* | The view will be reloaded only if a record with an ID from this list is present in the view. |
21+
22+
---
23+
24+
## ⚠️ Important Notes
25+
26+
Use this function **wisely**.
27+
28+
When reloading **form views**, be aware that if a user is currently editing a record,
29+
**their unsaved updates may be lost**.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## 🧩 Example Usage
2+
3+
Below is a code snippet showing how to use the `reload_views` helper function.
4+
5+
```python
6+
# Reload the kanban and form views for all salespeople when an opportunity is won
7+
# Will reload views only if the current opportunity is being displayed
8+
9+
group_id = self.env.ref("sales_team.group_sale_salesman").id
10+
users_to_reload = self.env["res.users"].search([("groups_id", "in", [group_id])])
11+
users_to_reload.reload_views(
12+
model="crm.lead",
13+
view_types=["kanban", "form"],
14+
rec_ids=[self.id],
15+
)

0 commit comments

Comments
 (0)