[FIX] spreadsheet: check valid json data file while uploading

JSONDecodeError: Expecting value: line 1 column 1 (char 0)
This error occurs when we try to upload an invalid json data file.

sentry- 3957419658

X-original-commit: https://github.com/odoo/odoo/commit/1a60a19a1c001c12cf933dd8c82332167b609038
Part-of: odoo/odoo#120818
This commit is contained in:
Om Rabara
2023-05-11 18:25:20 +02:00
parent c6af10ff34
commit f42f5324fd
2 changed files with 26 additions and 2 deletions
+12 -1
View File
@@ -2,8 +2,10 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import base64
from odoo import api, fields, models
import json
from odoo import api, fields, models, _
from odoo.addons.spreadsheet.utils import empty_spreadsheet_data_base64
from odoo.exceptions import ValidationError
class SpreadsheetMixin(models.AbstractModel):
_name = "spreadsheet.mixin"
@@ -28,3 +30,12 @@ class SpreadsheetMixin(models.AbstractModel):
spreadsheet.spreadsheet_binary_data = False
else:
spreadsheet.spreadsheet_binary_data = base64.b64encode(spreadsheet.spreadsheet_data.encode())
@api.onchange('spreadsheet_binary_data')
def _onchange_data_(self):
if self.spreadsheet_binary_data:
try:
data_str = base64.b64decode(self.spreadsheet_binary_data).decode('utf-8')
json.loads(data_str)
except:
raise ValidationError(_('Invalid JSON Data'))
@@ -1,8 +1,10 @@
from collections import defaultdict
from itertools import chain
import base64
import json
import re
from odoo.tests.common import TransactionCase
from odoo.tests.common import TransactionCase, Form
from odoo.exceptions import ValidationError
markdown_link_regex = r"^\[([^\[]+)\]\((.+)\)$"
@@ -216,3 +218,14 @@ class ValidateSpreadsheetData(TransactionCase):
raise AssertionError(
f"xml id '{xml_id}' used in spreadsheet '{spreadsheet_name}' does not exist"
)
def test_onchange_json_data(self):
group = self.env["spreadsheet.dashboard.group"].create(
{"name": "a group"}
)
spreadsheet_form = Form(self.env['spreadsheet.dashboard'])
spreadsheet_form.name = 'Test spreadsheet'
spreadsheet_form.dashboard_group_id = group
spreadsheet_form.spreadsheet_binary_data = base64.b64encode(json.dumps({'key': 'value'}).encode('utf-8'))
with self.assertRaises(ValidationError, msg='Invalid JSON Data'):
spreadsheet_form.spreadsheet_binary_data = base64.b64encode('invalid json'.encode('utf-8'))