-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SearchFilter.get_search_terms
returns list.
#9338
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,15 +24,19 @@ def search_smart_split(search_terms): | |
"""generator that first splits string by spaces, leaving quoted phrases together, | ||
then it splits non-quoted phrases by commas. | ||
""" | ||
split_terms = [] | ||
for term in smart_split(search_terms): | ||
# trim commas to avoid bad matching for quoted phrases | ||
term = term.strip(',') | ||
if term.startswith(('"', "'")) and term[0] == term[-1]: | ||
# quoted phrases are kept together without any other split | ||
yield unescape_string_literal(term) | ||
split_terms.append(unescape_string_literal(term)) | ||
else: | ||
# non-quoted tokens are split by comma, keeping only non-empty ones | ||
yield from (sub_term.strip() for sub_term in term.split(',') if sub_term) | ||
for sub_term in term.split(','): | ||
if sub_term: | ||
split_terms.append(sub_term.strip()) | ||
return split_terms | ||
|
||
|
||
class BaseFilterBackend: | ||
|
@@ -85,7 +89,8 @@ def get_search_terms(self, request): | |
""" | ||
value = request.query_params.get(self.search_param, '') | ||
field = CharField(trim_whitespace=False, allow_blank=True) | ||
return field.run_validation(value) | ||
cleaned_value = field.run_validation(value) | ||
return search_smart_split(cleaned_value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could have been done by simply changing to return list(search_smart_split(cleaned_value)) There is no point to changing the return value and definition of |
||
|
||
def construct_search(self, field_name, queryset): | ||
lookup = self.lookup_prefixes.get(field_name[0]) | ||
|
@@ -163,7 +168,7 @@ def filter_queryset(self, request, queryset, view): | |
reduce( | ||
operator.or_, | ||
(models.Q(**{orm_lookup: term}) for orm_lookup in orm_lookups) | ||
) for term in search_smart_split(search_terms) | ||
) for term in search_terms | ||
) | ||
queryset = queryset.filter(reduce(operator.and_, conditions)) | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Came from your haiku
Just to add 🐼 comment
Docstring needs fix
(“generator” -> Create list)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll put up a PR for it!