Add caching functions and enhance item retrieval in templates; implement dynamic field selection for list items
This commit is contained in:
parent
7e24aa0585
commit
09cfcee8b3
5 changed files with 110 additions and 7 deletions
|
|
@ -1,5 +1,57 @@
|
|||
from flask import Blueprint
|
||||
from flask import Blueprint, g
|
||||
|
||||
main = Blueprint('main', __name__)
|
||||
|
||||
from . import inventory, user, worklog, settings, index, search, hooks
|
||||
from . import inventory, user, worklog, settings, index, search, hooks
|
||||
from .. import db
|
||||
|
||||
def _cell_cache():
|
||||
if not hasattr(g, '_cell_cache'):
|
||||
g._cell_cache = {}
|
||||
return g._cell_cache
|
||||
|
||||
def _rows_cache():
|
||||
if not hasattr(g, '_rows_cache'):
|
||||
g._rows_cache = {}
|
||||
return g._rows_cache
|
||||
|
||||
@main.app_template_global()
|
||||
def cell(model_name: str, id_: int, field: str, default: str = ""):
|
||||
from ..ui.blueprint import get_model_class, call
|
||||
from ..ui.defaults import default_value
|
||||
key = (model_name, int(id_), field)
|
||||
cache = _cell_cache()
|
||||
if key in cache:
|
||||
return cache[key]
|
||||
|
||||
try:
|
||||
Model = get_model_class(model_name)
|
||||
val = call(Model, 'ui_value', db.session, id_=id_, field=field)
|
||||
if val is None:
|
||||
val = default_value(db.session, Model, id_=id_, field=field)
|
||||
except Exception:
|
||||
val = default
|
||||
if val is None:
|
||||
val = default
|
||||
cache[key] = val
|
||||
return val
|
||||
|
||||
@main.app_template_global()
|
||||
def cells(model_name: str, id_: int, *fields: str):
|
||||
from ..ui.blueprint import get_model_class, call
|
||||
from ..ui.defaults import default_values
|
||||
fields = [f for f in fields if f]
|
||||
key = (model_name, int(id_), tuple(fields))
|
||||
cache = _cell_cache()
|
||||
if key in cache:
|
||||
return cache[key]
|
||||
|
||||
try:
|
||||
Model = get_model_class(model_name)
|
||||
data = call(Model, 'ui_values', db.session, id_=int(id_), fields=fields)
|
||||
if data is None:
|
||||
data = default_values(db.session, Model, id_=int(id_), fields=fields)
|
||||
except Exception:
|
||||
data = {f: None for f in fields}
|
||||
cache[key] = data
|
||||
return data
|
||||
Loading…
Add table
Add a link
Reference in a new issue