Skip to content

Commit 0a3abdf

Browse files
committed
Registry Additional Info fixed
Signed-off-by: Lalith Kota <[email protected]>
1 parent 2d62edf commit 0a3abdf

File tree

8 files changed

+112
-77
lines changed

8 files changed

+112
-77
lines changed

.pre-commit-config.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
exclude: |
22
(?x)
33
# NOT INSTALLABLE ADDONS
4-
^g2p_registry_addl_info_rest_api/|
54
^g2p_registry_rest_api_extension_demo/|
65
# END NOT INSTALLABLE ADDONS
76
# Files and folders generated by bots, to avoid loops

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ addon | version | maintainers | summary
4040
[g2p_portal_auth](g2p_portal_auth/) | 17.0.0.0.0 | | G2P Portal Auth
4141
[g2p_profile_image](g2p_profile_image/) | 17.0.0.0.0 | | OpenG2P Profile Image
4242
[g2p_registry_addl_info](g2p_registry_addl_info/) | 17.0.0.0.0 | | G2P Registry: Additional Info
43+
[g2p_registry_addl_info_rest_api](g2p_registry_addl_info_rest_api/) | 17.0.0.0.0 | | G2P Registry: Additional Info REST API
4344
[g2p_registry_base](g2p_registry_base/) | 17.0.0.0.0 | | G2P Registry: Base
4445
[g2p_registry_documents](g2p_registry_documents/) | 17.0.0.0.0 | | G2P Registry: Documents
4546
[g2p_registry_encryption](g2p_registry_encryption/) | 17.0.0.0.0 | | G2P Registry: Encryption
@@ -57,7 +58,6 @@ Unported addons
5758
---------------
5859
addon | version | maintainers | summary
5960
--- | --- | --- | ---
60-
[g2p_registry_addl_info_rest_api](g2p_registry_addl_info_rest_api/) | 17.0.0.0.0 (unported) | | G2P Registry: Additional Info REST API
6161
[g2p_registry_rest_api_extension_demo](g2p_registry_rest_api_extension_demo/) | 17.0.0.0.0 (unported) | | G2P Registry: Rest API Extension Demo
6262

6363
[//]: # (end addons)
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,56 @@
11
/** @odoo-module **/
22

3-
import {onMounted, useExternalListener, useState} from "@odoo/owl";
3+
import {markup, useRef, useState} from "@odoo/owl";
44
import {TextField} from "@web/views/fields/text/text_field";
55
import {_t} from "@web/core/l10n/translation";
66
import {registry} from "@web/core/registry";
77
import {useService} from "@web/core/utils/hooks";
88

9-
export class JsonFieldWidget extends TextField {
9+
export class G2PRegistryAddlInfoComponent extends TextField {
10+
static template = "g2p_reg_addl_info_tpl";
11+
1012
setup() {
1113
super.setup();
12-
this.state = useState({recordClicked: false, noValue: false});
14+
this.state = useState({editingMode: false});
15+
this.textareaRef = useRef("textarea");
1316
this.notification = useService("notification");
14-
onMounted(() => this.validateValue());
15-
useExternalListener(window, "click", this.onclick);
16-
useExternalListener(window, "mouseup", this.onMouseup);
1717
}
1818

19-
validateValue() {
20-
const val = this.props.record.data.additional_g2p_info;
21-
if (val) {
22-
if ((!(val.charAt(0) === "{") && !(val.charAt(val.length - 1) === "}")) || !val) {
23-
this.state.noValue = true;
24-
}
25-
} else {
26-
this.state.noValue = true;
19+
editButtonClick() {
20+
const val = this.props.record.data[this.props.name];
21+
if (typeof val !== "string") {
22+
this.props.record.data[this.props.name] = JSON.stringify(val);
2723
}
24+
this.state.editingMode = true;
2825
}
2926

30-
onclick(event) {
31-
if (this.editingRecord && event.target.closest(".json-widget")) {
32-
this.state.recordClicked = true;
33-
this.state.noValue = true;
27+
doneButtonClick() {
28+
let val = null;
29+
try {
30+
val = JSON.parse(this.textareaRef.el.value);
31+
} catch (err) {
32+
this.notification.add(_t("Registry Additional Info"), {
33+
title: _t("Invalid Json Value"),
34+
type: "danger",
35+
});
36+
console.error(err);
37+
return;
3438
}
35-
this.validateValue();
36-
}
37-
38-
onMouseup(ev) {
39-
if (!ev.target.closest(".o_field_g2p_registry_addl_info_widget textarea")) {
40-
this.state.recordClicked = false;
41-
this.state.noValue = false;
39+
try {
40+
this.props.record.update({[this.props.name]: val});
41+
this.state.editingMode = false;
42+
} catch (err) {
43+
this.notification.add(_t("Registry Additional Info"), {
44+
title: _t("Error Updating Json"),
45+
type: "danger",
46+
});
47+
console.error(err);
4248
}
43-
this.validateValue();
44-
}
45-
46-
get editingRecord() {
47-
return !this.props.readonly;
4849
}
4950

5051
renderjson() {
52+
const valuesJsonOrig = this.props.record.data[this.props.name];
5153
try {
52-
const valuesJsonOrig = this.props.record.data.additional_g2p_info;
53-
if (typeof valuesJsonOrig === "string" || valuesJsonOrig instanceof String) {
54-
const parsedValue = JSON.parse(valuesJsonOrig);
55-
return parsedValue;
56-
}
57-
5854
if (Array.isArray(valuesJsonOrig)) {
5955
const sectionsJson = {};
6056
valuesJsonOrig.forEach((element) => {
@@ -65,31 +61,51 @@ export class JsonFieldWidget extends TextField {
6561
const valuesJson = this.flattenJson(valuesJsonOrig);
6662
return valuesJson;
6763
} catch (err) {
68-
this.notification.add(_t("Additional Information"), {
64+
console.error(err);
65+
this.notification.add(_t("Registry Additional Info"), {
6966
title: _t("Invalid Json Value"),
7067
type: "danger",
7168
});
72-
this.state.recordClicked = true;
69+
this.state.editingMode = true;
7370
return {};
7471
}
7572
}
7673

7774
flattenJson(object) {
78-
const jsonObject = JSON.parse(JSON.stringify(object));
75+
let jsonObject = object;
76+
if (typeof object === "string") {
77+
jsonObject = JSON.parse(object);
78+
}
7979
for (const key in jsonObject) {
80-
if (typeof jsonObject[key] === "object") {
81-
jsonObject[key] = JSON.stringify(jsonObject[key]);
80+
if (!jsonObject[key]) {
81+
continue;
82+
} else if (
83+
Array.isArray(jsonObject[key]) &&
84+
jsonObject[key].length > 0 &&
85+
typeof jsonObject[key][0] === "object" &&
86+
"document_id" in jsonObject[key][0] &&
87+
"document_slug" in jsonObject[key][0]
88+
) {
89+
var documentFiles = "";
90+
for (var i = 0; i < jsonObject[key].length; i++) {
91+
const document_slug = jsonObject[key][i].document_slug;
92+
const host = window.location.origin;
93+
if (i > 0) {
94+
documentFiles += `<br />`;
95+
}
96+
documentFiles += `<a href="${host}/storage.file/${document_slug}" target="_blank">${document_slug}<span class="fa fa-fw fa-external-link"></span></a>`;
97+
}
98+
jsonObject[key] = markup(documentFiles);
99+
} else if (typeof jsonObject[key] === "object") {
100+
jsonObject[key] = this.flattenJson(jsonObject[key]);
82101
}
83102
}
84103
return jsonObject;
85104
}
86105
}
87106

88-
JsonFieldWidget.template = "addl_info_each_table";
89-
90-
export const JsonField = {
91-
component: JsonFieldWidget,
92-
supportedTypes: ["json", "text", "html"],
107+
export const g2pRegistryAddlInfoWidget = {
108+
component: G2PRegistryAddlInfoComponent,
109+
supportedTypes: ["jsonb", "text", "html"],
93110
};
94-
95-
registry.category("fields").add("g2p_registry_addl_info_widget", JsonField);
111+
registry.category("fields").add("g2p_registry_addl_info_widget", g2pRegistryAddlInfoWidget);
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,54 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22
<templates xml:space="preserve">
3-
4-
<t t-name="addl_info_each_table">
5-
<t t-if="! state.recordClicked and ! state.noValue">
6-
<table
7-
class="o_group o_inner_group table table-bordered json-widget"
8-
border="1"
9-
style="border-collapse:collapse"
10-
>
11-
<tbody>
12-
<t t-foreach="Object.entries(this.renderjson())" t-as="key" t-key="key">
13-
<tr>
3+
<t t-name="g2p_registry_addl_info_tpl">
4+
<t t-if="!state.editingMode">
5+
<t t-set="jsonToRender" t-value="this.renderjson()" />
6+
<t t-call="g2p_registry_addl_info_table_tpl" />
7+
<button
8+
class="btn btn-success"
9+
name="g2p_registry_addl_info_edit_button"
10+
t-on-click="editButtonClick"
11+
>Edit</button>
12+
</t>
13+
<t t-else="">
14+
<t t-call="web.TextField" />
15+
<br />
16+
<button
17+
class="btn btn-success"
18+
name="g2p_registry_addl_info_save_button"
19+
t-on-click="doneButtonClick"
20+
>Done</button>
21+
</t>
22+
</t>
23+
<t t-name="g2p_registry_addl_info_table_tpl">
24+
<table class="o_group o_inner_group table table-bordered" border="1" style="border-collapse:collapse">
25+
<tbody>
26+
<t t-set="isArray" t-value="Array.isArray(jsonToRender)" />
27+
<t t-foreach="Object.entries(jsonToRender)" t-as="key" t-key="key[0]">
28+
<tr>
29+
<t t-if="!isArray">
1430
<td class="o_td_label">
15-
<label class="o_form_label o_quick_editable">
31+
<label class="o_form_label">
1632
<strong>
1733
<t t-esc="key[0]" />
1834
</strong>
1935
</label>
2036
</td>
21-
<td>
22-
<span class="o_field_widget o_quick_editable">
37+
</t>
38+
<td>
39+
<t t-if="typeof key[1] === 'object' || Array.isArray(key[1])">
40+
<t t-set="jsonToRender" t-value="key[1]" />
41+
<t t-call="g2p_registry_addl_info_table_tpl" />
42+
</t>
43+
<t t-else="">
44+
<span class="o_field_widget">
2345
<t t-esc="key[1]" />
2446
</span>
25-
</td>
26-
</tr>
27-
</t>
28-
</tbody>
29-
</table>
30-
</t>
31-
<t t-else="">
32-
<t t-call="web.TextField" />
33-
</t>
47+
</t>
48+
</td>
49+
</tr>
50+
</t>
51+
</tbody>
52+
</table>
3453
</t>
3554
</templates>

g2p_registry_addl_info_rest_api/__manifest__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
"demo": [],
1616
"images": [],
1717
"application": False,
18-
"installable": False,
18+
"installable": True,
1919
"auto_install": False,
2020
}

g2p_registry_addl_info_rest_api/models/group_membership.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33

44
class GroupMembersInfoIn(group_membership.GroupMembersInfoIn, extends=group_membership.GroupMembersInfoIn):
5-
additional_g2p_info: list[dict] | dict = None
5+
additional_g2p_info: list | dict | None = None

g2p_registry_addl_info_rest_api/models/registrant.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33

44
class RegistrantAddlInfoIn(registrant.RegistrantInfoIn, extends=registrant.RegistrantInfoIn):
5-
additional_g2p_info: list[dict] | dict = None
5+
additional_g2p_info: list | dict | None = None
66

77

88
class RegistrantAddlInfoOut(registrant.RegistrantInfoOut, extends=registrant.RegistrantInfoOut):
9-
additional_g2p_info: str = ""
9+
additional_g2p_info: list | dict | None = None

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ dependencies = [
2121
"odoo-addon-g2p_portal_auth @ {root:uri}/g2p_portal_auth",
2222
"odoo-addon-g2p_profile_image @ {root:uri}/g2p_profile_image",
2323
"odoo-addon-g2p_registry_addl_info @ {root:uri}/g2p_registry_addl_info",
24+
"odoo-addon-g2p_registry_addl_info_rest_api @ {root:uri}/g2p_registry_addl_info_rest_api",
2425
"odoo-addon-g2p_registry_base @ {root:uri}/g2p_registry_base",
2526
"odoo-addon-g2p_registry_documents @ {root:uri}/g2p_registry_documents",
2627
"odoo-addon-g2p_registry_encryption @ {root:uri}/g2p_registry_encryption",

0 commit comments

Comments
 (0)