Skip to content

Commit 873564e

Browse files
authored
Merge pull request #1221 from TOMToolkit/1219-fuzzy-name-search-returns-attributeerror-no-attribute-make_simple_name
use updated fuzzy_name match
2 parents 69fb8e9 + 337e71f commit 873564e

2 files changed

Lines changed: 5 additions & 11 deletions

File tree

tom_targets/base_models.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,13 @@ def match_exact_name(self, name):
160160
queryset = super().get_queryset().filter(name=name)
161161
return queryset
162162

163-
def match_fuzzy_name(self, name):
163+
def match_fuzzy_name(self, name, input_queryset=None):
164164
"""
165165
Returns a queryset of targets with a name OR ALIAS that, when processed by ``simplify_name``, match a similarly
166166
processed version of the name that is received.
167167
168168
:param name: The string against which target names and aliases will be matched.
169+
:param input_queryset: Optional queryset to filter the results. If not provided, all targets will be considered.
169170
170171
:return: queryset containing matching Targets. Will return targets even when matched value is an alias.
171172
"""
@@ -175,7 +176,8 @@ def match_fuzzy_name(self, name):
175176
for alias in target.names:
176177
if self.simplify_name(alias) == simple_name:
177178
matching_names.append(target.name)
178-
queryset = self.get_queryset().filter(name__in=matching_names)
179+
initial_queryset = input_queryset or self.get_queryset()
180+
queryset = initial_queryset.filter(name__in=matching_names)
179181
return queryset
180182

181183
def simplify_name(self, name):

tom_targets/filters.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import django_filters
44

55
from tom_targets.models import Target, TargetList
6-
from tom_targets.base_models import TargetMatchManager
76
from tom_targets.utils import cone_search_filter
87

98

@@ -71,14 +70,7 @@ def filter_name_fuzzy(self, queryset, name, value):
7170
Return a queryset for targets with names or aliases fuzzy matching the given coma-separated list of terms.
7271
A fuzzy match is determined by the `make_simple_name` method of the `TargetMatchManager` class.
7372
"""
74-
matching_names = []
75-
for term in value.split(','):
76-
simple_name = TargetMatchManager.make_simple_name(self, term)
77-
for target in Target.objects.all().prefetch_related('aliases'):
78-
for alias in target.names:
79-
if TargetMatchManager.make_simple_name(self, alias) == simple_name:
80-
matching_names.append(target.name)
81-
return queryset.filter(name__in=matching_names).distinct()
73+
return Target.matches.match_fuzzy_name(value, queryset).distinct()
8274

8375
cone_search = django_filters.CharFilter(method='filter_cone_search', label='Cone Search',
8476
help_text='RA, Dec, Search Radius (degrees)')

0 commit comments

Comments
 (0)