misc: fix category URLs with missing trailing slash and query string (#40799)

This commit is contained in:
Frédéric Péters 2020-03-17 23:14:02 +01:00
parent 3c45b4bd14
commit 7012ca290f
3 changed files with 28 additions and 0 deletions

View File

@ -18,6 +18,11 @@ from wcs.qommon import emails
OldFormPage = wcs.forms.root.FormPage
class AlternateFormPage(OldFormPage):
def __call__(self):
# Fix missing trailing slash; lose query string on purpose.
path = quixote.get_path()
return quixote.redirect(path + "/", permanent=True)
def form_side(self, *args, **kwargs):
form_side_html = OldFormPage.form_side(self, *args, **kwargs)
# add a 'Steps' title

View File

@ -287,6 +287,10 @@ class AlternateRootDirectory(OldRootDirectory):
except KeyError:
pass
else:
if len(path) == 1:
# category with missing trailing slash, redirect.
path = quixote.get_path()
return quixote.redirect(path + "/", permanent=True)
return FormsRootDirectory(cat)._q_traverse(path[1:])
raise e

View File

@ -135,6 +135,25 @@ def test_form_category_redirection():
resp = get_app(pub).get('/foobar/')
assert resp.location.endswith('/baz/foobar/')
# check missing trailing slashs are ok
resp = get_app(pub).get('/foobar')
assert resp.location.endswith('/baz/foobar')
resp = resp.follow()
assert resp.location.endswith('/baz/foobar/')
# even if there's a query string
resp = get_app(pub).get('/foobar?test=1')
assert resp.location.endswith('/baz/foobar?test=1')
resp = resp.follow()
assert resp.location.endswith('/baz/foobar/')
# check direct category access without trailing slash
resp = get_app(pub).get('/baz')
assert resp.location.endswith('/baz/')
resp = get_app(pub).get('/baz?test=1')
assert resp.location.endswith('/baz/')
def test_form_and_category_same_slug():
Category.wipe()
cat = Category(name='foobar')