diff --git a/network-api/networkapi/settings.py b/network-api/networkapi/settings.py index 9197797640c..4b347dd5bed 100644 --- a/network-api/networkapi/settings.py +++ b/network-api/networkapi/settings.py @@ -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", # diff --git a/network-api/networkapi/utility/middleware.py b/network-api/networkapi/utility/middleware.py index 1e1b83bbb9c..86f5c3369ce 100644 --- a/network-api/networkapi/utility/middleware.py +++ b/network-api/networkapi/utility/middleware.py @@ -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 @@ -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 diff --git a/network-api/networkapi/utility/tests/test_middleware.py b/network-api/networkapi/utility/tests/test_middleware.py index dd504c8cc7f..fe0f486dd41 100644 --- a/network-api/networkapi/utility/tests/test_middleware.py +++ b/network-api/networkapi/utility/tests/test_middleware.py @@ -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")