Skip to content

Perform redirects at DB level #6398

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

Merged
merged 14 commits into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
53 changes: 50 additions & 3 deletions readthedocs/redirects/querysets.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""Queryset for the redirects app."""

from django.db import models
from django.db.models import Value, CharField, Q, F

from readthedocs.core.utils.extend import SettingsOverrideObject
from readthedocs.projects import constants


class RedirectQuerySetBase(models.QuerySet):
Expand All @@ -25,8 +25,55 @@ def api(self, user=None, detail=True):
queryset = self._add_user_repos(queryset, user)
return queryset

def get_redirect_path_with_status(self, path, language=None, version_slug=None):
for redirect in self.select_related('project'):
def get_redirect_path_with_status(self, path, full_path=None, language=None, version_slug=None):
# 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(),
),
)
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', # Python implementation does "in"
) | 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_html',
path__endswith='.html',
)

# 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
queryset = queryset.filter(prefix | page | exact | sphinx_html | sphinx_htmldir)
for redirect in queryset.select_related('project'):
new_path = redirect.get_redirect_path(
path=path,
language=language,
Expand Down
2 changes: 1 addition & 1 deletion readthedocs/redirects/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def get_redirect_response(request, full_path):
language, version_slug, path = language_and_version_from_path(path)

path, http_status = project.redirects.get_redirect_path_with_status(
path=path, language=language, version_slug=version_slug
path=path, full_path=full_path, language=language, version_slug=version_slug
Copy link
Member Author

@humitos humitos Nov 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why we weren't passing this full_path already. We are re-calculating it for one of the cases at redirect_exact method.

Passing it at this point, allows us to use it as a value to compare at db level instead from Python itself.

)

if path is None:
Expand Down