Skip to content

Commit 6ad9ceb

Browse files
committed
Capture data on whether the incoming URL has a slash on it.
1 parent ee4bf63 commit 6ad9ceb

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

readthedocs/proxito/tests/test_redirects.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ def test_subproject_root_url_no_slash(self):
4040
r['Location'], 'https://project.dev.readthedocs.io/projects/subproject/en/latest/',
4141
)
4242

43+
def test_single_version_subproject_root_url_no_slash(self):
44+
self.subproject.single_version = True
45+
self.subproject.save()
46+
r = self.client.get('/projects/subproject', HTTP_HOST='project.dev.readthedocs.io')
47+
self.assertEqual(r.status_code, 302)
48+
self.assertEqual(
49+
r['Location'], 'https://project.dev.readthedocs.io/projects/subproject/',
50+
)
51+
4352
def test_root_redirect_with_query_params(self):
4453
r = self.client.get('/?foo=bar', HTTP_HOST='project.dev.readthedocs.io')
4554
self.assertEqual(r.status_code, 302)

readthedocs/proxito/urls.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@
147147
# (Sub)project single version
148148
url(
149149
(
150-
# the /? at the end of this regex is for ``/projects/subproject``
150+
# subproject_slash variable at the end of this regex is for ``/projects/subproject``
151151
# so that it will get captured here and redirect properly.
152-
r'^(?:projects/(?P<subproject_slug>{project_slug})/?)?'
152+
r'^(?:projects/(?P<subproject_slug>{project_slug})(?P<subproject_slash>/?))?'
153153
r'(?P<filename>{filename_slug})$'.format(**pattern_opts)
154154
),
155155
ServeDocs.as_view(),

readthedocs/proxito/views/serve.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,17 @@ def get(self,
5555
request,
5656
project_slug=None,
5757
subproject_slug=None,
58+
subproject_slash=None,
5859
lang_slug=None,
5960
version_slug=None,
6061
filename='',
6162
): # noqa
62-
"""Take the incoming parsed URL's and figure out what file to serve."""
63+
"""
64+
Take the incoming parsed URL's and figure out what file to serve.
65+
66+
``subproject_slash`` is used to determine if the subproject URL has a slash,
67+
so that we can decide if we need to serve docs or add a /.
68+
"""
6369

6470
version_slug = self.get_version_from_host(request, version_slug)
6571
final_project, lang_slug, version_slug, filename = _get_project_data_from_request( # noqa
@@ -87,6 +93,14 @@ def get(self,
8793
]):
8894
return self.system_redirect(request, final_project, lang_slug, version_slug, filename)
8995

96+
# Handle `/projects/subproject` URL redirection
97+
if all([
98+
final_project.single_version,
99+
filename == '',
100+
not subproject_slash,
101+
]):
102+
return self.system_redirect(request, final_project, lang_slug, version_slug, filename)
103+
90104
if all([
91105
(lang_slug is None or version_slug is None),
92106
not final_project.single_version,

0 commit comments

Comments
 (0)