Skip to content

Commit 2e67b7d

Browse files
authored
Merge pull request #6118 from stsewd/only-pass-public-versions-to-html-context
Only pass public versions to html context
2 parents 8798752 + 4ceb01e commit 2e67b7d

File tree

6 files changed

+81
-6
lines changed

6 files changed

+81
-6
lines changed

docs/api/v2.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ Version detail
190190
"active": true,
191191
"type": "tag",
192192
"identifier": "3a6b3995c141c0888af6591a59240ba5db7d9914",
193+
"privacy_level": "public",
193194
"downloads": {
194195
"pdf": "//readthedocs.org/projects/pip/downloads/pdf/stable/",
195196
"htmlzip": "//readthedocs.org/projects/pip/downloads/htmlzip/stable/",

readthedocs/api/v2/serializers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class Meta:
8686
'slug',
8787
'identifier',
8888
'verbose_name',
89+
'privacy_level',
8990
'active',
9091
'built',
9192
'downloads',

readthedocs/doc_builder/backends/sphinx.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from readthedocs.api.v2.client import api
2020
from readthedocs.builds import utils as version_utils
21+
from readthedocs.projects.constants import PUBLIC
2122
from readthedocs.projects.exceptions import ProjectConfigurationError
2223
from readthedocs.projects.models import Feature
2324
from readthedocs.projects.utils import safe_write
@@ -112,10 +113,22 @@ def get_config_params(self):
112113

113114
# Avoid hitting database and API if using Docker build environment
114115
if settings.DONT_HIT_API:
115-
versions = self.project.active_versions()
116+
if self.project.has_feature(Feature.ALL_VERSIONS_IN_HTML_CONTEXT):
117+
versions = self.project.active_versions()
118+
else:
119+
versions = self.project.active_versions().filter(
120+
privacy_level=PUBLIC,
121+
)
116122
downloads = self.version.get_downloads(pretty=True)
117123
else:
118-
versions = self.project.api_versions()
124+
if self.project.has_feature(Feature.ALL_VERSIONS_IN_HTML_CONTEXT):
125+
versions = self.project.api_versions()
126+
else:
127+
versions = [
128+
v
129+
for v in self.project.api_versions()
130+
if v.privacy_level == PUBLIC
131+
]
119132
downloads = api.version(self.version.pk).get()['downloads']
120133

121134
data = {

readthedocs/projects/models.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -932,8 +932,7 @@ def get_latest_build(self, finished=True):
932932
def api_versions(self):
933933
from readthedocs.builds.models import APIVersion
934934
ret = []
935-
for version_data in api.project(self.pk
936-
).active_versions.get()['versions']:
935+
for version_data in api.project(self.pk).active_versions.get()['versions']:
937936
version = APIVersion(**version_data)
938937
ret.append(version)
939938
return sort_version_aware(ret)
@@ -1480,6 +1479,7 @@ def add_features(sender, **kwargs):
14801479
EXTERNAL_VERSION_BUILD = 'external_version_build'
14811480
UPDATE_CONDA_STARTUP = 'update_conda_startup'
14821481
CONDA_APPEND_CORE_REQUIREMENTS = 'conda_append_core_requirements'
1482+
ALL_VERSIONS_IN_HTML_CONTEXT = 'all_versions_in_html_context'
14831483

14841484
FEATURES = (
14851485
(USE_SPHINX_LATEST, _('Use latest version of Sphinx')),
@@ -1531,7 +1531,13 @@ def add_features(sender, **kwargs):
15311531
CONDA_APPEND_CORE_REQUIREMENTS,
15321532
_('Append Read the Docs core requirements to environment.yml file'),
15331533
),
1534-
1534+
(
1535+
ALL_VERSIONS_IN_HTML_CONTEXT,
1536+
_(
1537+
'Pass all versions (including private) into the html context '
1538+
'when building with Sphinx'
1539+
),
1540+
),
15351541
)
15361542

15371543
projects = models.ManyToManyField(

readthedocs/rtd_tests/tests/test_api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,6 +2170,7 @@ def test_get_version_by_id(self):
21702170
'use_system_packages': False,
21712171
'users': [1],
21722172
},
2173+
'privacy_level': 'public',
21732174
'downloads': {},
21742175
'identifier': '2404a34eba4ee9c48cc8bc4055b99a48354f4950',
21752176
'slug': '0.8',

readthedocs/rtd_tests/tests/test_doc_builder.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
import os
32
import tempfile
43
from collections import namedtuple
@@ -17,6 +16,7 @@
1716
from readthedocs.doc_builder.backends.sphinx import BaseSphinx
1817
from readthedocs.doc_builder.exceptions import MkDocsYAMLParseError
1918
from readthedocs.doc_builder.python_environments import Virtualenv
19+
from readthedocs.projects.constants import PRIVATE, PROTECTED, PUBLIC
2020
from readthedocs.projects.exceptions import ProjectConfigurationError
2121
from readthedocs.projects.models import Feature, Project
2222

@@ -69,6 +69,59 @@ def test_conf_py_path(self, checkout_path, docs_dir):
6969
expected,
7070
)
7171

72+
@patch('readthedocs.doc_builder.backends.sphinx.api')
73+
@patch('readthedocs.projects.models.api')
74+
@patch('readthedocs.doc_builder.backends.sphinx.BaseSphinx.docs_dir')
75+
@patch('readthedocs.builds.models.Version.get_conf_py_path')
76+
@patch('readthedocs.projects.models.Project.checkout_path')
77+
def test_html_context_only_has_public_versions(
78+
self, checkout_path, get_conf_py_path,
79+
docs_dir, api_project, api_version,
80+
):
81+
tmp_dir = tempfile.mkdtemp()
82+
checkout_path.return_value = tmp_dir
83+
docs_dir.return_value = tmp_dir
84+
get_conf_py_path.side_effect = ProjectConfigurationError
85+
86+
api_version.version().get.return_value = {'downloads': []}
87+
api_project.project().active_versions.get.return_value = {
88+
'versions': [
89+
{
90+
'slug': 'v1',
91+
'privacy_level': PUBLIC,
92+
},
93+
{
94+
'slug': 'v2',
95+
'privacy_level': PUBLIC,
96+
},
97+
{
98+
'slug': 'v3',
99+
'privacy_level': PROTECTED,
100+
},
101+
{
102+
'slug': 'latest',
103+
'privacy_level': PRIVATE,
104+
},
105+
],
106+
}
107+
108+
python_env = Virtualenv(
109+
version=self.version,
110+
build_env=self.build_env,
111+
config=None,
112+
)
113+
base_sphinx = BaseSphinx(
114+
build_env=self.build_env,
115+
python_env=python_env,
116+
)
117+
base_sphinx.config_file = tempfile.mktemp()
118+
context = base_sphinx.get_config_params()
119+
versions = {
120+
v.slug
121+
for v in context['versions']
122+
}
123+
self.assertEqual(versions, {'v1', 'v2'})
124+
72125
@patch('readthedocs.doc_builder.backends.sphinx.BaseSphinx.docs_dir')
73126
@patch('readthedocs.doc_builder.backends.sphinx.BaseSphinx.create_index')
74127
@patch('readthedocs.doc_builder.backends.sphinx.BaseSphinx.get_config_params')

0 commit comments

Comments
 (0)