Skip to content
Snippets Groups Projects
Commit ac442b797458 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
Tags 18.0.3.0.0.1
No related merge requests found
Pipeline #117506 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,7 +17,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import base64
import json
import pprint
......@@ -59,10 +58,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 +62,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 +69,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 pprint.pformat(json.loads(bin_value))
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, "{'key': 'value'}")
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