Skip to content

Perform redirects at db level #6779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 94 additions & 2 deletions readthedocs/redirects/querysets.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
"""Queryset for the redirects app."""

from django.db import models
import logging

from django.db import models, DataError
from django.db.models import Value, CharField, IntegerField, Q, F, ExpressionWrapper
from django.db.models.functions import Substr, Length

from readthedocs.core.utils.extend import SettingsOverrideObject

log = logging.getLogger(__name__)


class RedirectQuerySetBase(models.QuerySet):

Expand All @@ -25,7 +31,93 @@ def api(self, user=None, detail=True):
return queryset

def get_redirect_path_with_status(self, path, full_path=None, language=None, version_slug=None):
for redirect in self.select_related('project'):
# add extra fields with the ``path`` and ``full_path`` to perform a
# filter at db level instead with Python
queryset = self.annotate(
path=Value(
path,
output_field=CharField(),
),
full_path=Value(
full_path,
output_field=CharField(),
),

from_url_length=ExpressionWrapper(
Length('from_url'),
output_field=IntegerField(),
),

# 1-indexed
from_url_without_rest=Substr(
'from_url',
1,
F('from_url_length') - 5, # Strip "$rest"
output_field=CharField(),
),

# 1-indexed
full_path_without_rest=Substr(
'full_path',
1,
F('from_url_length') - 5, # Strip "$rest"
output_field=CharField(),
),
)
prefix = Q(
redirect_type='prefix',
path__startswith=F('from_url'),
)
page = Q(
redirect_type='page',
path__iexact=F('from_url'),
)
exact = (
Q(
redirect_type='exact',
from_url__endswith='$rest',
# This works around a bug in Django doing a substr and an endswith,
# so instead we do 2 substrs and an exact
# https://code.djangoproject.com/ticket/29155
full_path_without_rest=F('from_url_without_rest'),
) | Q(
redirect_type='exact',
full_path__iexact=F('from_url'),
)
)
sphinx_html = (
Q(
redirect_type='sphinx_html',
path__endswith='/',
) | Q(
redirect_type='sphinx_html',
path__endswith='/index.html',
)
)
sphinx_htmldir = Q(
redirect_type='sphinx_htmldir',
path__endswith='.html',
)

try:
# Using the ``exact`` Q object we created here could cause an
# execption because it uses ``from_url_without_rest`` and
# ``full_path_without_rest`` which could produce a database error
# ("negative substring length not allowed") when ``from_url``'s
# length is less than 5 ('$rest' string substracted from it). We
# perform a query using ``exact`` here and if it fail we catch it
# and just ignore these redirects for this project.
queryset = queryset.filter(prefix | page | exact | sphinx_html | sphinx_htmldir)
queryset.select_related('project').count()
except DataError:
# Fallback to the query without using ``exact`` on it
log.warning('Failing Exact Redirects on this project. Ignoring them.')
queryset = queryset.filter(prefix | page | sphinx_html | sphinx_htmldir)

# There should be one and only one redirect returned by this query. I
# can't think in a case where there can be more at this point. I'm
# leaving the loop just in case for now
for redirect in queryset.select_related('project'):
new_path = redirect.get_redirect_path(
path=path,
language=language,
Expand Down