Fix bugs in alias-searching for objects. Resolves #1048.

This commit is contained in:
Griatch 2016-10-13 21:53:54 +02:00
parent fb96b44324
commit f59c9fd573

View file

@ -253,7 +253,7 @@ class ObjectDBManager(TypedObjectManager):
# build query objects # build query objects
candidates_id = [_GA(obj, "id") for obj in make_iter(candidates) if obj] candidates_id = [_GA(obj, "id") for obj in make_iter(candidates) if obj]
cand_restriction = candidates != None and Q(pk__in=make_iter(candidates_id)) or Q() cand_restriction = candidates != None and Q(pk__in=candidates_id) or Q()
type_restriction = typeclasses and Q(db_typeclass_path__in=make_iter(typeclasses)) or Q() type_restriction = typeclasses and Q(db_typeclass_path__in=make_iter(typeclasses)) or Q()
if exact: if exact:
# exact match - do direct search # exact match - do direct search
@ -261,23 +261,31 @@ class ObjectDBManager(TypedObjectManager):
Q(db_tags__db_key__iexact=ostring) & Q(db_tags__db_tagtype__iexact="alias"))).distinct() Q(db_tags__db_key__iexact=ostring) & Q(db_tags__db_tagtype__iexact="alias"))).distinct()
elif candidates: elif candidates:
# fuzzy with candidates # fuzzy with candidates
key_candidates = self.filter(cand_restriction & type_restriction) search_candidates = self.filter(cand_restriction & type_restriction)
else: else:
# fuzzy without supplied candidates - we select our own candidates # fuzzy without supplied candidates - we select our own candidates
key_candidates = self.filter(type_restriction & (Q(db_key__istartswith=ostring) | Q(db_tags__db_key__istartswith=ostring))).distinct() search_candidates = self.filter(type_restriction & (Q(db_key__istartswith=ostring) | Q(db_tags__db_key__istartswith=ostring))).distinct()
candidates_id = [_GA(obj, "id") for obj in key_candidates]
# fuzzy matching # fuzzy matching
key_strings = key_candidates.values_list("db_key", flat=True).order_by("id") key_strings = search_candidates.values_list("db_key", flat=True).order_by("id")
index_matches = string_partial_matching(key_strings, ostring, ret_index=True) index_matches = string_partial_matching(key_strings, ostring, ret_index=True)
if index_matches: if index_matches:
return [obj for ind, obj in enumerate(key_candidates) if ind in index_matches] # a match by key
return [obj for ind, obj in enumerate(search_candidates) if ind in index_matches]
else: else:
alias_candidates = self.filter(id__in=candidates_id, db_tags__db_tagtype__iexact="alias") # match by alias rather than by key
alias_strings = alias_candidates.values_list("db_key", flat=True) search_candidates = search_candidates.filter(db_tags__db_tagtype__iexact="alias",
db_tags__db_key__icontains=ostring)
alias_strings = []
alias_candidates = []
#TODO create the alias_strings and alias_candidates lists more effiently?
for candidate in search_candidates:
for alias in candidate.aliases.all():
alias_strings.append(alias)
alias_candidates.append(candidate)
index_matches = string_partial_matching(alias_strings, ostring, ret_index=True) index_matches = string_partial_matching(alias_strings, ostring, ret_index=True)
if index_matches: if index_matches:
return [alias.db_obj for ind, alias in enumerate(alias_candidates) if ind in index_matches] return [alias_candidates[ind] for ind in index_matches]
return [] return []
# main search methods and helper functions # main search methods and helper functions