Skip to content

Commit 77d4a97

Browse files
authored
Proxito: browndate for redirecting / to README.html (#11348)
* Proxito: browndate for redirecting `/` to `README.html` Related #9993 * Rename function to make it clearer * Keep indexing README.html for now * Update logic to take into account enforce brownout setting
1 parent 2319268 commit 77d4a97

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

readthedocs/projects/tasks/search.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ def _create_imported_files_and_search_index(
203203

204204
# Create the imported file only if it's a top-level 404 file,
205205
# or if it's an index file. We don't need to keep track of all files.
206-
if relpath == "404.html" or filename in ["index.html", "README.html"]:
206+
# TODO: delete README.html from this list after deprecation.
207+
tryfiles = ["index.html", "README.html"]
208+
if relpath == "404.html" or filename in tryfiles:
207209
html_files_to_save.append(html_file)
208210

209211
# We first index the files in ES, and then save the objects in the DB.

readthedocs/proxito/views/serve.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
ServeRedirectMixin,
4242
StorageFileNotFound,
4343
)
44+
from readthedocs.proxito.views.utils import allow_readme_html_as_index
4445
from readthedocs.redirects.exceptions import InfiniteRedirectException
4546
from readthedocs.storage import build_media_storage
4647

@@ -641,12 +642,16 @@ def _get_index_file_redirect(self, request, project, version, filename, full_pat
641642
- /en/latest/foo -> /en/latest/foo/README.html
642643
- /en/latest/foo/ -> /en/latest/foo/README.html
643644
"""
644-
tryfiles = ["index.html", "README.html"]
645-
# If the path ends with `/`, we already tried to serve
646-
# the `/index.html` file, so we only need to test for
647-
# the `/README.html` file.
648-
if full_path.endswith("/"):
649-
tryfiles = ["README.html"]
645+
646+
if allow_readme_html_as_index():
647+
tryfiles = ["index.html", "README.html"]
648+
# If the path ends with `/`, we already tried to serve
649+
# the `/index.html` file, so we only need to test for
650+
# the `/README.html` file.
651+
if full_path.endswith("/"):
652+
tryfiles = ["README.html"]
653+
else:
654+
tryfiles = ["index.html"]
650655

651656
tryfiles = [
652657
(filename.rstrip("/") + f"/{tryfile}").lstrip("/") for tryfile in tryfiles
@@ -664,7 +669,7 @@ def _get_index_file_redirect(self, request, project, version, filename, full_pat
664669
log.info("Redirecting to index file.", tryfile=tryfile)
665670
# Use urlparse so that we maintain GET args in our redirect
666671
parts = urlparse(full_path)
667-
if tryfile.endswith("README.html"):
672+
if allow_readme_html_as_index() and tryfile.endswith("README.html"):
668673
new_path = parts.path.rstrip("/") + "/README.html"
669674
else:
670675
new_path = parts.path.rstrip("/") + "/"

readthedocs/proxito/views/utils.py

+26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
import datetime
2+
3+
import pytz
14
import structlog
5+
from django.conf import settings
26
from django.http import HttpResponse
37
from django.shortcuts import render
48

@@ -56,3 +60,25 @@ def proxito_404_page_handler(
5660
)
5761
r.status_code = http_status
5862
return r
63+
64+
65+
def allow_readme_html_as_index():
66+
if not settings.RTD_ENFORCE_BROWNOUTS_FOR_DEPRECATIONS:
67+
return True
68+
69+
tzinfo = pytz.timezone("America/Los_Angeles")
70+
now = datetime.datetime.now(tz=tzinfo)
71+
72+
# Brownout dates as published in https://about.readthedocs.com/blog/2024/05/readme-html-deprecated/
73+
# fmt: off
74+
return not any([
75+
# 12 hours browndate
76+
datetime.datetime(2024, 6, 10, 0, 0, 0, tzinfo=tzinfo) < now < datetime.datetime(2024, 6, 10, 12, 0, 0, tzinfo=tzinfo),
77+
# 24 hours browndate
78+
datetime.datetime(2024, 6, 17, 0, 0, 0, tzinfo=tzinfo) < now < datetime.datetime(2024, 6, 18, 0, 0, 0, tzinfo=tzinfo),
79+
# 48 hours browndate
80+
datetime.datetime(2024, 6, 24, 0, 0, 0, tzinfo=tzinfo) < now < datetime.datetime(2024, 6, 26, 0, 0, 0, tzinfo=tzinfo),
81+
# Deprecated after July 1st
82+
datetime.datetime(2024, 7, 1, 0, 0, 0, tzinfo=tzinfo) < now,
83+
])
84+
# fmt: on

0 commit comments

Comments
 (0)