utils: cache get_warehouses() result based on schema files paths (#38067)

This commit is contained in:
Benjamin Dauvergne 2019-11-29 15:24:35 +01:00
parent 9cc619ee0d
commit 33a8eabdba
1 changed files with 21 additions and 4 deletions

View File

@ -21,22 +21,39 @@ import json
from django.conf import settings
from django.db import connection
from django.utils.translation import ugettext as _
try:
from functools import lru_cache
except ImportError:
from django.utils.lru_cache import lru_cache
from .schemas import Warehouse
def get_warehouses():
warehouses = []
def get_warehouses_paths():
for pattern in settings.BIJOE_SCHEMAS:
for path in glob.glob(pattern):
warehouses.append(Warehouse.from_json(json.load(open(path))))
yield path
if hasattr(connection, 'tenant'):
pattern = os.path.join(connection.tenant.get_directory(), 'schemas', '*.model')
for path in glob.glob(pattern):
warehouses.append(Warehouse.from_json(json.load(open(path))))
yield path
@lru_cache()
def get_warehouses_by_paths(paths):
warehouses = []
for path in paths:
d = json.load(open(path))
warehouses.append(Warehouse.from_json(d))
return warehouses
def get_warehouses():
paths = frozenset(get_warehouses_paths())
return get_warehouses_by_paths(paths)
def human_join(l):
if not l:
return ''