-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Refactor search code #5197
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
Refactor search code #5197
Changes from 3 commits
47e3c1a
a84bb15
6f36acd
0bfa998
936465d
0496cf6
ab28491
617f47f
d0f6082
af8a28f
5f197ec
465628e
e3e245c
e909759
49098d7
07dc26b
e9f3f3f
d6d02da
336e674
eb65817
144ed0e
d314f58
03ad482
5dfc17a
fcc9620
701ecdb
f636737
7583f9a
747da90
6c317f1
e2655de
f457ff7
7b2013c
0741a25
85956e0
6ac2f35
e062191
8e4cc2b
8c7bda4
5b9f460
e2e271b
a993f08
d52b968
fc277fa
417ea45
80c58c7
72d867f
5f118ff
e263e69
1a3e146
fab9f42
0a06726
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 |
---|---|---|
|
@@ -21,19 +21,10 @@ def __init__(self, user, **kwargs): | |
but is used on the .com | ||
""" | ||
self.user = user | ||
if 'filter_user' in kwargs: | ||
self.filter_user = kwargs.pop('filter_user') | ||
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. nitpick: this can be written as
to avoid the |
||
super().__init__(**kwargs) | ||
|
||
def search(self): | ||
""" | ||
Filter out full content on search results. | ||
|
||
This was causing all of the indexed content to be returned, which was | ||
never used on the client side. | ||
""" | ||
s = super().search() | ||
s = s.source(exclude=['content', 'headers']) | ||
return s | ||
|
||
def query(self, search, query): | ||
""" | ||
Add query part to ``search`` when needed. | ||
|
@@ -42,6 +33,7 @@ def query(self, search, query): | |
""" | ||
search = super().query(search, query) | ||
search = search.highlight_options(encoder='html', number_of_fragments=3) | ||
search = search.source(exclude=['content', 'headers']) | ||
return search | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
from django.shortcuts import get_object_or_404 | ||
from django_elasticsearch_dsl.registries import registry | ||
|
||
from readthedocs.builds.models import Version | ||
from readthedocs.projects.models import Project | ||
|
||
|
||
|
@@ -15,14 +16,20 @@ | |
|
||
# TODO: Rewrite all the views using this in Class Based View, | ||
# and move this function to a mixin | ||
def get_project_list_or_404(project_slug, user): | ||
"""Return list of project and its subprojects.""" | ||
queryset = Project.objects.api(user).only('slug') | ||
|
||
project = get_object_or_404(queryset, slug=project_slug) | ||
subprojects = queryset.filter(superprojects__parent_id=project.id) | ||
def get_project_list_or_404(project_slug, user, version_slug=None): | ||
""" | ||
Return list of project and its subprojects. | ||
|
||
project_list = list(subprojects) + [project] | ||
It filters by Version privacy instead of Project privacy. | ||
""" | ||
# Support private projects with public versions | ||
project_list = [] | ||
main_project = get_object_or_404(Project.objects.all(), slug=project_slug) | ||
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. nitpick: no need to |
||
subprojects = Project.objects.filter(superprojects__parent_id=main_project.id) | ||
for project in list(subprojects) + [main_project]: | ||
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. Not sure, but I'm thinking that this for could be replaced by a query itself, like:
Use it if you consider it clearer. |
||
version = Version.objects.public(user).filter(project__slug=project.slug, slug=version_slug) | ||
if version.count(): | ||
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. nitpick: |
||
project_list.append(version[0].project) | ||
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. nitpick: I think using |
||
return 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.
nitpick: I'd like to come up with a better name for this. I'm thinking about
filter_by_user
which is a little more explicit.What we want to communicate here is "filter versions by users permissions" I suppose, but I didn't find a good name for that :(