65 lines
2.3 KiB
Python
65 lines
2.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2022 Fabien Bourgeois <fabien@yaltik.com>
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU Affero General Public License as
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
# License, or (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU Affero General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
""" Override of databse filtering to allow new features """
|
|
|
|
from re import match
|
|
from json import loads
|
|
from odoo import http
|
|
from odoo.tools import config
|
|
|
|
db_filter_odoo = http.db_filter
|
|
|
|
# Cache usage
|
|
db_filter_dict = {}
|
|
all_hosts = []
|
|
db_filter_str = config.get('database_from_hosts', False)
|
|
if db_filter_str:
|
|
for entry in db_filter_str.split('|'):
|
|
database, hosts = entry.split(':')
|
|
hosts = hosts.split(',')
|
|
db_filter_dict[database] = hosts
|
|
all_hosts += hosts
|
|
http_host_resolv = {}
|
|
|
|
def db_filter(dbs, httprequest=None):
|
|
""" Override db_filter """
|
|
if db_filter_str:
|
|
httprequest = httprequest or http.request.httprequest
|
|
http_host = httprequest.environ.get('HTTP_HOST', '').split(':')[0]
|
|
# If in cache and not empty
|
|
if http_host in http_host_resolv and http_host_resolv.get(http_host):
|
|
dbs = http_host_resolv[http_host]
|
|
elif http_host in all_hosts:
|
|
for database, hosts in db_filter_dict.items():
|
|
for host in hosts:
|
|
if host == http_host:
|
|
dbs = [i for i in dbs if match(database, i)]
|
|
break
|
|
if http_host not in http_host_resolv:
|
|
http_host_resolv[http_host] = dbs
|
|
else:
|
|
dbs = db_filter_odoo(dbs, httprequest)
|
|
else:
|
|
dbs = db_filter_odoo(dbs, httprequest)
|
|
return dbs
|
|
|
|
|
|
if (config.get('proxy_mode')
|
|
and 'dbfilter_from_config' in config.get('server_wide_modules')):
|
|
http.db_filter = db_filter
|