Skip to content
Snippets Groups Projects
Commit 5d5282825fb2 authored by Houzefa Abbasbhay's avatar Houzefa Abbasbhay :slight_smile:
Browse files

Fix JSON pretty-print in envelope/message forms

parent 2df42804e8d2
No related branches found
No related tags found
1 merge request!16Fix JSON pretty-print in envelope/message forms
Pipeline #117510 passed
Changelog
=========
18.0.2.1.2
----------
Fix JSON pretty-print in envelope/message forms.
18.0.2.1.1
----------
......
......@@ -22,7 +22,7 @@
"license": "AGPL-3",
"summary": "Xbus common elements",
"icon": "/xbus_common/static/description/icon.svg",
"version": "18.0.2.1.1",
"version": "18.0.2.1.2",
"category": "Technical",
"author": "XCG Consulting",
"website": "https://orbeet.io/",
......
......@@ -17,5 +17,4 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import base64
import json
......@@ -21,5 +20,4 @@
import json
import pprint
from odoo import api, fields, models, tools # type: ignore[import-untyped]
......@@ -59,10 +57,10 @@
@api.depends("header")
def _compute_header_json(self):
for message in self:
message.header_json = message._convert_binary_to_json(
message.header_json = message._pprint_json(
message.with_context(bin_size=False).header
)
@api.depends("body")
def _compute_body_json(self):
for message in self:
......@@ -63,10 +61,10 @@
message.with_context(bin_size=False).header
)
@api.depends("body")
def _compute_body_json(self):
for message in self:
message.body_json = message._convert_binary_to_json(
message.body_json = message._pprint_json(
message.with_context(bin_size=False).body
)
......@@ -70,22 +68,13 @@
message.with_context(bin_size=False).body
)
def _convert_binary_to_json(self, binary_value):
if binary_value:
if isinstance(
binary_value, bytes
): # Check if it is of type bytes before decoding
string_value = base64.b64decode(
binary_value
).decode() # utf-8 by default
else:
string_value = binary_value
if string_value.startswith("{") or string_value.startswith("["):
try:
json_dict = json.loads(string_value)
return pprint.pformat(json_dict)
except json.JSONDecodeError:
return False
def _pprint_json(self, bin_value):
"""Pretty-print when payloads actually are JSON."""
if bin_value and bin_value[:1] in (b"{", b"["):
try:
return json.dumps(json.loads(bin_value), indent=2)
except json.JSONDecodeError:
return False
return False
def _get_type_selection(self) -> list[tuple[str, str]]:
......
......@@ -103,7 +103,7 @@
self.cr.execute(
"INSERT INTO xbus_message (envelope_id, uid, type, header, body) VALUES "
"(%s, %s, %s, %s, %s) RETURNING id",
(envelope_id, uid, "test_1", b"header", b"body"),
(envelope_id, uid, "test_1", b'{"key": "value"}', b"body"),
)
message1_id = self.cr.fetchall()[0][0]
# Add a message with body only
......@@ -119,5 +119,6 @@
self.env["xbus.message"]
.with_user(self.env.ref("base.user_admin"))
.search([("envelope_id", "=", envelope_id)])
.sorted("id")
)
self.assertEqual(len(messages), 2)
......@@ -122,2 +123,8 @@
)
self.assertEqual(len(messages), 2)
self.assertEqual((message := messages[0]).type, "test_1")
self.assertEqual(message.header, b'{"key": "value"}')
self.assertEqual(message.header_json, '{\n "key": "value"\n}')
self.assertEqual((message := messages[1]).type, "test_2")
self.assertFalse(message.header)
self.assertFalse(message.header_json)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment