Skip to content

Commit 03ce54a

Browse files
committed
Use Func(function='replace') for exact redirect
`Func(function='replace')` allows us to modify a field in the row. We use it to remove the `$rest` part of the URL and be able to compare the beginning of the from URL (without the `$rest`) against the `full_path`.
1 parent ca3f3f9 commit 03ce54a

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

readthedocs/redirects/querysets.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Queryset for the redirects app."""
22

33
from django.db import models
4-
from django.db.models import Value, CharField, Q, F
4+
from django.db.models import Value, CharField, Q, F, Func
55

66
from readthedocs.core.utils.extend import SettingsOverrideObject
77

@@ -37,6 +37,16 @@ def get_redirect_path_with_status(self, path, full_path=None, language=None, ver
3737
full_path,
3838
output_field=CharField(),
3939
),
40+
41+
# NOTE: using replace here could take some time if there are a lot
42+
# of redirect for this project.
43+
from_url_without_rest=Func(
44+
F('from_url'),
45+
Value('$rest'),
46+
Value(''),
47+
# This could be done with ``Replace`` once on Django 2.2
48+
function='replace',
49+
),
4050
)
4151
prefix = Q(
4252
redirect_type='prefix',
@@ -50,6 +60,7 @@ def get_redirect_path_with_status(self, path, full_path=None, language=None, ver
5060
Q(
5161
redirect_type='exact',
5262
from_url__endswith='$rest', # Python implementation does "in"
63+
full_path__startswith=F('from_url_without_rest'),
5364
) | Q(
5465
redirect_type='exact',
5566
full_path__iexact=F('from_url'),

0 commit comments

Comments
 (0)