Skip to content

Commit 7bcb3e1

Browse files
authored
Merge pull request #5805 from saadmk11/remove-pr-from-es
Remove PR Versions from Elasticsearch Indexing and HTMLFile Queryset Added
2 parents 12f6658 + 6fd5f68 commit 7bcb3e1

File tree

8 files changed

+115
-6
lines changed

8 files changed

+115
-6
lines changed

readthedocs/core/views/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def get_context_data(self, **kwargs):
6161

6262

6363
def random_page(request, project_slug=None): # pylint: disable=unused-argument
64-
html_file = HTMLFile.objects.order_by('?')
64+
html_file = HTMLFile.objects.internal().order_by('?')
6565
if project_slug:
6666
html_file = html_file.filter(project__slug=project_slug)
6767
html_file = html_file.first()

readthedocs/projects/managers.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
from django.db import models
22

3+
from readthedocs.core.utils.extend import (
4+
get_override_class,
5+
SettingsOverrideObject
6+
)
7+
from readthedocs.projects.querysets import HTMLFileQuerySet
38

4-
class HTMLFileManager(models.Manager):
9+
10+
class HTMLFileManagerBase(models.Manager):
11+
12+
@classmethod
13+
def from_queryset(cls, queryset_class, class_name=None):
14+
queryset_class = get_override_class(
15+
HTMLFileQuerySet,
16+
HTMLFileQuerySet._default_class, # pylint: disable=protected-access
17+
)
18+
return super().from_queryset(queryset_class, class_name)
519

620
def get_queryset(self):
721
return super().get_queryset().filter(name__endswith='.html')
22+
23+
24+
class HTMLFileManager(SettingsOverrideObject):
25+
_default_class = HTMLFileManagerBase

readthedocs/projects/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
FeatureQuerySet,
3232
ProjectQuerySet,
3333
RelatedProjectQuerySet,
34+
HTMLFileQuerySet,
3435
)
3536
from readthedocs.projects.templatetags.projects_tags import sort_version_aware
3637
from readthedocs.projects.validators import (
@@ -1216,7 +1217,7 @@ class HTMLFile(ImportedFile):
12161217
class Meta:
12171218
proxy = True
12181219

1219-
objects = HTMLFileManager()
1220+
objects = HTMLFileManager.from_queryset(HTMLFileQuerySet)()
12201221

12211222
def get_processed_json(self):
12221223
"""

readthedocs/projects/querysets.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from django.db.models import Q, OuterRef, Subquery, Prefetch
55
from guardian.shortcuts import get_objects_for_user
66

7+
from readthedocs.builds.constants import EXTERNAL
78
from readthedocs.core.utils.extend import SettingsOverrideObject
89

910
from . import constants
@@ -213,3 +214,27 @@ def for_project(self, project):
213214
Q(projects=project) |
214215
Q(default_true=True, add_date__gt=project.pub_date),
215216
).distinct()
217+
218+
219+
class HTMLFileQuerySetBase(models.QuerySet):
220+
221+
def internal(self):
222+
"""
223+
HTMLFileQuerySet method that only includes internal version html files.
224+
225+
It will exclude pull request/merge request Version html files from the queries
226+
and only include BRANCH, TAG, UNKONWN type Version html files.
227+
"""
228+
return self.exclude(version__type=EXTERNAL)
229+
230+
def external(self):
231+
"""
232+
HTMLFileQuerySet method that only includes external version html files.
233+
234+
It will only include pull request/merge request Version html files in the queries.
235+
"""
236+
return self.filter(version__type=EXTERNAL)
237+
238+
239+
class HTMLFileQuerySet(SettingsOverrideObject):
240+
_default_class = HTMLFileQuerySetBase

readthedocs/rtd_tests/tests/test_managers.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from readthedocs.builds.constants import EXTERNAL, BRANCH, TAG
66
from readthedocs.builds.models import Version, Build
77
from readthedocs.projects.constants import PUBLIC, PRIVATE, PROTECTED
8-
from readthedocs.projects.models import Project
8+
from readthedocs.projects.models import Project, HTMLFile
99

1010

1111
User = get_user_model()
@@ -211,3 +211,45 @@ def test_external_build_manager_with_public_with_user_and_project(self):
211211
def test_external_build_manager_with_api(self):
212212
self.assertNotIn(self.internal_builds, Build.external.api())
213213
self.assertIn(self.external_version_build, Build.external.api())
214+
215+
216+
class TestHTMLFileManager(TestCase):
217+
218+
fixtures = ['test_data']
219+
220+
def setUp(self):
221+
self.user = User.objects.create(username='test_user', password='test')
222+
self.client.login(username='test_user', password='test')
223+
self.pip = Project.objects.get(slug='pip')
224+
# Create a External Version. ie: pull/merge request Version.
225+
self.external_version = get(
226+
Version,
227+
project=self.pip,
228+
active=True,
229+
type=EXTERNAL,
230+
privacy_level=PUBLIC
231+
)
232+
self.html_file = HTMLFile.objects.create(
233+
project=self.pip,
234+
version=self.external_version,
235+
name='file.html',
236+
slug='file',
237+
path='file.html',
238+
md5='abcdef',
239+
commit='1234567890abcdef',
240+
)
241+
self.internal_html_files = HTMLFile.objects.exclude(version__type=EXTERNAL)
242+
243+
def test_internal_html_file_queryset(self):
244+
"""
245+
It will exclude pull/merge request Version html files from the queries
246+
and only include BRANCH, TAG, UNKONWN type Version files.
247+
"""
248+
self.assertNotIn(self.html_file, HTMLFile.objects.internal())
249+
250+
def test_external_html_file_queryset(self):
251+
"""
252+
It will only include pull/merge request Version html files in the queries.
253+
"""
254+
self.assertNotIn(self.internal_html_files, HTMLFile.objects.external())
255+
self.assertIn(self.html_file, HTMLFile.objects.external())

readthedocs/search/documents.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def get_queryset(self):
157157

158158
# Do not index files that belong to non sphinx project
159159
# Also do not index certain files
160-
queryset = queryset.filter(
160+
queryset = queryset.internal().filter(
161161
project__documentation_type__contains='sphinx'
162162
)
163163

readthedocs/search/tests/test_api.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from django.core.urlresolvers import reverse
33
from django_dynamic_fixture import G
44

5-
65
from readthedocs.builds.models import Version
76
from readthedocs.projects.models import HTMLFile
87
from readthedocs.search.tests.utils import get_search_query_from_project_file
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import pytest
2+
3+
from readthedocs.builds.constants import EXTERNAL
4+
from readthedocs.projects.models import HTMLFile
5+
from readthedocs.search.documents import PageDocument
6+
7+
8+
@pytest.mark.django_db
9+
@pytest.mark.search
10+
class TestPageDocument:
11+
12+
def test_get_queryset_does_not_include_external_versions(self, project):
13+
# turn version into PR Version
14+
version = project.versions.all()[0]
15+
version.type = EXTERNAL
16+
version.save()
17+
18+
html_file = HTMLFile.objects.filter(
19+
project__slug=project.slug, version=version
20+
)
21+
qs = PageDocument().get_queryset()
22+
23+
assert qs.model == HTMLFile
24+
assert not set(html_file).issubset(set(qs))

0 commit comments

Comments
 (0)