35 lines
1.3 KiB
Python
35 lines
1.3 KiB
Python
|
|
# middleware.py
|
||
|
|
import re
|
||
|
|
import odoo
|
||
|
|
from odoo.tools import config as odoo_config
|
||
|
|
from odoo import tools, api, fields, models, _
|
||
|
|
|
||
|
|
|
||
|
|
class URLRewriteMiddleware:
|
||
|
|
"""WSGI middleware to rewrite 'odoo' in URLs to custom base"""
|
||
|
|
|
||
|
|
def __init__(self, app):
|
||
|
|
self.app = app
|
||
|
|
|
||
|
|
def _get_replacement(self):
|
||
|
|
"""Get replacement value from ir.config_parameter"""
|
||
|
|
try:
|
||
|
|
from odoo.modules.registry import Registry
|
||
|
|
registry = Registry(odoo_config.get('database', 'postgres'))
|
||
|
|
with registry.cursor() as cr:
|
||
|
|
env = api.Environment(cr, odoo.SUPERUSER_ID, {})
|
||
|
|
return env['ir.config_parameter'].sudo().get_param('web.base.sorturl', '')
|
||
|
|
except Exception:
|
||
|
|
return ''
|
||
|
|
|
||
|
|
def __call__(self, environ, start_response):
|
||
|
|
path = environ.get('PATH_INFO', '')
|
||
|
|
replacement = self._get_replacement()
|
||
|
|
|
||
|
|
# Only rewrite non-static paths containing 'odoo'
|
||
|
|
if replacement and 'odoo' in path and '/web/static' not in path:
|
||
|
|
new_path = path.replace('odoo', replacement, 1) # Replace first occurrence only
|
||
|
|
environ['PATH_INFO'] = new_path
|
||
|
|
environ['REQUEST_URI'] = environ.get('REQUEST_URI', '').replace('odoo', replacement, 1)
|
||
|
|
|
||
|
|
return self.app(environ, start_response)
|