Skip to content

Search: Little refactor #7076

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

Merged
merged 1 commit into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions readthedocs/search/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,24 @@ def get_queryset(self):
# Validate all the required params are there
self.validate_query_params()
query = self.request.query_params.get('q', '')
kwargs = {'filter_by_user': False, 'filters': {}}
kwargs['filters']['project'] = [p.slug for p in self.get_all_projects()]
kwargs['filters']['version'] = self._get_version().slug
filters = {}
filters['project'] = [p.slug for p in self.get_all_projects()]
filters['version'] = self._get_version().slug

# Check to avoid searching all projects in case these filters are empty.
if not kwargs['filters']['project']:
if not filters['project']:
log.info("Unable to find a project to search")
return HTMLFile.objects.none()
if not kwargs['filters']['version']:
if not filters['version']:
log.info("Unable to find a version to search")
return HTMLFile.objects.none()

user = self.request.user
queryset = PageSearch(
query=query, user=user, **kwargs
query=query,
filters=filters,
user=self.request.user,
# We use a permission class to control authorization
filter_by_user=False,
)
return queryset

Expand Down
4 changes: 2 additions & 2 deletions readthedocs/search/tests/test_faceted_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_search_exact_match(self, client, project, case):
cased_query = getattr(query_text, case)
query = cased_query()

page_search = PageSearch(query=query, user='')
page_search = PageSearch(query=query)
results = page_search.execute()

assert len(results) == 1
Expand All @@ -37,7 +37,7 @@ def test_search_combined_result(self, client, project):
- Where `Foo` or `Bar` is present
"""
query = 'Elasticsearch Query'
page_search = PageSearch(query=query, user='')
page_search = PageSearch(query=query)
results = page_search.execute()
assert len(results) == 3

Expand Down
2 changes: 1 addition & 1 deletion readthedocs/search/tests/test_xss.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TestXSS:

def test_facted_page_xss(self, client, project):
query = 'XSS'
page_search = PageSearch(query=query, user='')
page_search = PageSearch(query=query)
results = page_search.execute()
expected = """
&lt;h3&gt;<span>XSS</span> exploit&lt;&#x2F;h3&gt;
Expand Down