Skip to content

Commit 5a2ec01

Browse files
committed
[FIX] override create an write functions
1 parent 9bc1eed commit 5a2ec01

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

spp_custom_fields_ui/models/custom_fields_ui.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Part of OpenSPP. See LICENSE file for full copyright and licensing details.
22

33
from odoo import api, fields, models
4+
from odoo.exceptions import UserError
45

56
FIELD_TYPES = [(key, key) for key in sorted(fields.Field.by_type)]
67

@@ -118,3 +119,33 @@ def set_compute(self):
118119
else:
119120
rec.ttype = "integer"
120121
rec.compute += "self.compute_count_and_set_indicator('%s', kinds, domain)" % name
122+
123+
def _should_reload_view(self):
124+
"""
125+
Check if the view should be reloaded after creating/updating custom fields.
126+
This is needed because custom fields modify the model registry.
127+
"""
128+
return any(record.target_type for record in self)
129+
130+
@api.model_create_multi
131+
def create(self, vals_list):
132+
"""Override create to trigger page reload after field creation"""
133+
records = super().create(vals_list)
134+
# Trigger reload only for custom fields with target_type in UI context
135+
if self.env.context.get("params") and records._should_reload_view():
136+
return {
137+
"type": "ir.actions.client",
138+
"tag": "reload",
139+
}
140+
return records
141+
142+
def write(self, vals):
143+
"""Override write to trigger page reload after field update"""
144+
result = super().write(vals)
145+
# Trigger reload only for custom fields with target_type in UI context
146+
if self.env.context.get("params") and self._should_reload_view():
147+
return {
148+
"type": "ir.actions.client",
149+
"tag": "reload",
150+
}
151+
return result

spp_custom_fields_ui/tests/test_custom_fields_ui.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,90 @@ def test_09_set_compute_error_on_type_change(self):
196196
"Changing the type of a field is not yet supported",
197197
):
198198
field.set_compute()
199+
200+
def test_10_create_with_ui_context_returns_reload_action(self):
201+
"""Test that create with UI context returns reload action"""
202+
# Simulate UI context with params
203+
context = {"params": {"model": "ir.model.fields", "view_type": "form"}}
204+
result = (
205+
self.field_model.with_context(context)
206+
.create(
207+
{
208+
"name": "x_cst_grp_reload_test",
209+
"model_id": self.model_id.id,
210+
"field_description": "Reload Test",
211+
"ttype": "char",
212+
"state": "manual",
213+
"target_type": "grp",
214+
"field_category": "cst",
215+
}
216+
)
217+
)
218+
219+
# When created in UI context with target_type, should return reload action
220+
self.assertIsInstance(result, dict)
221+
self.assertEqual(result.get("type"), "ir.actions.client")
222+
self.assertEqual(result.get("tag"), "reload")
223+
224+
def test_11_create_without_ui_context_returns_recordset(self):
225+
"""Test that create without UI context returns normal recordset"""
226+
# Create without params in context (programmatic call)
227+
result = self.field_model.create(
228+
{
229+
"name": "x_cst_grp_no_reload",
230+
"model_id": self.model_id.id,
231+
"field_description": "No Reload Test",
232+
"ttype": "char",
233+
"state": "manual",
234+
"target_type": "grp",
235+
"field_category": "cst",
236+
}
237+
)
238+
239+
# Should return recordset, not action dict
240+
self.assertEqual(result._name, "ir.model.fields")
241+
self.assertTrue(result.id)
242+
243+
def test_12_write_with_ui_context_returns_reload_action(self):
244+
"""Test that write with UI context returns reload action"""
245+
field = self.field_model.create(
246+
{
247+
"name": "x_cst_grp_write_test",
248+
"model_id": self.model_id.id,
249+
"field_description": "Write Test",
250+
"ttype": "char",
251+
"state": "manual",
252+
"target_type": "grp",
253+
"field_category": "cst",
254+
}
255+
)
256+
257+
# Update with UI context
258+
context = {"params": {"model": "ir.model.fields", "view_type": "form"}}
259+
result = field.with_context(context).write({"field_description": "Updated Description"})
260+
261+
# Should return reload action
262+
self.assertIsInstance(result, dict)
263+
self.assertEqual(result.get("type"), "ir.actions.client")
264+
self.assertEqual(result.get("tag"), "reload")
265+
266+
def test_13_write_without_ui_context_returns_boolean(self):
267+
"""Test that write without UI context returns boolean"""
268+
field = self.field_model.create(
269+
{
270+
"name": "x_cst_grp_write_no_reload",
271+
"model_id": self.model_id.id,
272+
"field_description": "Write No Reload",
273+
"ttype": "char",
274+
"state": "manual",
275+
"target_type": "grp",
276+
"field_category": "cst",
277+
}
278+
)
279+
280+
# Update without UI context (programmatic call)
281+
result = field.write({"field_description": "Updated Without Reload"})
282+
283+
# Should return boolean True
284+
self.assertTrue(result)
285+
self.assertIsInstance(result, bool)

0 commit comments

Comments
 (0)