Skip to content

Commit f1b3402

Browse files
committed
Add APPEND_SLASH handling to the root middleware
1 parent bba4d7f commit f1b3402

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Change log
1818
- Imported the ``old_richtext`` module to ``feincms3.plugins`` as long as it is
1919
available.
2020
- Changed the linked CKEditor version to 4.18.0.
21+
- Added ``APPEND_SLASH`` handling to the middleware created by
22+
:func:`feincms3.root.middleware.create_page_if_404_middleware`. This has to
23+
be done explicitly because valid page paths aren't resolvable when using a
24+
middleware.
2125

2226

2327
`3.4`_ (2022-03-10)

feincms3/root/middleware.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ def handler(request, page):
4040

4141
from functools import wraps
4242

43-
from django.http import HttpResponseNotFound, HttpResponseRedirect
43+
from django.conf import settings
44+
from django.http import (
45+
HttpResponseNotFound,
46+
HttpResponsePermanentRedirect,
47+
HttpResponseRedirect,
48+
)
4449

4550

4651
class _UseRootMiddlewareResponse(HttpResponseNotFound):
@@ -90,6 +95,10 @@ def inner(request):
9095
target = f"/{request.LANGUAGE_CODE}/"
9196
if qs.filter(path=target).exists():
9297
return HttpResponseRedirect(target)
98+
if settings.APPEND_SLASH and not request.path_info.endswith("/"):
99+
target = request.path + "/"
100+
if qs.filter(path=target).exists():
101+
return HttpResponsePermanentRedirect(target)
93102
return response
94103

95104
return inner

tests/testapp/test_feincms3.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from django.forms.models import modelform_factory
1010
from django.template import Context, Template, TemplateSyntaxError
1111
from django.test import Client, RequestFactory, TestCase
12-
from django.test.utils import isolate_apps
12+
from django.test.utils import isolate_apps, override_settings
1313
from django.urls import NoReverseMatch, reverse, set_urlconf
1414
from django.utils.translation import deactivate_all, override
1515

@@ -1459,3 +1459,29 @@ def test_404_for_resolvable_app_path(self):
14591459
# Page exists at URL but doesn't override the 404 generated by the blog app
14601460
response = self.client.get("/blog/0/")
14611461
self.assertEqual(response.status_code, 404)
1462+
1463+
def test_append_slash(self):
1464+
"""Requests without slash are redirected if APPEND_SLASH and target exists"""
1465+
1466+
Page.objects.create(
1467+
title="home",
1468+
slug="home",
1469+
path="/home/",
1470+
static_path=True,
1471+
language_code="de",
1472+
is_active=True,
1473+
menu="main",
1474+
)
1475+
1476+
with override_settings(APPEND_SLASH=True):
1477+
response = self.client.get("/home")
1478+
self.assertRedirects(
1479+
response, "/home/", fetch_redirect_response=False, status_code=301
1480+
)
1481+
1482+
response = self.client.get("/blub")
1483+
self.assertEqual(response.status_code, 404)
1484+
1485+
with override_settings(APPEND_SLASH=False):
1486+
response = self.client.get("/home")
1487+
self.assertEqual(response.status_code, 404)

0 commit comments

Comments
 (0)