Skip to content

Commit

Permalink
feat: add setting so redirect preserve params
Browse files Browse the repository at this point in the history
Added a new setting that allow to configure globally if the django-cms
redirects preserving the query parameters.
`REDIRECT_PRESERVE_QUERY_PARAMS`.
This feature is usefull for example:
1. marketing campains extra parameters,
2. social networks extra parameters like `fbclick`,
3. custom developed parameters, after that page has been moved, the
older URLs for that page should preserve the functionality.
  • Loading branch information
igobranco committed Feb 6, 2023
1 parent 86a9a14 commit 952504e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
25 changes: 25 additions & 0 deletions cms/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,31 @@ def test_redirect_with_toolbar(self):
response = self.client.get(self.get_toolbar_disable_url(page_url))
self.assertEqual(response.status_code, 302)

def test_redirect_not_preserving_query_parameters(self):
# test redirect checking that the query parameters aren't preserved
redirect = '/en/'
one = create_page("one", "nav_playground.html", "en", published=True,
redirect=redirect)
url = one.get_absolute_url()
params = "?param_name=param_value"
request = self.get_request(url + params)
response = details(request, one.get_path())
self.assertEqual(response.status_code, 302)
self.assertEqual(response['Location'], redirect)

@override_settings(CMS_REDIRECT_PRESERVE_QUERY_PARAMS=True)
def test_redirect_preserving_query_parameters(self):
# test redirect checking that query parameters are preserved
redirect = '/en/'
one = create_page("one", "nav_playground.html", "en", published=True,
redirect=redirect)
url = one.get_absolute_url()
params = "?param_name=param_value"
request = self.get_request(url + params)
response = details(request, one.get_path())
self.assertEqual(response.status_code, 302)
self.assertEqual(response['Location'], redirect + params)

def test_login_required(self):
self.create_homepage("page", "nav_playground.html", "en", published=True, login_required=True)
plain_url = '/accounts/'
Expand Down
1 change: 1 addition & 0 deletions cms/utils/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def wrapper():
),
'COLOR_SCHEME': 'light',
'COLOR_SCHEME_TOGGLE': False,
'REDIRECT_PRESERVE_QUERY_PARAMS': False,
}


Expand Down
4 changes: 4 additions & 0 deletions cms/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ def details(request, slug):
toolbar.redirect_url = redirect_url
elif redirect_url not in own_urls:
# prevent redirect to self
if get_cms_setting('REDIRECT_PRESERVE_QUERY_PARAMS'):
query_string = request.META.get('QUERY_STRING')
if query_string:
redirect_url += "?" + query_string
return HttpResponseRedirect(redirect_url)

# permission checks
Expand Down
10 changes: 10 additions & 0 deletions docs/reference/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1190,3 +1190,13 @@ when the "Content" field is filled in. There should be no need to change it,
unless you **don't** use ``djangocms-text-ckeditor`` in your project **and**
your custom plugin defined in :setting:`CMS_PAGE_WIZARD_CONTENT_PLUGIN` have a
body field **different** than ``body``.

.. setting:: CMS_REDIRECT_PRESERVE_QUERY_PARAMS

CMS_REDIRECT_PRESERVE_QUERY_PARAMS
===================================

default
``False``

This indicates to the CMS that redirects should preserve the query parameters.

0 comments on commit 952504e

Please sign in to comment.