-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOdoo JSON-RPC.lua
More file actions
623 lines (537 loc) · 23.1 KB
/
Copy pathOdoo JSON-RPC.lua
File metadata and controls
623 lines (537 loc) · 23.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
---------------------------------------------------------------------------------------------------------
-- Odoo JSON-RPC Custom Data Provider for EasyCatalog
--
-- Read-only connector for Odoo models through execute_kw/search_read.
-- Supports pagination, domains, ordering, context, field aliases and Odoo relation/list flattening.
-- Credentials and customer-specific settings are deliberately not included in this distributable file.
---------------------------------------------------------------------------------------------------------
local PROVIDER_NAME = "Odoo JSON-RPC"
local PROVIDER_VERSION = "1.0.0"
local actionConfig = nil
local configdialog = nil
---------------------------------------------------------------------------------------------------------
-- Helpers
---------------------------------------------------------------------------------------------------------
local function trim(value)
if value == nil then return "" end
return tostring(value):match("^%s*(.-)%s*$")
end
-- Lua prefixes a raised error with "chunk:line:", which is noise in a user alert.
local function clean_error(message)
return (tostring(message):gsub("^.-:%d+:%s*", ""))
end
local function copy_defaults(config)
local defaults = {
name = "Odoo",
endpoint = "",
database = "",
user_id = "",
login = "",
api_key = "",
model = "product.product",
key_field = "id",
fields = "id, name",
domain = "[]",
order = "id asc",
context = "",
page_size = "500",
aliases = "",
many2one_mode = "Display value",
array_mode = "Line breaks",
image_fields = "",
initialized = false
}
config = config or {}
for key, value in pairs(defaults) do
if config[key] == nil then config[key] = value end
end
return config
end
local function split_names(value)
local result = {}
local seen = {}
for raw_name in trim(value):gmatch("[^,%s;]+") do
local name = trim(raw_name)
if name ~= "" and not seen[name] then
result[#result + 1] = name
seen[name] = true
end
end
return result
end
local function parse_aliases(value)
local aliases = {}
local targets = {}
for entry in trim(value):gmatch("[^,;\r\n]+") do
local source, target = entry:match("^%s*([^=%s]+)%s*=%s*([^=%s]+)%s*$")
if source == nil or target == nil then
error("Invalid field alias '" .. trim(entry) .. "'. Use source=target.")
end
if aliases[source] and aliases[source] ~= target then
error("The field '" .. source .. "' has more than one alias.")
end
if targets[target] and targets[target] ~= source then
error("The alias '" .. target .. "' is used for more than one field.")
end
aliases[source] = target
targets[target] = source
end
return aliases
end
local function json_string(value)
return cjson.encode(tostring(value or ""))
end
local function validate_json_array(value, label)
value = trim(value)
if value == "" then value = "[]" end
if value:sub(1, 1) ~= "[" or value:sub(-1) ~= "]" then
error(label .. " must be a JSON array.")
end
local ok = pcall(cjson.decode, value)
if not ok then error(label .. " contains invalid JSON.") end
return value
end
local function validate_json_object(value, label)
value = trim(value)
if value == "" then return "" end
if value:sub(1, 1) ~= "{" or value:sub(-1) ~= "}" then
error(label .. " must be a JSON object.")
end
local ok = pcall(cjson.decode, value)
if not ok then error(label .. " contains invalid JSON.") end
return value
end
local function safe_error_message(response)
if type(response) ~= "table" or type(response.error) ~= "table" then
return "Unknown Odoo API error."
end
local data = response.error.data
if type(data) == "table" then
if trim(data.message) ~= "" then return trim(data.message) end
if trim(data.name) ~= "" then return trim(data.name) end
end
if trim(response.error.message) ~= "" then return trim(response.error.message) end
return "Unknown Odoo API error."
end
local function rpc_call(config, service, method, args_json)
local body = "{" ..
"\"jsonrpc\":\"2.0\"," ..
"\"method\":\"call\"," ..
"\"id\":1," ..
"\"params\":{" ..
"\"service\":" .. json_string(service) .. "," ..
"\"method\":" .. json_string(method) .. "," ..
"\"args\":" .. args_json ..
"}}"
local headers = { ["Content-Type"] = "application/json" }
local code, response_body = HTTP.postwithheaders(config.endpoint, body, headers)
if code == 2 then return nil, "cancelled" end
if tonumber(code) == nil or tonumber(code) < 200 or tonumber(code) >= 300 then
error("Odoo request failed (HTTP " .. tostring(code) .. ").")
end
local ok, response = pcall(cjson.decode, response_body or "")
if not ok or type(response) ~= "table" then
error("Odoo returned an invalid JSON response.")
end
if response.error ~= nil then
error("Odoo API: " .. safe_error_message(response))
end
if response.result == nil then
error("Odoo response does not contain a result.")
end
return response.result, nil
end
local function resolve_user_id(config)
local uid = tonumber(trim(config.user_id))
if uid ~= nil then return math.floor(uid) end
local login = trim(config.login)
if login == "" then
error("Enter either a numeric User ID or a Login.")
end
local args_json = "[" ..
json_string(config.database) .. "," ..
json_string(login) .. "," ..
json_string(config.api_key) .. ",{}]"
local result, state = rpc_call(config, "common", "authenticate", args_json)
if state == "cancelled" then return nil, state end
uid = tonumber(result)
if uid == nil or uid <= 0 then
error("Odoo authentication failed. Check database, login and API key/password.")
end
return math.floor(uid), nil
end
local function build_search_read_args(config, uid, offset, limit)
local domain_json = validate_json_array(config.domain, "Domain")
local context_json = validate_json_object(config.context, "Context")
local requested_fields = split_names(config.fields)
local has_explicit_fields = #requested_fields > 0
local field_seen = {}
for _, name in ipairs(requested_fields) do field_seen[name] = true end
if has_explicit_fields and not field_seen[config.key_field] then
requested_fields[#requested_fields + 1] = config.key_field
end
local keyword_args = {}
if has_explicit_fields then
keyword_args[#keyword_args + 1] = "\"fields\":" .. cjson.encode(requested_fields)
end
keyword_args[#keyword_args + 1] = "\"limit\":" .. tostring(limit)
keyword_args[#keyword_args + 1] = "\"offset\":" .. tostring(offset)
if trim(config.order) ~= "" then
keyword_args[#keyword_args + 1] = "\"order\":" .. json_string(trim(config.order))
end
if context_json ~= "" then
keyword_args[#keyword_args + 1] = "\"context\":" .. context_json
end
return "[" ..
json_string(config.database) .. "," ..
tostring(uid) .. "," ..
json_string(config.api_key) .. "," ..
json_string(config.model) .. "," ..
"\"search_read\"," ..
"[" .. domain_json .. "]," ..
"{" .. table.concat(keyword_args, ",") .. "}]"
end
local function fetch_page(config, uid, offset, limit)
local args_json = build_search_read_args(config, uid, offset, limit)
local result, state = rpc_call(config, "object", "execute_kw", args_json)
if state == "cancelled" then return nil, state end
if type(result) ~= "table" then
error("Odoo search_read did not return a list of records.")
end
return result, nil
end
local function is_array(value)
if type(value) ~= "table" then return false end
local count = 0
local max_index = 0
for key, _ in pairs(value) do
if type(key) ~= "number" or key < 1 or key % 1 ~= 0 then return false end
count = count + 1
if key > max_index then max_index = key end
end
return count == max_index
end
local function scalar_to_string(value)
if value == nil or value == cjson.null then return "" end
if type(value) == "boolean" then return value and "true" or "false" end
return tostring(value)
end
local function normalize_value(value, many2one_mode, array_mode)
if type(value) ~= "table" then return scalar_to_string(value) end
if not is_array(value) then return cjson.encode(value) end
if #value == 0 then return "" end
local is_many2one = #value == 2 and type(value[1]) == "number" and
(type(value[2]) == "string" or type(value[2]) == "boolean")
if is_many2one then
if many2one_mode == "ID" then return scalar_to_string(value[1]) end
if many2one_mode == "JSON" then return cjson.encode(value) end
return scalar_to_string(value[2])
end
if array_mode == "JSON" then return cjson.encode(value) end
local parts = {}
for index = 1, #value do
local item = value[index]
if type(item) == "table" then
parts[#parts + 1] = cjson.encode(item)
else
parts[#parts + 1] = scalar_to_string(item)
end
end
local separator = array_mode == "Comma separated" and ", " or "\r"
return table.concat(parts, separator)
end
local function validate_config(config)
config.endpoint = trim(config.endpoint):gsub("/+$", "")
config.database = trim(config.database)
config.user_id = trim(config.user_id)
config.login = trim(config.login)
config.model = trim(config.model)
config.key_field = trim(config.key_field)
config.page_size = trim(config.page_size)
if trim(config.name) == "" then return "Name", "Enter a data source name." end
if config.endpoint == "" then return "Endpoint", "Enter the Odoo JSON-RPC URL." end
if not config.endpoint:match("^https://") and not config.endpoint:match("^http://localhost") and not config.endpoint:match("^http://127%.0%.0%.1") then
return "Endpoint", "Use HTTPS for remote Odoo connections."
end
if config.database == "" then return "Database", "Enter the Odoo database name." end
if trim(config.api_key) == "" then return "APIKey", "Enter an API key or password." end
if config.user_id == "" and config.login == "" then return "UserID", "Enter a User ID or Login." end
if config.user_id ~= "" and (tonumber(config.user_id) == nil or tonumber(config.user_id) <= 0) then
return "UserID", "User ID must be a positive number, or leave it blank and enter Login."
end
if config.model == "" then return "Model", "Enter an Odoo model." end
if config.key_field == "" then return "KeyField", "Enter a unique key field." end
local page_size = tonumber(config.page_size)
if page_size == nil or page_size < 1 or page_size > 10000 or page_size % 1 ~= 0 then
return "PageSize", "Page size must be a whole number from 1 to 10000."
end
-- Validate one at a time, so the reported widget is the field that is wrong.
local ok, message = pcall(validate_json_array, config.domain, "Domain")
if not ok then return "Domain", clean_error(message) end
ok, message = pcall(validate_json_object, config.context, "Context")
if not ok then return "Context", clean_error(message) end
ok, message = pcall(parse_aliases, config.aliases)
if not ok then return "Aliases", clean_error(message) end
return "", ""
end
---------------------------------------------------------------------------------------------------------
-- EasyCatalog lifecycle and configuration UI
---------------------------------------------------------------------------------------------------------
function Initialize()
return copy_defaults({})
end
local function read_dialog(config, dialog)
config.name = dialog:getwidget("Name").content
config.endpoint = dialog:getwidget("Endpoint").content
config.database = dialog:getwidget("Database").content
config.user_id = dialog:getwidget("UserID").content
config.login = dialog:getwidget("Login").content
config.api_key = dialog:getwidget("APIKey").content
config.model = dialog:getwidget("Model").content
config.key_field = dialog:getwidget("KeyField").content
config.fields = dialog:getwidget("Fields").content
config.domain = dialog:getwidget("Domain").content
config.order = dialog:getwidget("Order").content
config.context = dialog:getwidget("Context").content
config.page_size = dialog:getwidget("PageSize").content
config.aliases = dialog:getwidget("Aliases").content
config.many2one_mode = dialog:getwidget("Many2oneMode").selectedtext
config.array_mode = dialog:getwidget("ArrayMode").selectedtext
config.image_fields = dialog:getwidget("ImageFields").content
return config
end
function ApplyUI(dialog, config)
if config == nil then return nil end
read_dialog(config, dialog)
local widget, message = validate_config(config)
if widget ~= "" then
DIALOG.alert(message)
return widget
end
config.initialized = true
return config
end
function TestOdooConnection()
if configdialog == nil or actionConfig == nil then return end
local test_config = copy_defaults({})
read_dialog(test_config, configdialog)
local widget, message = validate_config(test_config)
if widget ~= "" then
DIALOG.alert(message)
return
end
local ok, result = pcall(function()
local uid, state = resolve_user_id(test_config)
if state == "cancelled" then return "Request cancelled." end
local records, page_state = fetch_page(test_config, uid, 0, 1)
if page_state == "cancelled" then return "Request cancelled." end
return "Connection successful. Model '" .. test_config.model .. "' returned " .. tostring(#records) .. " test record(s)."
end)
if ok then DIALOG.alert(result) else DIALOG.alert(tostring(result)) end
end
function ConfigureDataSource(config, template)
config = copy_defaults(config)
actionConfig = config
local enable_name = template or config.initialized == false
local title = enable_name and "New Odoo JSON-RPC Data Source" or "Odoo JSON-RPC Data Source"
configdialog = DIALOG.new({ title = title, validate = "ApplyUI" })
local label_left = 20
local label_right = 135
local field_left = 145
local field_right = 650
local top = 18
local spacing = 29
local widgets = {}
local function add_edit(label, id, value, password)
widgets[#widgets + 1] = { type = "statictext", title = label, align = "right", left = label_left, top = top, right = label_right, height = 20 }
widgets[#widgets + 1] = { type = password and "editboxpassword" or "editbox", id = id, left = field_left, top = top, right = field_right, height = 22, content = value or "", enable = id ~= "Name" or enable_name }
top = top + spacing
end
add_edit("Name:", "Name", config.name)
add_edit("JSON-RPC URL:", "Endpoint", config.endpoint)
add_edit("Database:", "Database", config.database)
add_edit("User ID (optional):", "UserID", config.user_id)
add_edit("Login (optional):", "Login", config.login)
add_edit("API key / password:", "APIKey", config.api_key, true)
add_edit("Model:", "Model", config.model)
add_edit("Unique key field:", "KeyField", config.key_field)
add_edit("Fields (comma list):", "Fields", config.fields)
add_edit("Domain (JSON):", "Domain", config.domain)
add_edit("Order:", "Order", config.order)
add_edit("Context (JSON):", "Context", config.context)
add_edit("Page size:", "PageSize", config.page_size)
add_edit("Aliases (from=to):", "Aliases", config.aliases)
widgets[#widgets + 1] = { type = "statictext", title = "Many2one fields:", align = "right", left = label_left, top = top, right = label_right, height = 20 }
widgets[#widgets + 1] = { type = "menu", id = "Many2oneMode", left = field_left, top = top, right = 345, height = 22, menuitems = { "Display value", "ID", "JSON" }, selectedtext = config.many2one_mode }
top = top + spacing
widgets[#widgets + 1] = { type = "statictext", title = "Other arrays:", align = "right", left = label_left, top = top, right = label_right, height = 20 }
widgets[#widgets + 1] = { type = "menu", id = "ArrayMode", left = field_left, top = top, right = 345, height = 22, menuitems = { "Line breaks", "Comma separated", "JSON" }, selectedtext = config.array_mode }
top = top + spacing
add_edit("Image URL fields:", "ImageFields", config.image_fields)
widgets[#widgets + 1] = { type = "button", id = "Test", title = "Test", left = field_left, top = top + 4, width = 80, height = 24, onchange = TestOdooConnection }
widgets[#widgets + 1] = { type = "cancelbutton", id = "Cancel", title = "Cancel", left = field_right - 190, top = top + 4, width = 85, height = 24 }
widgets[#widgets + 1] = { type = "okbutton", id = "OK", title = "OK", left = field_right - 90, top = top + 4, width = 85, height = 24 }
widgets[#widgets + 1] = { type = "statictext", title = " ", left = 20, top = top + 38, right = field_right, height = 2 }
configdialog:addwidget(widgets)
if configdialog:open() then
read_dialog(config, configdialog)
config.initialized = true
return config
end
end
function ConfigureUITemplate(config)
return ConfigureDataSource(config, true)
end
function ConfigureUI(config)
return ConfigureDataSource(config, false)
end
---------------------------------------------------------------------------------------------------------
-- Synchronization
---------------------------------------------------------------------------------------------------------
function Synchronize(config, datasource)
config = copy_defaults(config)
local widget, message = validate_config(config)
if widget ~= "" then error(message) end
local aliases = parse_aliases(config.aliases)
local image_fields = {}
for _, field_name in ipairs(split_names(config.image_fields)) do image_fields[field_name] = true end
local requested_fields = split_names(config.fields)
local page_size = tonumber(config.page_size)
local uid, auth_state = resolve_user_id(config)
if auth_state == "cancelled" then return nil end
local key_output = aliases[config.key_field] or config.key_field
local field_order = {}
local field_seen = {}
local source_for_output = {}
local function register_field(source)
local output = aliases[source] or source
if output == key_output then
if source ~= config.key_field then
error("Field alias collision on key field '" .. key_output .. "'.")
end
return
end
if source_for_output[output] and source_for_output[output] ~= source then
error("More than one Odoo field maps to EasyCatalog field '" .. output .. "'.")
end
source_for_output[output] = source
if not field_seen[output] then
field_seen[output] = true
field_order[#field_order + 1] = output
end
end
for _, source in ipairs(requested_fields) do register_field(source) end
if config.key_field ~= "id" then register_field("id") end
local records = {}
local keys_seen = {}
local progress = PROGRESSBAR.new()
progress:show("Downloading from Odoo", 100, true)
local offset = 0
local page = 1
local previous_signature = nil
while true do
-- update returns true once the user clicks Cancel. Ignoring it would leave
-- the button on screen doing nothing while the download carries on.
local cancelled = progress:update("Downloading page " .. tostring(page) .. " (" .. tostring(#records) .. " records)")
if cancelled == true then return nil end
local page_records, page_state = fetch_page(config, uid, offset, page_size)
if page_state == "cancelled" then return nil end
if #page_records > 0 then
local first_id = page_records[1] and page_records[1].id or ""
local last_id = page_records[#page_records] and page_records[#page_records].id or ""
local signature = tostring(#page_records) .. ":" .. tostring(first_id) .. ":" .. tostring(last_id)
if previous_signature == signature then
error("Odoo pagination did not advance. Check whether this endpoint supports offset and limit.")
end
previous_signature = signature
end
for _, source_record in ipairs(page_records) do
if type(source_record) ~= "table" then error("Odoo returned an invalid record.") end
-- Odoo returns the boolean false for an empty field. Test the raw value:
-- normalising first would turn that into the text "false", which is
-- indistinguishable from a field legitimately containing "false".
local raw_key = source_record[config.key_field]
if raw_key == nil or raw_key == false or raw_key == cjson.null then
error("Record id " .. tostring(source_record.id or "?") .. " has no value for key field '" .. config.key_field .. "'.")
end
local key_value = normalize_value(raw_key, config.many2one_mode, config.array_mode)
if key_value == "" then
error("Record id " .. tostring(source_record.id or "?") .. " has an empty value for key field '" .. config.key_field .. "'.")
end
if keys_seen[key_value] then
error("Duplicate value '" .. key_value .. "' in key field '" .. config.key_field .. "'.")
end
keys_seen[key_value] = true
local new_record = { key_value }
local discovered = {}
for source, _ in pairs(source_record) do discovered[#discovered + 1] = source end
table.sort(discovered)
for _, source in ipairs(discovered) do
local output = aliases[source] or source
if source ~= config.key_field then
register_field(source)
local value = normalize_value(source_record[source], config.many2one_mode, config.array_mode)
if (image_fields[source] or image_fields[output]) and source_record[source] == false then value = "" end
new_record[output] = value
end
end
records[#records + 1] = new_record
end
if #page_records < page_size then break end
offset = offset + #page_records
page = page + 1
if page > 100000 then error("Odoo pagination exceeded 100,000 pages.") end
end
local fields = { { name = key_output, key = "true" } }
for _, output in ipairs(field_order) do
local source = source_for_output[output] or output
local field = { name = output }
if image_fields[source] or image_fields[output] then
field.locationtype = "url"
field.imagepath = "FIELDSTR(FIELDNAME())"
end
fields[#fields + 1] = field
end
config.initialized = true
return RECORDSET.new(fields, records)
end
---------------------------------------------------------------------------------------------------------
-- Read-only provider and asset hooks
---------------------------------------------------------------------------------------------------------
function SaveRecords(config, datasource, records)
for record_index = 1, records:size() do
local record = records:getrecord(record_index)
for field_index = 1, record:size() do
record:field(field_index):setupdatestate(0)
end
end
end
function CanResolveAssetURI(field, uri)
return false
end
function ResolveAssetURI(config, uri)
return uri
end
function GetAsset(config, location, filepath)
return false
end
function GetReleaseNotes()
return {
title = PROVIDER_NAME .. " Release Notes",
body = "<h1>" .. PROVIDER_NAME .. "</h1>" ..
"<h3>v" .. PROVIDER_VERSION .. "</h3>" ..
"<ul>" ..
"<li>Read-only execute_kw/search_read connector.</li>" ..
"<li>Configurable model, fields, domain, order and context.</li>" ..
"<li>Automatic offset/limit pagination.</li>" ..
"<li>Configurable aliases, relations, arrays and image URL fields.</li>" ..
"</ul>"
}
end
function GetInfo()
return {
url = "https://www.odoo.com/documentation/18.0/developer/reference/external_api.html",
name = PROVIDER_NAME,
version = PROVIDER_VERSION
}
end