work around new sqlalchemy limitations

This commit is contained in:
j 2024-06-08 12:45:21 +01:00
parent c14d250166
commit 60e17ab076
2 changed files with 14 additions and 8 deletions

View file

@ -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]

View file

@ -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