From 6babea0d74f18dc0cf35cefc588b20a20e66bcdc Mon Sep 17 00:00:00 2001 From: Santos Gallegos Date: Wed, 28 Aug 2019 19:00:15 -0500 Subject: [PATCH 1/7] Add test --- .../rtd_tests/tests/test_doc_builder.py | 51 ++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/readthedocs/rtd_tests/tests/test_doc_builder.py b/readthedocs/rtd_tests/tests/test_doc_builder.py index ea57dbad780..98c1d4b3bdb 100644 --- a/readthedocs/rtd_tests/tests/test_doc_builder.py +++ b/readthedocs/rtd_tests/tests/test_doc_builder.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- import os import tempfile from collections import namedtuple @@ -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, PUBLIC from readthedocs.projects.exceptions import ProjectConfigurationError from readthedocs.projects.models import Feature, Project @@ -69,6 +69,55 @@ 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': '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') From 4da58096e2dbad77f7745671492a047d1a6e5221 Mon Sep 17 00:00:00 2001 From: Santos Gallegos Date: Wed, 28 Aug 2019 19:31:03 -0500 Subject: [PATCH 2/7] Add new field --- readthedocs/api/v2/serializers.py | 1 + readthedocs/rtd_tests/tests/test_api.py | 1 + 2 files changed, 2 insertions(+) diff --git a/readthedocs/api/v2/serializers.py b/readthedocs/api/v2/serializers.py index d153166e612..dddf48a540b 100644 --- a/readthedocs/api/v2/serializers.py +++ b/readthedocs/api/v2/serializers.py @@ -86,6 +86,7 @@ class Meta: 'slug', 'identifier', 'verbose_name', + 'privacy_level', 'active', 'built', 'downloads', diff --git a/readthedocs/rtd_tests/tests/test_api.py b/readthedocs/rtd_tests/tests/test_api.py index cfd1b66c815..0ef479bcf14 100644 --- a/readthedocs/rtd_tests/tests/test_api.py +++ b/readthedocs/rtd_tests/tests/test_api.py @@ -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', From 47e89acef585f937ae1edeac5f596726412ed1d0 Mon Sep 17 00:00:00 2001 From: Santos Gallegos Date: Wed, 28 Aug 2019 19:31:31 -0500 Subject: [PATCH 3/7] Filter by public --- readthedocs/doc_builder/backends/sphinx.py | 11 +++++++++-- readthedocs/projects/models.py | 3 +-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/readthedocs/doc_builder/backends/sphinx.py b/readthedocs/doc_builder/backends/sphinx.py index 3cce1831383..59760d3bab9 100644 --- a/readthedocs/doc_builder/backends/sphinx.py +++ b/readthedocs/doc_builder/backends/sphinx.py @@ -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 @@ -112,10 +113,16 @@ 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() + versions = self.project.active_versions().filter( + privacy_level=PUBLIC, + ) downloads = self.version.get_downloads(pretty=True) else: - versions = self.project.api_versions() + versions = [ + v + for v in self.project.api_versions() + if v.privacy_level == PUBLIC + ] downloads = api.version(self.version.pk).get()['downloads'] data = { diff --git a/readthedocs/projects/models.py b/readthedocs/projects/models.py index e025730b9c8..981a954a638 100644 --- a/readthedocs/projects/models.py +++ b/readthedocs/projects/models.py @@ -936,8 +936,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) From c19ff0452d0d175271d035b306c8deaeebaf60cf Mon Sep 17 00:00:00 2001 From: Santos Gallegos Date: Wed, 28 Aug 2019 23:03:10 -0500 Subject: [PATCH 4/7] Update docs --- docs/api/v2.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/api/v2.rst b/docs/api/v2.rst index c4a5200c844..9b0e82fcba4 100644 --- a/docs/api/v2.rst +++ b/docs/api/v2.rst @@ -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/", From 5f9469cfc51e44eb914d05c1b09af2c78ea77d89 Mon Sep 17 00:00:00 2001 From: Santos Gallegos Date: Thu, 29 Aug 2019 14:17:46 -0500 Subject: [PATCH 5/7] Include protected version on test case --- readthedocs/rtd_tests/tests/test_doc_builder.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/readthedocs/rtd_tests/tests/test_doc_builder.py b/readthedocs/rtd_tests/tests/test_doc_builder.py index 98c1d4b3bdb..9cae164007d 100644 --- a/readthedocs/rtd_tests/tests/test_doc_builder.py +++ b/readthedocs/rtd_tests/tests/test_doc_builder.py @@ -16,7 +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, PUBLIC +from readthedocs.projects.constants import PRIVATE, PROTECTED, PUBLIC from readthedocs.projects.exceptions import ProjectConfigurationError from readthedocs.projects.models import Feature, Project @@ -94,6 +94,10 @@ def test_html_context_only_has_public_versions( 'slug': 'v2', 'privacy_level': PUBLIC, }, + { + 'slug': 'v3', + 'privacy_level': PROTECTED, + }, { 'slug': 'latest', 'privacy_level': PRIVATE, From cf97522273fd33b765d9bd8491528cbdb3fdf261 Mon Sep 17 00:00:00 2001 From: Santos Gallegos Date: Mon, 16 Dec 2019 14:02:18 -0500 Subject: [PATCH 6/7] Add feature flag --- readthedocs/doc_builder/backends/sphinx.py | 22 ++++++++++++++-------- readthedocs/projects/models.py | 6 +++++- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/readthedocs/doc_builder/backends/sphinx.py b/readthedocs/doc_builder/backends/sphinx.py index 98a722a90dc..6e47e2286ae 100644 --- a/readthedocs/doc_builder/backends/sphinx.py +++ b/readthedocs/doc_builder/backends/sphinx.py @@ -113,16 +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().filter( - privacy_level=PUBLIC, - ) + 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 = [ - v - for v in self.project.api_versions() - if v.privacy_level == PUBLIC - ] + 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 = { diff --git a/readthedocs/projects/models.py b/readthedocs/projects/models.py index 680f3453547..9242a986166 100644 --- a/readthedocs/projects/models.py +++ b/readthedocs/projects/models.py @@ -1479,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')), @@ -1530,7 +1531,10 @@ 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( From 4ceb01eb0feb8836bfad117d161807c61e02f7c8 Mon Sep 17 00:00:00 2001 From: Santos Gallegos Date: Mon, 16 Dec 2019 18:19:01 -0500 Subject: [PATCH 7/7] Linter --- readthedocs/projects/models.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/readthedocs/projects/models.py b/readthedocs/projects/models.py index 9242a986166..37aad41a22f 100644 --- a/readthedocs/projects/models.py +++ b/readthedocs/projects/models.py @@ -1533,7 +1533,10 @@ def add_features(sender, **kwargs): ), ( ALL_VERSIONS_IN_HTML_CONTEXT, - _('Pass all versions (including private) into the html context when building with Sphinx'), + _( + 'Pass all versions (including private) into the html context ' + 'when building with Sphinx' + ), ), )