Skip to content
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

Only pass public versions to html context #6118

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api/v2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ Version detail
"active": true,
"type": "tag",
"identifier": "3a6b3995c141c0888af6591a59240ba5db7d9914",
"privacy_level": "public",
"downloads": {
"pdf": "//readthedocs.org/projects/pip/downloads/pdf/stable/",
"htmlzip": "//readthedocs.org/projects/pip/downloads/htmlzip/stable/",
Expand Down
1 change: 1 addition & 0 deletions readthedocs/api/v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class Meta:
'slug',
'identifier',
'verbose_name',
'privacy_level',
'active',
'built',
'downloads',
Expand Down
17 changes: 15 additions & 2 deletions readthedocs/doc_builder/backends/sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from readthedocs.api.v2.client import api
from readthedocs.builds import utils as version_utils
from readthedocs.projects.constants import PUBLIC
from readthedocs.projects.exceptions import ProjectConfigurationError
from readthedocs.projects.models import Feature
from readthedocs.projects.utils import safe_write
Expand Down Expand Up @@ -112,10 +113,22 @@ def get_config_params(self):

# Avoid hitting database and API if using Docker build environment
if settings.DONT_HIT_API:
versions = self.project.active_versions()
if self.project.has_feature(Feature.ALL_VERSIONS_IN_HTML_CONTEXT):
versions = self.project.active_versions()
else:
versions = self.project.active_versions().filter(
privacy_level=PUBLIC,
)
downloads = self.version.get_downloads(pretty=True)
else:
versions = self.project.api_versions()
if self.project.has_feature(Feature.ALL_VERSIONS_IN_HTML_CONTEXT):
versions = self.project.api_versions()
else:
versions = [
v
for v in self.project.api_versions()
if v.privacy_level == PUBLIC
]
downloads = api.version(self.version.pk).get()['downloads']

data = {
Expand Down
12 changes: 9 additions & 3 deletions readthedocs/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -932,8 +932,7 @@ def get_latest_build(self, finished=True):
def api_versions(self):
from readthedocs.builds.models import APIVersion
ret = []
for version_data in api.project(self.pk
).active_versions.get()['versions']:
for version_data in api.project(self.pk).active_versions.get()['versions']:
version = APIVersion(**version_data)
ret.append(version)
return sort_version_aware(ret)
Expand Down Expand Up @@ -1480,6 +1479,7 @@ def add_features(sender, **kwargs):
EXTERNAL_VERSION_BUILD = 'external_version_build'
UPDATE_CONDA_STARTUP = 'update_conda_startup'
CONDA_APPEND_CORE_REQUIREMENTS = 'conda_append_core_requirements'
ALL_VERSIONS_IN_HTML_CONTEXT = 'all_versions_in_html_context'

FEATURES = (
(USE_SPHINX_LATEST, _('Use latest version of Sphinx')),
Expand Down Expand Up @@ -1531,7 +1531,13 @@ def add_features(sender, **kwargs):
CONDA_APPEND_CORE_REQUIREMENTS,
_('Append Read the Docs core requirements to environment.yml file'),
),

(
ALL_VERSIONS_IN_HTML_CONTEXT,
_(
'Pass all versions (including private) into the html context '
'when building with Sphinx'
),
),
)

projects = models.ManyToManyField(
Expand Down
1 change: 1 addition & 0 deletions readthedocs/rtd_tests/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2170,6 +2170,7 @@ def test_get_version_by_id(self):
'use_system_packages': False,
'users': [1],
},
'privacy_level': 'public',
'downloads': {},
'identifier': '2404a34eba4ee9c48cc8bc4055b99a48354f4950',
'slug': '0.8',
Expand Down
55 changes: 54 additions & 1 deletion readthedocs/rtd_tests/tests/test_doc_builder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
import os
import tempfile
from collections import namedtuple
Expand All @@ -17,6 +16,7 @@
from readthedocs.doc_builder.backends.sphinx import BaseSphinx
from readthedocs.doc_builder.exceptions import MkDocsYAMLParseError
from readthedocs.doc_builder.python_environments import Virtualenv
from readthedocs.projects.constants import PRIVATE, PROTECTED, PUBLIC
from readthedocs.projects.exceptions import ProjectConfigurationError
from readthedocs.projects.models import Feature, Project

Expand Down Expand Up @@ -69,6 +69,59 @@ def test_conf_py_path(self, checkout_path, docs_dir):
expected,
)

@patch('readthedocs.doc_builder.backends.sphinx.api')
@patch('readthedocs.projects.models.api')
@patch('readthedocs.doc_builder.backends.sphinx.BaseSphinx.docs_dir')
@patch('readthedocs.builds.models.Version.get_conf_py_path')
@patch('readthedocs.projects.models.Project.checkout_path')
def test_html_context_only_has_public_versions(
self, checkout_path, get_conf_py_path,
docs_dir, api_project, api_version,
):
tmp_dir = tempfile.mkdtemp()
checkout_path.return_value = tmp_dir
docs_dir.return_value = tmp_dir
get_conf_py_path.side_effect = ProjectConfigurationError

api_version.version().get.return_value = {'downloads': []}
api_project.project().active_versions.get.return_value = {
'versions': [
{
'slug': 'v1',
'privacy_level': PUBLIC,
},
{
'slug': 'v2',
'privacy_level': PUBLIC,
},
{
'slug': 'v3',
'privacy_level': PROTECTED,
},
{
'slug': 'latest',
'privacy_level': PRIVATE,
},
],
}

python_env = Virtualenv(
version=self.version,
build_env=self.build_env,
config=None,
)
base_sphinx = BaseSphinx(
build_env=self.build_env,
python_env=python_env,
)
base_sphinx.config_file = tempfile.mktemp()
context = base_sphinx.get_config_params()
versions = {
v.slug
for v in context['versions']
}
self.assertEqual(versions, {'v1', 'v2'})

@patch('readthedocs.doc_builder.backends.sphinx.BaseSphinx.docs_dir')
@patch('readthedocs.doc_builder.backends.sphinx.BaseSphinx.create_index')
@patch('readthedocs.doc_builder.backends.sphinx.BaseSphinx.get_config_params')
Expand Down