-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
"""Search views.""" | ||
import collections | ||
import logging | ||
|
@@ -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' | ||
|
||
|
@@ -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( | ||
lambda: ProjectSearch, | ||
{ | ||
'project': ProjectSearch, | ||
'domain': DomainSearch, | ||
'file': PageSearch, | ||
'all': AllSearch, | ||
} | ||
) | ||
|
||
results = '' | ||
results = None | ||
facets = {} | ||
|
||
if user_input.query: | ||
|
@@ -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 | ||
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. 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 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. We may want to add another condition next to the
and remove the lambda from the defaultdict. 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. 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. 👍 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 | ||
|
||
|
@@ -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) | ||
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. if a default isn't given to |
||
if not value or avail_facet not in facets: | ||
continue | ||
if value not in [val[0] for val in facets[avail_facet]]: | ||
|
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.
This feels more like an object than an empty string