Skip to content

Commit 874a7f0

Browse files
committed
fix: recordset table
1 parent 5c7b0f8 commit 874a7f0

File tree

5 files changed

+75
-57
lines changed

5 files changed

+75
-57
lines changed

CHANGELOG.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
# Changelog
22

3-
**11.7.3**
3+
**11.8.0**
44

55
```
6+
ADD: options: Theming (part 1)
7+
68
FIX: exportfile xml: Include ids for records without reference
9+
FIX: hex2rgb: Correctly handle hex values
10+
FIX: Command Assistant: Colors (issue #142)
11+
FIX: Recordset Table: Display the contents of fields of type Object (issue #143)
12+
FIX: Recordset Table: Correct processing of binary type fields
713
```
814

915
**11.7.2**

src/js/page/odoo/commands/common/read.mjs

+14-12
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,24 @@ import type Terminal from '@odoo/terminal';
1515
async function cmdSearchModelRecordId(this: Terminal, kwargs: CMDCallbackArgs, ctx: CMDCallbackContext) {
1616
const search_all_fields = kwargs.field[0] === '*';
1717
let fields = kwargs.field;
18-
const bin_fields = [];
18+
let fieldDefs = {};
1919

2020
// Due to possible problems with binary fields it is necessary to filter them out
2121
if (search_all_fields) {
2222
if (!kwargs.read_binary) {
2323
// $FlowFixMe
24-
const fieldDefs = await callModel<{[string]: Object}>(
24+
fieldDefs = await callModel<{[string]: Object}>(
2525
kwargs.model,
2626
'fields_get',
27-
[fields],
27+
[],
2828
null,
2929
await this.getContext(),
3030
kwargs.options,
3131
);
3232

3333
fields = [];
3434
Object.entries(fieldDefs).forEach(item => {
35-
if (item[1].type === 'binary') {
36-
bin_fields.push(item[0]);
37-
} else {
35+
if (item[1].type !== 'binary') {
3836
fields.push(item[0]);
3937
}
4038
});
@@ -44,16 +42,20 @@ async function cmdSearchModelRecordId(this: Terminal, kwargs: CMDCallbackArgs, c
4442
}
4543

4644
const result = await searchRead(kwargs.model, [['id', 'in', kwargs.id]], fields, await this.getContext());
47-
48-
if (bin_fields.length !== 0) {
49-
for (const item of result) {
50-
for (const bin_field of bin_fields) {
51-
item[bin_field] = {oterm: true, binary: true};
45+
if (search_all_fields) {
46+
if (!kwargs.read_binary) {
47+
const def_fields = Object.keys(fieldDefs);
48+
for (const field of def_fields) {
49+
if (fieldDefs[field].type === 'binary') {
50+
for (const record of result) {
51+
record[field] = null;
52+
}
53+
}
5254
}
5355
}
5456
}
5557

56-
const recordset = Recordset.make(kwargs.model, result);
58+
const recordset = Recordset.make(kwargs.model, result, fieldDefs);
5759
ctx.screen.print(recordset);
5860
return recordset;
5961
}

src/js/page/odoo/commands/common/search.mjs

+15-11
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async function cmdSearchModelRecord(this: Terminal, kwargs: CMDCallbackArgs, ctx
2525
}
2626
const sresult = buff.data.slice(0, lines_total);
2727
buff.data = buff.data.slice(lines_total);
28-
const recordset = Recordset.make(kwargs.model, sresult);
28+
const recordset = Recordset.make(kwargs.model, sresult, buff.fieldDefs);
2929
ctx.screen.print(recordset);
3030
if (buff.data.length) {
3131
ctx.screen.printError(
@@ -52,16 +52,14 @@ async function cmdSearchModelRecord(this: Terminal, kwargs: CMDCallbackArgs, ctx
5252
}
5353

5454
// Due to possible problems with binary fields it is necessary to filter them out
55-
const bin_fields = [];
55+
let fieldDefs = {};
5656
if (search_all_fields) {
5757
if (!kwargs.read_binary) {
58-
const fieldDefs = await getFieldsInfo(kwargs.model, false, await this.getContext(), kwargs.options);
58+
fieldDefs = await getFieldsInfo(kwargs.model, false, await this.getContext(), kwargs.options);
5959

6060
fields = [];
6161
Object.entries(fieldDefs).forEach(item => {
62-
if (item[1].type === 'binary') {
63-
bin_fields.push(item[0]);
64-
} else {
62+
if (item[1].type !== 'binary') {
6563
fields.push(item[0]);
6664
}
6765
});
@@ -76,10 +74,15 @@ async function cmdSearchModelRecord(this: Terminal, kwargs: CMDCallbackArgs, ctx
7674
orderBy: kwargs.order,
7775
}));
7876

79-
if (bin_fields.length !== 0) {
80-
for (const item of result) {
81-
for (const bin_field of bin_fields) {
82-
item[bin_field] = {oterm: true, binary: true};
77+
if (search_all_fields) {
78+
if (!kwargs.read_binary) {
79+
const def_fields = Object.keys(fieldDefs);
80+
for (const field of def_fields) {
81+
if (fieldDefs[field].type === 'binary') {
82+
for (const record of result) {
83+
record[field] = null;
84+
}
85+
}
8386
}
8487
}
8588
}
@@ -90,10 +93,11 @@ async function cmdSearchModelRecord(this: Terminal, kwargs: CMDCallbackArgs, ctx
9093
search_buffer[ctx.meta.info.cmdName] = {
9194
model: kwargs.model,
9295
data: sresult.slice(lines_total),
96+
fieldDefs: fieldDefs,
9397
};
9498
sresult = sresult.slice(0, lines_total);
9599
}
96-
const recordset = Recordset.make(kwargs.model, sresult);
100+
const recordset = Recordset.make(kwargs.model, sresult, fieldDefs);
97101
ctx.screen.print(recordset);
98102
ctx.screen.print(i18n.t("cmdSearch.result.count", "Records count: {{count}}", {count: sresult.length}));
99103
if (need_truncate) {

src/js/page/terminal/core/recordset.mjs

+32-28
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import isEmpty from '@trash/utils/is_empty';
66
import isNumber from '@trash/utils/is_number';
77

8+
export type FieldDef = {...};
9+
810
const RecordHandler = {
911
// $FlowFixMe
1012
get(target: Object, prop: mixed) {
@@ -13,33 +15,31 @@ const RecordHandler = {
1315
prop === 'toWrite' ||
1416
prop === 'rollback' ||
1517
prop === 'persist' ||
18+
prop === '__info' ||
1619
typeof prop === 'symbol'
1720
) {
1821
const ref = target[prop];
19-
if (typeof ref === 'function') {
20-
return ref.bind(target);
21-
}
22-
return target[prop];
22+
return (typeof ref === 'function') ? ref.bind(target) : ref;
2323
}
24-
return target.values[prop];
24+
return target.__values[prop];
2525
},
2626
// $FlowFixMe
2727
set(target: Object, prop: mixed, value: mixed) {
28-
target.modified_fields.push(prop);
29-
if (target.values.length === 1) {
30-
return Reflect.set(target.values[0], prop, value);
28+
target.__modified_fields.push(prop);
29+
if (target.__values.length === 1) {
30+
return Reflect.set(target.__values[0], prop, value);
3131
}
32-
return Reflect.set(target.values, prop, value);
32+
return Reflect.set(target.__values, prop, value);
3333
},
3434

3535
// $FlowFixMe
3636
ownKeys(target: Object) {
37-
return Reflect.ownKeys(target.values);
37+
return Reflect.ownKeys(target.__values);
3838
},
3939
// $FlowFixMe
4040
has(target: Object, prop: mixed) {
4141
if (typeof prop === 'string' || typeof prop === 'number') {
42-
return prop in target.values;
42+
return prop in target.__values;
4343
}
4444
return false;
4545
},
@@ -50,36 +50,38 @@ const RecordHandler = {
5050

5151
export class Record {
5252
#origin: {[string]: mixed} = {};
53-
values: {[string]: mixed};
54-
modified_fields: Array<string> = [];
53+
__info: FieldDef;
54+
__values: {[string]: mixed};
55+
__modified_fields: Array<string> = [];
5556

56-
constructor(values: {...}) {
57+
constructor(values: {...}, field_info: FieldDef) {
5758
this.#origin = {...values};
58-
this.values = values;
59+
this.__values = values;
60+
this.__info = field_info;
5961
}
6062

6163
persist() {
62-
this.#origin = {...this.values};
63-
this.modified_fields = [];
64+
this.#origin = {...this.__values};
65+
this.__modified_fields = [];
6466
}
6567

6668
toJSON(): {...} {
67-
return this.values;
69+
return this.__values;
6870
}
6971

7072
toWrite(): {[string]: mixed} {
7173
const write_vals: {[string]: mixed} = {};
72-
for (const field_name of this.modified_fields) {
73-
write_vals[field_name] = this.values[field_name];
74+
for (const field_name of this.__modified_fields) {
75+
write_vals[field_name] = this.__values[field_name];
7476
}
7577
return write_vals;
7678
}
7779

7880
rollback() {
79-
for (const field_name of this.modified_fields) {
80-
this.values[field_name] = this.#origin[field_name];
81+
for (const field_name of this.__modified_fields) {
82+
this.__values[field_name] = this.#origin[field_name];
8183
}
82-
this.modified_fields = [];
84+
this.__modified_fields = [];
8385
}
8486

8587
// $FlowFixMe
@@ -90,7 +92,7 @@ export class Record {
9092
// $FlowFixMe
9193
[Symbol.toPrimitive](hint) {
9294
if (hint === 'string') {
93-
return JSON.stringify(this.values);
95+
return JSON.stringify(this.__values);
9496
}
9597
}
9698
}
@@ -136,21 +138,23 @@ const RecordsetHandler = {
136138
export default class Recordset {
137139
#model: string;
138140
#records: Array<Record> = [];
141+
#fields: {[string]: FieldDef} = {};
139142

140143
// $FlowFixMe
141144
static isValid(obj: Object) {
142145
return obj instanceof Recordset;
143146
}
144147

145-
static make(model: string, values: Array<{[string]: mixed}>): Recordset {
146-
const rs = new Recordset(model, values);
148+
static make(model: string, values: Array<{[string]: mixed}>, fields?: {[string]: FieldDef}): Recordset {
149+
const rs = new Recordset(model, values, fields);
147150
return new Proxy(rs, RecordsetHandler);
148151
}
149152

150-
constructor(model: string, values: Array<{[string]: mixed}>) {
153+
constructor(model: string, values: Array<{[string]: mixed}>, fields?: {[string]: FieldDef}) {
151154
this.#model = model;
155+
this.#fields = fields || {};
152156
for (const rec_vals of values) {
153-
const record = new Record(rec_vals);
157+
const record = new Record(rec_vals, this.#fields);
154158
this.#records.push(new Proxy(record, RecordHandler));
155159
}
156160
}

src/js/page/terminal/templates/screen_table_cell_record.mjs

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22
// Copyright Alexandre Díaz <[email protected]>
33
// License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
44

5+
import {Record} from '@terminal/core/recordset';
56
import encodeHTML from '@terminal/utils/encode_html';
7+
import prettyObjectString from '@terminal/utils/pretty_object_string';
68

79
export default function (model: string, field: string, item: {[string]: string | number}): string {
810
const item_val = item[field];
9-
if (
11+
if (item instanceof Record && item.__info[field]?.type === 'binary') {
12+
return `<span class='btn terminal-btn-secondary o_terminal_click o_terminal_read_bin_field' data-model='${model}' data-id='${item.id}' data-field='${field}'>Try Read Field</span>`;
13+
} else if (
1014
item_val !== null &&
11-
typeof item_val !== 'undefined' &&
1215
typeof item_val === 'object' &&
13-
item_val.oterm &&
14-
item_val.binary
16+
!(item_val instanceof Array)
1517
) {
16-
return `<span class='btn terminal-btn-secondary o_terminal_click o_terminal_read_bin_field' data-model='${model}' data-id='${item.id}' data-field='${field}'>Try Read Field</span>`;
18+
return prettyObjectString(item_val);
1719
}
1820
return encodeHTML(String(item_val));
1921
}

0 commit comments

Comments
 (0)