Skip to content

Commit

Permalink
Proxito: browndate for redirecting / to README.html
Browse files Browse the repository at this point in the history
Related #9993
  • Loading branch information
humitos committed May 23, 2024
1 parent 266ea25 commit 88f58bf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
8 changes: 7 additions & 1 deletion readthedocs/projects/tasks/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from readthedocs.builds.models import Build, Version
from readthedocs.projects.models import HTMLFile, Project
from readthedocs.projects.signals import files_changed
from readthedocs.proxito.views.utils import allow_readme_html_at_root_url
from readthedocs.search.documents import PageDocument
from readthedocs.search.utils import index_objects, remove_indexed_files
from readthedocs.storage import build_media_storage
Expand Down Expand Up @@ -203,7 +204,12 @@ def _create_imported_files_and_search_index(

# Create the imported file only if it's a top-level 404 file,
# or if it's an index file. We don't need to keep track of all files.
if relpath == "404.html" or filename in ["index.html", "README.html"]:
if allow_readme_html_at_root_url():
tryfiles = ["index.html", "README.html"]
else:
tryfiles = ["index.html"]

if relpath == "404.html" or filename in tryfiles:
html_files_to_save.append(html_file)

# We first index the files in ES, and then save the objects in the DB.
Expand Down
19 changes: 12 additions & 7 deletions readthedocs/proxito/views/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
ServeRedirectMixin,
StorageFileNotFound,
)
from readthedocs.proxito.views.utils import allow_readme_html_at_root_url
from readthedocs.redirects.exceptions import InfiniteRedirectException
from readthedocs.storage import build_media_storage

Expand Down Expand Up @@ -641,12 +642,16 @@ def _get_index_file_redirect(self, request, project, version, filename, full_pat
- /en/latest/foo -> /en/latest/foo/README.html
- /en/latest/foo/ -> /en/latest/foo/README.html
"""
tryfiles = ["index.html", "README.html"]
# If the path ends with `/`, we already tried to serve
# the `/index.html` file, so we only need to test for
# the `/README.html` file.
if full_path.endswith("/"):
tryfiles = ["README.html"]

if allow_readme_html_at_root_url():
tryfiles = ["index.html", "README.html"]
# If the path ends with `/`, we already tried to serve
# the `/index.html` file, so we only need to test for
# the `/README.html` file.
if full_path.endswith("/"):
tryfiles = ["README.html"]
else:
tryfiles = ["index.html"]

tryfiles = [
(filename.rstrip("/") + f"/{tryfile}").lstrip("/") for tryfile in tryfiles
Expand All @@ -664,7 +669,7 @@ def _get_index_file_redirect(self, request, project, version, filename, full_pat
log.info("Redirecting to index file.", tryfile=tryfile)
# Use urlparse so that we maintain GET args in our redirect
parts = urlparse(full_path)
if tryfile.endswith("README.html"):
if allow_readme_html_at_root_url() and tryfile.endswith("README.html"):
new_path = parts.path.rstrip("/") + "/README.html"
else:
new_path = parts.path.rstrip("/") + "/"
Expand Down
23 changes: 23 additions & 0 deletions readthedocs/proxito/views/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import datetime

import pytz
import structlog
from django.conf import settings
from django.http import HttpResponse
from django.shortcuts import render

Expand Down Expand Up @@ -56,3 +60,22 @@ def proxito_404_page_handler(
)
r.status_code = http_status
return r


def allow_readme_html_at_root_url():
tzinfo = pytz.timezone("America/Los_Angeles")
now = datetime.datetime.now(tz=tzinfo)

# Brownout dates as published in https://about.readthedocs.com/blog/2024/05/readme-html-deprecated/
# fmt: off
return not any([
# 12 hours browndate
datetime.datetime(2024, 6, 10, 0, 0, 0, tzinfo=tzinfo) < now < datetime.datetime(2024, 6, 10, 12, 0, 0, tzinfo=tzinfo),
# 24 hours browndate
datetime.datetime(2024, 6, 17, 0, 0, 0, tzinfo=tzinfo) < now < datetime.datetime(2024, 6, 18, 0, 0, 0, tzinfo=tzinfo),
# 48 hours browndate
datetime.datetime(2024, 6, 24, 0, 0, 0, tzinfo=tzinfo) < now < datetime.datetime(2024, 6, 26, 0, 0, 0, tzinfo=tzinfo),
# Deprecated after July 1st
datetime.datetime(2024, 7, 1, 0, 0, 0, tzinfo=tzinfo) < now,
]) and settings.RTD_ENFORCE_BROWNOUTS_FOR_DEPRECATIONS
# fmt: on

0 comments on commit 88f58bf

Please sign in to comment.