Skip to content

Fix unbound var in search view #5794

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 2 commits into from
Jun 12, 2019
Merged
Changes from 1 commit
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
45 changes: 20 additions & 25 deletions readthedocs/search/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

"""Search views."""
import collections
import logging
Expand All @@ -10,9 +8,14 @@
from readthedocs.builds.constants import LATEST
from readthedocs.projects.models import Project
from readthedocs.search.faceted_search import (
AllSearch, ProjectSearch, PageSearch, DomainSearch, ALL_FACETS
ALL_FACETS,
AllSearch,
DomainSearch,
PageSearch,
ProjectSearch,
)


log = logging.getLogger(__name__)
LOG_TEMPLATE = '(Elastic Search) [%(user)s:%(type)s] [%(project)s:%(version)s:%(language)s] %(msg)s'

Expand Down Expand Up @@ -56,8 +59,17 @@ def elastic_search(request, project_slug=None):
role_name=request.GET.get('role_name'),
index=request.GET.get('index'),
)
search_facets = collections.defaultdict(
ProjectSearch,
{
'project': ProjectSearch,
'domain': DomainSearch,
'file': PageSearch,
'all': AllSearch,
}
)

results = ''
results = None
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels more like an object than an empty string

facets = {}

if user_input.query:
Expand All @@ -68,26 +80,9 @@ def elastic_search(request, project_slug=None):
if value:
kwargs[avail_facet] = value

if user_input.type == 'project':
search = ProjectSearch(
query=user_input.query, user=request.user, **kwargs
)

elif user_input.type == 'domain':
search = DomainSearch(
query=user_input.query, user=request.user, **kwargs
)

elif user_input.type == 'file':
search = PageSearch(
query=user_input.query, user=request.user, **kwargs
)

elif user_input.type == 'all':
search = AllSearch(
query=user_input.query, user=request.user, **kwargs
)

search = search_facets[user_input.type](
query=user_input.query, user=request.user, **kwargs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The solution with defaultdict is more readable to me, I like it. Although, there is a new logic hidden: if the type does not exist (for any reason) we are going to return ProjectSearch results. Is that OK? would be better to just return no results at all?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to add another condition next to the user_input.query

... and (user_input.type in search_facets):

and remove the lambda from the defaultdict.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

It seems we have the default set in multiples places then. I think we could just have only one.

)
results = search[:50].execute()
facets = results.facets

Expand All @@ -105,7 +100,7 @@ def elastic_search(request, project_slug=None):

# Make sure our selected facets are displayed even when they return 0 results
for avail_facet in ALL_FACETS:
value = getattr(user_input, avail_facet)
value = getattr(user_input, avail_facet, None)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if a default isn't given to getattr it raises an exception.

if not value or avail_facet not in facets:
continue
if value not in [val[0] for val in facets[avail_facet]]:
Expand Down