Skip to content

Commit ca3f3f9

Browse files
committed
Trigger redirect logic from El Proxito
Adapt El Proxito `serve_docs` to detect if there is any redirect configured on the project that matches the URL and return the proper redirect in that case.
1 parent fd66a6a commit ca3f3f9

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

readthedocs/proxito/views.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
import mimetypes
66
import os
77
from functools import wraps
8-
from urllib.parse import urlparse
8+
from urllib.parse import urlparse, urlunparse
99

1010
from django.conf import settings
1111
from django.core.files.storage import get_storage_class
12-
from django.http import Http404, HttpResponse, HttpResponseRedirect
12+
from django.http import Http404, HttpResponse, HttpResponseRedirect, HttpResponsePermanentRedirect
1313
from django.shortcuts import get_object_or_404, render
1414
from django.urls import resolve as url_resolve
1515
from django.utils.encoding import iri_to_uri
@@ -245,11 +245,23 @@ def serve_docs(
245245
)
246246
raise Http404('Invalid URL for project with versions')
247247

248-
# TODO: Redirects need to be refactored before we can turn them on
249-
# They currently do 1 request per redirect that exists for the project
250-
# path, http_status = final_project.redirects.get_redirect_path_with_status(
251-
# language=lang_slug, version_slug=version_slug, path=filename
252-
# )
248+
full_path = request.path
249+
redirect_path, http_status = final_project.redirects.get_redirect_path_with_status(
250+
language=lang_slug, version_slug=version_slug, path=filename, full_path=full_path,
251+
)
252+
if redirect_path is not None:
253+
schema, netloc, path, params, query, fragments = urlparse(full_path)
254+
new_path = urlunparse((schema, netloc, redirect_path, params, query, fragments))
255+
256+
# Re-use the domain and protocol used in the current request.
257+
# Redirects shouldn't change the domain, version or language.
258+
# However, if the new_path is already an absolute URI, just use it
259+
new_path = request.build_absolute_uri(new_path)
260+
261+
if http_status and http_status == 301:
262+
return HttpResponsePermanentRedirect(new_path)
263+
264+
return HttpResponseRedirect(new_path)
253265

254266
# Don't do auth checks
255267
# try:

0 commit comments

Comments
 (0)