-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Fix an issue where we were not properly filtering projects #5309
Conversation
This PR looks to break some tests around search. |
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.
Changes look good for now.
Although, we will need to find a better fix probably at self.get_all_projects
to return the projects we need under different authentication circumstances or filter this list of project from corporate after checking for permissions.
@@ -65,6 +65,10 @@ def get_queryset(self): | |||
kwargs = {'filter_by_user': False} | |||
kwargs['projects_list'] = [p.slug for p in self.get_all_projects()] | |||
kwargs['versions_list'] = self.request.query_params.get('version') | |||
if not kwargs['projects_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.
We want two check two things here, 1) if the key exists on the dictionary and 2) if the value of the key contains something. if not kwargs.get('projects_list'):
gives us these two checks
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.
We set the key 2 lines above, I don't know how it wouldn't exist.
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 got confused with the test failing, but it was a typo:
if not kwargs['projects_list']:
raise ValidationError("Unable to find a project to search")
> if not kwargs['version_list']:
E KeyError: 'version_list'
@@ -81,9 +81,9 @@ def faceted_search( | |||
} | |||
|
|||
filters = {} | |||
if projects_list: | |||
if projects_list is not None: |
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 think that we want to check that the value here is a list (via isinstance
).
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 think it may also be a queryset. An empty list doesn't help us here either, so perhaps we just keep this with the existing check?
I went ahead an pushed my suggested checkings for the lists. I think that should fix the tests --feel free to revert the commit if you think that the checking is not valid. |
readthedocs/search/documents.py
Outdated
@@ -81,9 +81,9 @@ def faceted_search( | |||
} | |||
|
|||
filters = {} | |||
if projects_list: | |||
if isinstance(projects_list) and projects_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.
Doesn't isinstance
need 2 arguments? What is it checking for an instance of?
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.
🤦♂️
You are right.
I QAed this and I get a validation error when using the search,
Summarizing, with this PR ES search does not work :/ I suppose that |
It works fine for me locally (after fixing the variable name :D) |
readthedocs/search/api.py
Outdated
@@ -65,6 +65,10 @@ def get_queryset(self): | |||
kwargs = {'filter_by_user': False} | |||
kwargs['projects_list'] = [p.slug for p in self.get_all_projects()] | |||
kwargs['versions_list'] = self.request.query_params.get('version') | |||
if not kwargs.get('project_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.
if not kwargs.get('project_list'): | |
if not kwargs.get('projects_list'): |
readthedocs/search/documents.py
Outdated
@@ -81,9 +81,9 @@ def faceted_search( | |||
} | |||
|
|||
filters = {} | |||
if projects_list: | |||
if isinstance(projects_list, list) and projects_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.
These feel like the wrong solution. I think we should just go back to the original logic. An empty list doesn't work when passed into ES either, so really we want to be checking for truthyness for it to be evaluated in a meaningful way.
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 checks that projects_list
is effectively a list and it's not empty (and projects_list
).
readthedocs/search/documents.py
Outdated
filters['project'] = projects_list | ||
if versions_list: | ||
if isinstance(versions_list, (list, str)) and versions_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.
This one was tricky because of the _list
suffix. versions_list
can be a list or just a latest
string, for example. I was expecting it to behave as projects_list
that it's a list of one element in that case.
OK, I made a lot of mistakes on this PR. I will revert all my changes and just fix the typo that you had originally. I'm sorry for the noise here. |
There were some edge cases where the `public` queryset method on Project’s and Version’s could return an empty list. This lead to not properly filtering the queryset in the docsearch API. Properly raise an exception in this case, so that we don’t return search results that aren’t properly filtered.
26fc020
to
b000284
Compare
QAed this again with the latest push (your changes plus the typo fix) and it works properly in corporate (ES does not run when accessing via Shared Link) and community. |
There were some edge cases where the
public
queryset method on Project’s and Version’s could return an empty list. This lead to not properly filtering the queryset in the docsearch API. Properly raise an exception in this case, so that we don’t return search results that aren’t properly filtered.