Skip to content

Commit

Permalink
Add middleware to redirect pt-br to correct locale path (#13157)
Browse files Browse the repository at this point in the history
* New middleware to redirect to correct locale path

* Generalize middleware, include fy-nl check, add tests

* Fix line length
  • Loading branch information
robdivincenzo authored Nov 26, 2024
1 parent 2c91af3 commit 7b1ec12
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions network-api/networkapi/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@
"whitenoise.middleware.WhiteNoiseMiddleware",
"django.middleware.gzip.GZipMiddleware",
"debug_toolbar.middleware.DebugToolbarMiddleware" if DEBUG_TOOLBAR_ENABLED else None,
"networkapi.utility.middleware.NormalizeLocaleMiddleware",
"networkapi.utility.middleware.TargetDomainRedirectMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
#
Expand Down
29 changes: 29 additions & 0 deletions network-api/networkapi/utility/middleware.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.conf import settings
from django.http.response import HttpResponseRedirectBase
from django.shortcuts import redirect
from django.utils.deprecation import MiddlewareMixin

hostnames = settings.TARGET_DOMAINS

Expand Down Expand Up @@ -52,3 +54,30 @@ def __call__(self, request):
return HttpResponseTemporaryRedirect(redirect_url)

return self.get_response(request)


# Middleware to normalize the pt-br and fy-nl locales to their capitalized regions (pt-BR and fy-NL)
class NormalizeLocaleMiddleware(MiddlewareMixin):
# Dictionary mapping incorrect locale keys to their normalized forms
locales_to_normalize = {"pt-br": "pt-BR", "fy-nl": "fy-NL"}

def process_request(self, request):
# Split the path into segments
path_segments = request.path_info.strip("/").split("/")

# Check if the first segment is a locale that needs to be normalized
if len(path_segments) > 0 and path_segments[0] in self.locales_to_normalize:
normalized_locale = self.locales_to_normalize[path_segments[0]]
# Only redirect if the current locale is not already in the correct form
if path_segments[0] != normalized_locale:
path_segments[0] = normalized_locale
new_path = "/" + "/".join(path_segments) + "/" # Ensure trailing slash

# Preserve query parameters if they exist
query_string = request.META.get("QUERY_STRING")
if query_string:
new_path += f"?{query_string}"

return redirect(new_path, permanent=False) # Using temporary redirects

return None
18 changes: 18 additions & 0 deletions network-api/networkapi/utility/tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,21 @@ def test_sends_x_robots_tag(self):
xrobotstag_middleware = XRobotsTagMiddleware(MagicMock())
response = xrobotstag_middleware(MagicMock())
response.__setitem__.assert_called_with("X-Robots-Tag", "noindex")


class NormalizeLocaleMiddlewareTest(TestCase):
def test_redirect_with_pt_br_language(self):
# Simulate a request to a URL with 'pt-br' in the path
response = self.client.get("/pt-br/?form=donate-header")

# Assert that the middleware issues a redirect to the normalized URL
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, "/pt-BR/?form=donate-header")

def test_redirect_with_fy_NL_language(self):
# Simulate a request to a URL with 'fy-nl' in the path
response = self.client.get("/fy-nl/?form=donate-header")

# Assert that the middleware issues a redirect to the normalized URL
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, "/fy-NL/?form=donate-header")

0 comments on commit 7b1ec12

Please sign in to comment.