support query in autocomplete for keys other than title
This commit is contained in:
parent
788c122cc7
commit
c216716493
1 changed files with 13 additions and 3 deletions
|
@ -292,14 +292,17 @@ def autocomplete(request):
|
||||||
param data
|
param data
|
||||||
key
|
key
|
||||||
value
|
value
|
||||||
operator '', '^', '$'
|
operator '=', '==', '^', '$'
|
||||||
|
query
|
||||||
range
|
range
|
||||||
return
|
return
|
||||||
|
|
||||||
|
query can be an item query to limit results
|
||||||
'''
|
'''
|
||||||
data = json.loads(request.POST['data'])
|
data = json.loads(request.POST['data'])
|
||||||
if not 'range' in data:
|
if not 'range' in data:
|
||||||
data['range'] = [0, 10]
|
data['range'] = [0, 10]
|
||||||
op = data.get('operator', '')
|
op = data.get('operator', '==')
|
||||||
|
|
||||||
key = settings.CONFIG['keys'][data['key']]
|
key = settings.CONFIG['keys'][data['key']]
|
||||||
order_by = key.get('autocompleteSortKey', False)
|
order_by = key.get('autocompleteSortKey', False)
|
||||||
|
@ -313,6 +316,8 @@ def autocomplete(request):
|
||||||
if data['value']:
|
if data['value']:
|
||||||
if op == '':
|
if op == '':
|
||||||
qs = qs.filter(find__key=data['key'], find__value__icontains=data['value'])
|
qs = qs.filter(find__key=data['key'], find__value__icontains=data['value'])
|
||||||
|
elif op == '==':
|
||||||
|
qs = qs.filter(find__key=data['key'], find__value__iexact=data['value'])
|
||||||
elif op == '^':
|
elif op == '^':
|
||||||
qs = qs.filter(find__key=data['key'], find__value__istartswith=data['value'])
|
qs = qs.filter(find__key=data['key'], find__value__istartswith=data['value'])
|
||||||
elif op == '$':
|
elif op == '$':
|
||||||
|
@ -324,12 +329,17 @@ def autocomplete(request):
|
||||||
else:
|
else:
|
||||||
qs = models.Facet.objects.filter(key=data['key'])
|
qs = models.Facet.objects.filter(key=data['key'])
|
||||||
if data['value']:
|
if data['value']:
|
||||||
if op == '':
|
if op == '=':
|
||||||
qs = qs.filter(value__icontains=data['value'])
|
qs = qs.filter(value__icontains=data['value'])
|
||||||
|
elif op == '==':
|
||||||
|
qs = qs.filter(value__iexact=data['value'])
|
||||||
elif op == '^':
|
elif op == '^':
|
||||||
qs = qs.filter(value__istartswith=data['value'])
|
qs = qs.filter(value__istartswith=data['value'])
|
||||||
elif op == '$':
|
elif op == '$':
|
||||||
qs = qs.filter(value__iendswith=data['value'])
|
qs = qs.filter(value__iendswith=data['value'])
|
||||||
|
if 'query' in data:
|
||||||
|
item_query = parse_query({'query': data.get('query', {})}, request.user)['qs']
|
||||||
|
qs = qs.filter(item__in=item_query)
|
||||||
qs = qs.values('value').annotate(items=Count('id'))
|
qs = qs.values('value').annotate(items=Count('id'))
|
||||||
qs = qs.order_by(order_by)
|
qs = qs.order_by(order_by)
|
||||||
qs = qs[data['range'][0]:data['range'][1]]
|
qs = qs[data['range'][0]:data['range'][1]]
|
||||||
|
|
Loading…
Reference in a new issue