|
5 | 5 | import mimetypes
|
6 | 6 | import os
|
7 | 7 | from functools import wraps
|
8 |
| -from urllib.parse import urlparse |
| 8 | +from urllib.parse import urlparse, urlunparse |
9 | 9 |
|
10 | 10 | from django.conf import settings
|
11 | 11 | 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 |
13 | 13 | from django.shortcuts import get_object_or_404, render
|
14 | 14 | from django.urls import resolve as url_resolve
|
15 | 15 | from django.utils.encoding import iri_to_uri
|
@@ -245,11 +245,23 @@ def serve_docs(
|
245 | 245 | )
|
246 | 246 | raise Http404('Invalid URL for project with versions')
|
247 | 247 |
|
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) |
253 | 265 |
|
254 | 266 | # Don't do auth checks
|
255 | 267 | # try:
|
|
0 commit comments