Skip to content

Commit

Permalink
connector_importer: define serialized fields as binary
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardoalso committed Sep 20, 2024
1 parent 8c5c46f commit 97cd497
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions connector_importer/models/recordset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
# Copyright 2018 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

import base64
import json
import os
from collections import OrderedDict

from odoo import api, fields, models

from odoo.addons.base_sparse_field.models.fields import Serialized
from odoo.addons.component.utils import is_component_registry_ready
from odoo.addons.queue_job.job import DONE, STATES

Expand Down Expand Up @@ -64,8 +65,8 @@ class ImportRecordset(models.Model):
create_date = fields.Datetime()
record_ids = fields.One2many("import.record", "recordset_id", string="Records")
# store info about imports report
report_data = Serialized()
shared_data = Serialized()
report_data = fields.Binary(attachment=True)
shared_data = fields.Binary(attachment=True)
report_html = fields.Html("Report summary", compute="_compute_report_html")
full_report_url = fields.Char(compute="_compute_full_report_url")
jobs_global_state = fields.Selection(
Expand Down Expand Up @@ -141,9 +142,14 @@ def _set_serialized(self, fname, values, reset=False):
"""Update serialized data."""
_values = {}
if not reset:
_values = self[fname]
_values.update(values)
self[fname] = _values
_values = getattr(self, fname) or {}
if _values:
json_raw_data = self._get_json_from_binary(_values)

_values.update(json_raw_data)
json_report_data = json.dumps(_values)
_values = base64.b64encode(bytes(json_report_data, "utf-8"))
setattr(self, fname, _values)
# Without invalidating cache we will have a bug because of Serialized
# field in odoo. It uses json.loads on convert_to_cache, which leads
# to all of our int dict keys converted to strings. Except for the
Expand All @@ -160,9 +166,17 @@ def set_report(self, values, reset=False):
self.ensure_one()
self._set_serialized("report_data", values, reset=reset)

def _get_json_from_binary(self, binary_data=None):
json_raw_data = {}
if binary_data:
json_raw_data = base64.b64decode(binary_data).decode("utf-8")
json_raw_data = json.loads(json_raw_data)
return json_raw_data

def get_report(self):
self.ensure_one()
return self.report_data or {}
json_raw_data = self._get_json_from_binary(self.report_data)
return json_raw_data

def set_shared(self, values, reset=False):
"""Update import report values."""
Expand All @@ -171,7 +185,8 @@ def set_shared(self, values, reset=False):

def get_shared(self):
self.ensure_one()
return self.shared_data or {}
json_raw_data = self._get_json_from_binary(self.shared_data)
return json_raw_data

def _prepare_for_import_session(self, start=True):
"""Wipe all session related data."""
Expand All @@ -180,9 +195,10 @@ def _prepare_for_import_session(self, start=True):
report_data["_last_start"] = fields.Datetime.to_string(
fields.Datetime.now()
)
json_report_data = json.dumps(report_data)
values = {
"record_ids": [(5, 0, 0)],
"report_data": report_data,
"report_data": base64.b64encode(bytes(json_report_data, "utf-8")),
"shared_data": {},
}
self.write(values)
Expand Down

0 comments on commit 97cd497

Please sign in to comment.