tune find api
This commit is contained in:
parent
b0f0446fcb
commit
294ea33960
1 changed files with 12 additions and 11 deletions
|
@ -3,6 +3,7 @@
|
||||||
import json
|
import json
|
||||||
import hashlib
|
import hashlib
|
||||||
import os
|
import os
|
||||||
|
import unicodedata
|
||||||
|
|
||||||
from sqlalchemy.orm import load_only
|
from sqlalchemy.orm import load_only
|
||||||
from sqlalchemy import func
|
from sqlalchemy import func
|
||||||
|
@ -48,7 +49,7 @@ def find(data):
|
||||||
if g is None:
|
if g is None:
|
||||||
items = q['qs'].options(load_only('id'))
|
items = q['qs'].options(load_only('id'))
|
||||||
qs = models.Find.query.filter_by(key=q['group'])
|
qs = models.Find.query.filter_by(key=q['group'])
|
||||||
if items.count():
|
if items.first():
|
||||||
qs = qs.filter(models.Find.item_id.in_(items))
|
qs = qs.filter(models.Find.item_id.in_(items))
|
||||||
for f in qs.values('value', 'findvalue'):
|
for f in qs.values('value', 'findvalue'):
|
||||||
value = f[0]
|
value = f[0]
|
||||||
|
@ -64,12 +65,13 @@ def find(data):
|
||||||
sort_type = utils.get_by_id(settings.config['itemKeys'], q['group']).get('sortType')
|
sort_type = utils.get_by_id(settings.config['itemKeys'], q['group']).get('sortType')
|
||||||
def _sort_key(k):
|
def _sort_key(k):
|
||||||
if sort_type == 'person' and q['sort'][0]['key'] == 'name':
|
if sort_type == 'person' and q['sort'][0]['key'] == 'name':
|
||||||
return get_sort_name(k[q['sort'][0]['key']])
|
v = get_sort_name(k[q['sort'][0]['key']])
|
||||||
else:
|
else:
|
||||||
return k[q['sort'][0]['key']]
|
v = k[q['sort'][0]['key']]
|
||||||
g.sort(key=_sort_key)
|
if isinstance(v, str):
|
||||||
if q['sort'][0]['operator'] == '-':
|
v = unicodedata.normalize('NFKD', v).lower()
|
||||||
g.reverse()
|
return v
|
||||||
|
g.sort(key=_sort_key, reverse=q['sort'][0]['operator'] == '-')
|
||||||
state.cache.set(key, g)
|
state.cache.set(key, g)
|
||||||
if 'positions' in data:
|
if 'positions' in data:
|
||||||
response['positions'] = {}
|
response['positions'] = {}
|
||||||
|
@ -86,12 +88,11 @@ def find(data):
|
||||||
ids = [i.id for i in q['qs'].options(load_only('id'))]
|
ids = [i.id for i in q['qs'].options(load_only('id'))]
|
||||||
response['positions'] = utils.get_positions(ids, data['positions'])
|
response['positions'] = utils.get_positions(ids, data['positions'])
|
||||||
elif 'keys' in data:
|
elif 'keys' in data:
|
||||||
response['items'] = []
|
response['items'] = [
|
||||||
for i in q['qs'][q['range'][0]:q['range'][1]]:
|
i.json(data['keys']) for i in q['qs'][q['range'][0]:q['range'][1]]
|
||||||
j = i.json()
|
]
|
||||||
response['items'].append({k:j[k] for k in j if not data['keys'] or k in data['keys']})
|
|
||||||
else:
|
else:
|
||||||
size = [i.info.get('size', 0) for i in q['qs'].join(models.Sort).options(load_only('id', 'info'))]
|
size = [i.info.get('size', 0) for i in q['qs'].options(load_only('id', 'info'))]
|
||||||
response['items'] = len(size)
|
response['items'] = len(size)
|
||||||
response['size'] = sum(size)
|
response['size'] = sum(size)
|
||||||
return response
|
return response
|
||||||
|
|
Loading…
Reference in a new issue