Skip to content

Commit b267b42

Browse files
committed
[IMP] usability_webhooks: call api external data with table
1 parent ff42aff commit b267b42

6 files changed

Lines changed: 178 additions & 0 deletions

File tree

usability_webhooks/__manifest__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414
"data/config_parameter.xml",
1515
"data/ir_cron.xml",
1616
"views/api_log.xml",
17+
"views/base_api_data_views.xml",
1718
],
1819
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
22

33
from . import api_log
4+
from . import base_api_data
5+
from . import common_base_api
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright 2025 Ecosoft Co., Ltd. (https://ecosoft.co.th)
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
3+
4+
from odoo import fields, models
5+
6+
7+
class BaseApiData(models.Model):
8+
_name = "base.api.data"
9+
_description = "Base API Data"
10+
11+
name = fields.Char(required=True, translate=True)
12+
active = fields.Boolean(default=True)
13+
description = fields.Char()
14+
model_id = fields.Many2one(
15+
comodel_name="ir.model",
16+
required=True,
17+
ondelete="cascade",
18+
)
19+
api_data_line = fields.One2many(
20+
comodel_name="base.api.data.line",
21+
inverse_name="api_data_id",
22+
)
23+
path_url = fields.Char()
24+
path_method = fields.Char()
25+
26+
27+
class BaseApiDataLine(models.Model):
28+
_name = "base.api.data.line"
29+
_description = "Base API Data Line"
30+
_order = "sequence, id"
31+
32+
api_data_id = fields.Many2one(
33+
comodel_name="base.api.data",
34+
required=True,
35+
string="API Data",
36+
ondelete="restrict",
37+
)
38+
sequence = fields.Integer(default=10)
39+
name = fields.Char(
40+
required=True,
41+
)
42+
field_type = fields.Selection(
43+
selection=[
44+
("string", "String"),
45+
("int", "Integer"),
46+
("float", "Float"),
47+
],
48+
string="Type",
49+
required=True,
50+
)
51+
is_dynamic = fields.Boolean(string="Dynamic", default=False)
52+
value = fields.Char(
53+
required=True,
54+
)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2025 Ecosoft Co., Ltd. (https://ecosoft.co.th)
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
3+
4+
from odoo import models
5+
from odoo.exceptions import UserError
6+
from odoo.tools.safe_eval import safe_eval
7+
8+
9+
class CommonBaseApi(models.AbstractModel):
10+
_name = "common.base.api"
11+
_description = "Common Base API"
12+
13+
def get_api_data(self, api_data):
14+
data_dict = {}
15+
for line in api_data.api_data_line:
16+
if line.is_dynamic:
17+
# Dynamic expression: evaluate with record context
18+
try:
19+
value = safe_eval(line.value, {"record": self})
20+
except Exception as e:
21+
raise UserError(
22+
self.env._(
23+
f"Error evaluating dynamic field {line.name}: {str(e)}"
24+
)
25+
) from e
26+
else:
27+
# Static value: keep as string or numeric
28+
if line.field_type == "float":
29+
try:
30+
value = float(line.value)
31+
except ValueError:
32+
raise UserError(
33+
self.env._(
34+
f"Field {line.name} must be float, got: {line.value}"
35+
)
36+
) from None
37+
elif line.field_type == "int":
38+
try:
39+
value = int(line.value)
40+
except ValueError:
41+
raise UserError(
42+
self.env._(
43+
f"Field {line.name} must be integer, got: {line.value}"
44+
)
45+
) from None
46+
else:
47+
value = line.value
48+
data_dict[line.name] = value
49+
return data_dict
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
22
access_api_log,access_api_log,model_api_log,base.group_user,1,1,1,1
3+
access_base_api_data_user,access_base_api_data_user,model_base_api_data,base.group_erp_manager,1,1,1,1
4+
access_base_api_data_line_user,access_base_api_data_line_user,model_base_api_data_line,base.group_erp_manager,1,1,1,1
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<odoo>
3+
<record id="base_api_data_list_view" model="ir.ui.view">
4+
<field name="name">base.api.data.list.view</field>
5+
<field name="model">base.api.data</field>
6+
<field name="arch" type="xml">
7+
<list>
8+
<field name="name" />
9+
<field name="model_id" />
10+
</list>
11+
</field>
12+
</record>
13+
<record id="base_api_data_form_view" model="ir.ui.view">
14+
<field name="name">base.api.data.form.view</field>
15+
<field name="model">base.api.data</field>
16+
<field name="arch" type="xml">
17+
<form>
18+
<sheet>
19+
<widget
20+
name="web_ribbon"
21+
title="Archived"
22+
bg_color="text-bg-danger"
23+
invisible="active"
24+
/>
25+
<div class="oe_title">
26+
<label class="oe_edit_only" for="name" string="API Data Name" />
27+
<h1>
28+
<field name="name" placeholder="API Data Name" />
29+
</h1>
30+
</div>
31+
<group>
32+
<group>
33+
<field name="model_id" />
34+
<field name="path_url" />
35+
<field name="path_method" />
36+
</group>
37+
<group>
38+
<field name="description" />
39+
</group>
40+
</group>
41+
<notebook>
42+
<page name="api_line" string="API Line">
43+
<field name="api_data_line">
44+
<list editable="bottom">
45+
<field name="sequence" widget="handle" />
46+
<field name="name" />
47+
<field name="field_type" />
48+
<field name="value" />
49+
<field name="is_dynamic" />
50+
</list>
51+
</field>
52+
</page>
53+
</notebook>
54+
</sheet>
55+
</form>
56+
</field>
57+
</record>
58+
59+
<record id="ir_api_exports_action" model="ir.actions.act_window">
60+
<field name="name">API Data Export</field>
61+
<field name="res_model">base.api.data</field>
62+
<field name="view_mode">list,form</field>
63+
</record>
64+
<menuitem
65+
id="ir_api_exports_menu"
66+
parent="base.next_id_2"
67+
name="API Data Export"
68+
action="ir_api_exports_action"
69+
/>
70+
</odoo>

0 commit comments

Comments
 (0)