def set_language(request): """ Since this view changes how the user will see the rest of the site, it must only be accessed as a POST request. If called as a GET request, it will error. """ lang_code = request.POST.get(LANGUAGE_QUERY_PARAMETER) next_url = urlsplit(request.POST.get("next")) if request.POST.get("next") else None if lang_code and check_for_language(lang_code): if next_url and is_valid_path(next_url.path): # If it is a recognized Kolibri path, then translate it to the new language and return it. next_path = urlunsplit( ( next_url[0], next_url[1], translate_url(next_url[2], lang_code), next_url[3], next_url[4], ) ) else: next_path = translate_url(reverse("kolibri:core:redirect_user"), lang_code) response = HttpResponse(next_path) if hasattr(request, "session"): request.session[LANGUAGE_SESSION_KEY] = lang_code # Always set cookie response.set_cookie( settings.LANGUAGE_COOKIE_NAME, lang_code, max_age=settings.LANGUAGE_COOKIE_AGE, path=settings.LANGUAGE_COOKIE_PATH, domain=settings.LANGUAGE_COOKIE_DOMAIN, ) else: lang_code = ( get_device_language() or get_accept_headers_language(request) or get_settings_language() ) if next_url and is_valid_path(next_url.path): # If it is a recognized Kolibri path, then translate it using the default language code for this device next_path = urlunsplit( ( next_url[0], next_url[1], translate_url(next_url[2], lang_code), next_url[3], next_url[4], ) ) else: next_path = translate_url(reverse("kolibri:core:redirect_user"), lang_code) response = HttpResponse(next_path) if hasattr(request, "session"): request.session.pop(LANGUAGE_SESSION_KEY, "") response.delete_cookie(settings.LANGUAGE_COOKIE_NAME) return response
def test_default_language_without_namespaced_logout(self): # Test /logout without any in-path language code. Expect default language setting. self.client.login(**self.credentials) response = self.client.get("/logout") self.assertTrue(get_settings_language() in response.url)