[FIX] base_import_module: adapt ormcache for industries

Instead of changing the type of lists to make an ormcache that works,
we'v extracted the function that call the server apps.odoo.com to pass
it the payload in json (as it will be sent to the server), and use the
payload value as ormcache

closes odoo/odoo#149015

Signed-off-by: Pierre Masereel (pim) <pim@odoo.com>
This commit is contained in:
Pierre Masereel
2024-01-25 13:42:43 +00:00
parent af77d74f59
commit 6f887582cc
+25 -19
View File
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import ast
import base64
import json
import logging
import lxml
import os
@@ -25,8 +26,6 @@ _logger = logging.getLogger(__name__)
APPS_URL = "https://apps.odoo.com"
MAX_FILE_SIZE = 100 * 1024 * 1024 # in megabytes
def to_tuple(t):
return tuple(map(to_tuple, t)) if isinstance(t, (list, tuple)) else t
class IrModule(models.Model):
_inherit = "ir.module.module"
@@ -287,9 +286,8 @@ class IrModule(models.Model):
@api.model
def web_search_read(self, domain, specification, offset=0, limit=None, order=None, count_limit=None):
if _domain_asks_for_industries(domain):
fields_name = tuple(specification.keys())
domain_tuple = to_tuple(domain or [])
modules_list = self._get_modules_from_apps(fields_name, 'industries', False, domain_tuple, offset=offset, limit=limit)
fields_name = list(specification.keys())
modules_list = self._get_modules_from_apps(fields_name, 'industries', False, domain, offset=offset, limit=limit)
return {
'length': len(modules_list),
'records': modules_list,
@@ -308,7 +306,7 @@ class IrModule(models.Model):
}
def web_read(self, specification):
fields = tuple(specification.keys())
fields = list(specification.keys())
module_type = self.env.context.get('module_type', 'official')
if module_type != 'official':
modules_list = self._get_modules_from_apps(fields, module_type, self.env.context.get('module_name'))
@@ -317,23 +315,20 @@ class IrModule(models.Model):
return super().web_read(specification)
@api.model
@ormcache('fields', 'module_type', 'module_name', 'domain', 'limit', 'offset')
def _get_modules_from_apps(self, fields, module_type, module_name, domain=None, limit=None, offset=None):
payload = {
'series': major_version,
'module_fields': fields,
'module_type': module_type,
'module_name': module_name,
'domain': domain,
'limit': limit,
'offset': offset,
'params': {
'series': major_version,
'module_fields': fields,
'module_type': module_type,
'module_name': module_name,
'domain': domain,
'limit': limit,
'offset': offset,
}
}
try:
resp = requests.post(
f"{APPS_URL}/loempia/listdatamodules",
json={'params': payload},
timeout=5.0,
)
resp = self._call_apps(json.dumps(payload))
resp.raise_for_status()
modules_list = resp.json().get('result', [])
for mod in modules_list:
@@ -357,6 +352,17 @@ class IrModule(models.Model):
except requests.exceptions.ConnectionError:
raise UserError(_('Connection to %s failed The list of industry modules cannot be fetched') % APPS_URL)
@api.model
@ormcache('payload')
def _call_apps(self, payload):
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
return requests.post(
f"{APPS_URL}/loempia/listdatamodules",
data=payload,
headers=headers,
timeout=5.0,
)
@api.model
@ormcache()
def _get_industry_categories_from_apps(self):