Files
2026-07-01 14:41:49 +07:00

106 lines
3.8 KiB
Python

import json
import logging
import psycopg2
import threading
import re
from odoo import tools, api, fields, models, _
from odoo.addons.base.models.ir_http import _logger, FasterRule, IrHttp
from odoo.addons.base.models.assetsbundle import JavascriptAsset
from odoo.http import request, ROUTING_KEYS
from odoo.tools.misc import submap
from odoo.modules.registry import Registry
from odoo.tools.js_transpiler import transpile_javascript
from odoo.tools import config as odoo_config
import odoo
import odoo.exceptions
import odoo.modules.registry
import werkzeug.utils
import werkzeug.routing
import werkzeug.exceptions
import werkzeug
# Global variable for base URL replacement
base_sorturl = ['']
class IrConfigParameter(models.Model):
_inherit = "ir.config_parameter"
def write(self, vals):
result = super(IrConfigParameter, self).write(vals)
if result and any(rec.key == 'web.base.sorturl' for rec in self):
# Clear routing cache properly for Odoo 19
self.env['ir.http'].env.registry.clear_cache("routing")
# Regenerate assets bundles
self.env['ir.attachment'].regenerate_assets_bundles()
# Return proper action for client reload
return {'type': 'ir.actions.client', 'tag': 'reload'}
return result
# ✅ Override JavascriptAsset.content property - Odoo 19 compatible
@property
def content(self):
content = super(JavascriptAsset, self).content
replacement = base_sorturl[0] or ''
if self.name == "/web/static/src/core/browser/router.js":
content = re.sub(r'(?<![@\w])odoo(?!\w)', replacement, content)
if self.name == "/web/static/src/webclient/navbar/navbar.js":
content = re.sub(r'(?<![@\w])odoo(?!\w)', replacement, content)
if self.is_transpiled:
if not getattr(self, '_converted_content', None):
self._converted_content = transpile_javascript(self.url, content)
return self._converted_content
return content
JavascriptAsset.content = content
# ✅ Override routing_map - Keep BOTH original and custom routes
@tools.ormcache('key', cache='routing')
def routing_map(self, key=None):
config_parameter = self.env['ir.config_parameter'].sudo()
base_sorturl[0] = config_parameter.get_param("web.base.sorturl", "")
_logger.info("Generating routing map for key %s, base_sorturl=%s",
str(key), base_sorturl[0])
registry = Registry(threading.current_thread().dbname)
installed = registry._init_modules.union(
set(odoo_config.get('server_wide_modules', []))
)
mods = sorted(installed)
routing_map = werkzeug.routing.Map(
strict_slashes=False, converters=self._get_converters())
for url, endpoint in self._generate_routing_rules(mods, converters=self._get_converters()):
# ✅ Add the custom route (e.g., /china/...)
if 'odoo' in url and base_sorturl[0]:
custom_url = url.replace('odoo', base_sorturl[0])
routing = submap(endpoint.routing, ROUTING_KEYS)
if routing['methods'] is not None and 'OPTIONS' not in routing['methods']:
routing['methods'] = routing['methods'] + ['OPTIONS']
rule = FasterRule(custom_url, endpoint=endpoint, **routing)
rule.merge_slashes = False
routing_map.add(rule)
_logger.debug("Added custom route: %s", custom_url)
# ✅ ALWAYS add the original route (e.g., /odoo/...)
routing = submap(endpoint.routing, ROUTING_KEYS)
if routing['methods'] is not None and 'OPTIONS' not in routing['methods']:
routing['methods'] = routing['methods'] + ['OPTIONS']
rule = FasterRule(url, endpoint=endpoint, **routing)
rule.merge_slashes = False
routing_map.add(rule)
_logger.debug("Added original route: %s", url)
return routing_map
IrHttp.routing_map = routing_map