From 60e17ab0762bf797fdbe92b9c83c135268098b61 Mon Sep 17 00:00:00 2001 From: j Date: Sat, 8 Jun 2024 12:45:21 +0100 Subject: [PATCH] work around new sqlalchemy limitations --- oml/item/api.py | 11 ++++++++--- oml/queryparser.py | 11 ++++++----- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/oml/item/api.py b/oml/item/api.py index 4e94270..db9d102 100644 --- a/oml/item/api.py +++ b/oml/item/api.py @@ -5,7 +5,7 @@ import os import unicodedata from sqlalchemy.orm import load_only -from sqlalchemy.sql.expression import text +from sqlalchemy.sql.expression import text, column from sqlalchemy import func from oxtornado import actions @@ -58,8 +58,13 @@ def find(data): qs = models.Find.query.filter_by(key=q['group']) if items is None or items.first(): if items is not None: - qs = qs.filter(models.Find.item_id.in_(items)) - values = list(qs.values('value', 'findvalue', 'sortvalue')) + ids = [i[0] for i in items.with_entities(column('id'))] + qs = qs.filter(models.Find.item_id.in_(ids)) + values = list(qs.values( + column('value'), + column('findvalue'), + column('sortvalue'), + )) for f in values: value = f[0] findvalue = f[1] diff --git a/oml/queryparser.py b/oml/queryparser.py index 2091fb2..b6e7dcb 100644 --- a/oml/queryparser.py +++ b/oml/queryparser.py @@ -5,7 +5,7 @@ import unicodedata import sqlalchemy.orm.exc from sqlalchemy.sql import operators from sqlalchemy.orm import load_only -from sqlalchemy.sql.expression import text +from sqlalchemy.sql.expression import text, column import utils import settings @@ -14,6 +14,7 @@ from fulltext import find_fulltext import logging logger = logging.getLogger(__name__) + def get_operator(op, type='str'): return { 'str': { @@ -135,7 +136,8 @@ class Parser(object): q = get_operator(op)(self._find.findvalue, v) if k != '*': q &= (self._find.key == k) - ids = self._model.query.join(self._find).filter(q).options(load_only('id')) + ids = self._find.query.filter(q).with_entities(column('item_id')) + ids = [i[0] for i in ids] return self.in_ids(ids, exclude) elif k == 'list': nickname, name = v.split(':', 1) @@ -221,7 +223,7 @@ class Parser(object): for condition in conditions: if 'conditions' in condition: q = self.parse_conditions(condition['conditions'], - condition.get('operator', '&')) + condition.get('operator', '&')) else: q = self.parse_condition(condition) if isinstance(q, list): @@ -266,8 +268,7 @@ class Parser(object): qs = self._model.query #only include items that have hard metadata conditions = self.parse_conditions(data.get('query', {}).get('conditions', []), - data.get('query', {}).get('operator', '&')) + data.get('query', {}).get('operator', '&')) for c in conditions: qs = qs.filter(c) - #print(qs) return qs