From 360a5dff9102676990946c790abdd025401048a1 Mon Sep 17 00:00:00 2001 From: Will Thompson Date: Thu, 14 May 2015 17:11:40 +0100 Subject: [PATCH 2/2] Fix findEntities( ... name == x ... ) --- pandora/entity/managers.py | 15 ++++++++++++++- pandora/entity/views.py | 11 +++-------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/pandora/entity/managers.py b/pandora/entity/managers.py index e315194..47b725f 100644 --- a/pandora/entity/managers.py +++ b/pandora/entity/managers.py @@ -5,6 +5,17 @@ from django.db.models import Q, Manager import ox from ox.django.query import QuerySet + +def namePredicate(op, value): + pat = { + '==': '|%s|', + '^': '|%s', + '$': '%s|', + }.get(op, '%s') + + return ('name_find__icontains', pat % value) + + def parseCondition(condition, user, item=None): ''' ''' @@ -29,10 +40,12 @@ def parseCondition(condition, user, item=None): return Q(**{k: v}) if isinstance(v, bool): #featured and public flag key = k + elif k == 'name_find': + key, v = namePredicate(op, v) else: if k == '*': k = 'find__value' - elif k not in ('name_find', 'id', 'user__username', 'type'): + elif k not in ('id', 'user__username', 'type'): find_key = k k = 'find__value' key = "%s%s" % (k, { diff --git a/pandora/entity/views.py b/pandora/entity/views.py index 626626e..762b1b6 100644 --- a/pandora/entity/views.py +++ b/pandora/entity/views.py @@ -19,6 +19,7 @@ from itemlist.models import List from changelog.models import add_changelog import models +from managers import namePredicate def get_entity_or_404_json(id): try: @@ -119,14 +120,8 @@ def autocompleteEntities(request, data): qs = models.Entity.objects.filter(type=data['key']) if data['value']: - if op == '=': - qs = qs.filter(name_find__icontains=data['value']) - elif op == '==': - qs = qs.filter(name_find__icontains=u'|%s|'%data['value']) - elif op == '^': - qs = qs.filter(name_find__icontains=u'|%s'%data['value']) - elif op == '$': - qs = qs.filter(name_find__icontains=u'%s|'%data['value']) + k, v = namePredicate(op, data['value']) + qs = qs.filter(**{k: v}) qs = qs.order_by(order_by) if op != '$': value_lower = data['value'].lower() -- 2.1.0