-
-
Notifications
You must be signed in to change notification settings - Fork 301
Allows users to change language when not signed-in. thus, implementing proper language redirection as in Kolibri #3721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bjester
merged 7 commits into
learningequality:unstable
from
ozer550:FIX_CHANGE_LANGUAGE
Nov 2, 2022
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
17b3dfc
Work in progress
ozer550 40513c0
final implementation with tests
ozer550 b558ca7
set language cookie age
ozer550 e41eed0
fix valid_path bug
ozer550 140b644
Remove console log
ozer550 aa6f998
broken change language in main app drawer
ozer550 8d9d503
update docstring
ozer550 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
contentcuration/contentcuration/frontend/shared/languageSwitcher/mixin.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -427,3 +427,5 @@ def gettext(s): | |
|
|
||
|
|
||
| DEFAULT_AUTO_FIELD = "django.db.models.AutoField" | ||
|
|
||
| LANGUAGE_COOKIE_AGE = 3600 * 24 * 14 | ||
150 changes: 150 additions & 0 deletions
150
contentcuration/contentcuration/tests/test_setlanguage.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| import json | ||
|
|
||
| from django.conf import settings | ||
| from django.http import HttpResponseNotAllowed | ||
| from django.test import override_settings | ||
| from django.test import TestCase | ||
| from django.urls import reverse | ||
| from django.urls import translate_url | ||
| from django.utils.translation import get_language | ||
| from django.utils.translation import LANGUAGE_SESSION_KEY | ||
|
|
||
|
|
||
| @override_settings(LANGUAGE_CODE="en") | ||
| class I18NTests(TestCase): | ||
| """ | ||
| Tests set_language view in kolibri/core/views.py | ||
| Copied from https://github.com/django/django/blob/stable/1.11.x/tests/view_tests/tests/test_i18n.py | ||
| """ | ||
|
|
||
| def set_post_data(self, lang_code, next_url=""): | ||
| post_data = { | ||
| "language": lang_code, | ||
| "next": next_url, | ||
| } | ||
| return json.dumps(post_data) | ||
|
|
||
| def _get_inactive_language_code(self): | ||
| """Return language code for a language which is not activated.""" | ||
| current_language = get_language() | ||
| return [ | ||
| code for code, name in settings.LANGUAGES if not code == current_language | ||
| ][0] | ||
|
|
||
| def test_setlang(self): | ||
| """ | ||
| The set_language view can be used to change the session language. | ||
| """ | ||
| lang_code = self._get_inactive_language_code() | ||
| response = self.client.post(reverse("set_language"), self.set_post_data(lang_code), content_type='application/json') | ||
| self.assertEqual(response.status_code, 200) | ||
| self.assertEqual( | ||
| response.content.decode("utf-8"), | ||
| translate_url(reverse("base"), lang_code), | ||
| ) | ||
| self.assertEqual(self.client.session[LANGUAGE_SESSION_KEY], lang_code) | ||
|
|
||
| def test_setlang_next_valid(self): | ||
| """ | ||
| The set_language view can be used to change the session language. | ||
| The user is redirected to the "next" argument. | ||
| """ | ||
| lang_code = self._get_inactive_language_code() | ||
| next_url = reverse("channels") | ||
| response = self.client.post(reverse("set_language"), self.set_post_data(lang_code, next_url), content_type='application/json') | ||
| self.assertEqual(response.status_code, 200) | ||
| self.assertEqual( | ||
| response.content.decode("utf-8"), | ||
| translate_url(reverse("channels"), lang_code), | ||
| ) | ||
| self.assertEqual(self.client.session[LANGUAGE_SESSION_KEY], lang_code) | ||
|
|
||
| def test_setlang_next_invalid(self): | ||
| """ | ||
| The set_language view can be used to change the session language. | ||
| The user is redirected to base redirect if the "next" argument is invalid. | ||
| """ | ||
| lang_code = self._get_inactive_language_code() | ||
| next_url = "/not/a/real/url" | ||
| response = self.client.post(reverse("set_language"), self.set_post_data(lang_code, next_url), content_type='application/json') | ||
| self.assertEqual(response.status_code, 200) | ||
| self.assertEqual( | ||
| response.content.decode("utf-8"), | ||
| translate_url(reverse("base"), lang_code), | ||
| ) | ||
| self.assertEqual(self.client.session[LANGUAGE_SESSION_KEY], lang_code) | ||
|
|
||
| def test_setlang_null(self): | ||
| """ | ||
| Test language code set to null which shoul direct to default language "en" | ||
| """ | ||
| lang_code = self._get_inactive_language_code() | ||
| response = self.client.post(reverse("set_language"), self.set_post_data(lang_code), content_type='application/json') | ||
| self.assertEqual(response.status_code, 200) | ||
| self.assertEqual( | ||
| response.content.decode("utf-8"), | ||
| translate_url(reverse("base"), lang_code), | ||
| ) | ||
| self.assertEqual(self.client.session[LANGUAGE_SESSION_KEY], lang_code) | ||
| lang_code = None | ||
| response = self.client.post(reverse("set_language"), self.set_post_data(lang_code), content_type='application/json') | ||
| self.assertEqual(response.status_code, 200) | ||
| self.assertEqual( | ||
| response.content.decode("utf-8"), | ||
| translate_url(reverse("base"), "en"), | ||
| ) | ||
| self.assertFalse(LANGUAGE_SESSION_KEY in self.client.session) | ||
|
|
||
| def test_setlang_null_next_valid(self): | ||
| """ | ||
| The set_language view can be used to change the session language. | ||
| The user is redirected to the "next" argument. | ||
| """ | ||
| lang_code = self._get_inactive_language_code() | ||
| response = self.client.post(reverse("set_language"), self.set_post_data(lang_code), content_type='application/json') | ||
| self.assertEqual(response.status_code, 200) | ||
| self.assertEqual( | ||
| response.content.decode("utf-8"), | ||
| translate_url(reverse("base"), lang_code), | ||
| ) | ||
| self.assertEqual(self.client.session[LANGUAGE_SESSION_KEY], lang_code) | ||
| next_url = reverse("channels") | ||
| lang_code = None | ||
| response = self.client.post(reverse("set_language"), self.set_post_data(lang_code, next_url), content_type='application/json') | ||
| self.assertEqual(response.status_code, 200) | ||
| self.assertEqual( | ||
| response.content.decode("utf-8"), | ||
| translate_url(reverse("channels"), "en"), | ||
| ) | ||
| self.assertFalse(LANGUAGE_SESSION_KEY in self.client.session) | ||
|
|
||
| def test_setlang_null_next_invalid(self): | ||
| """ | ||
| The set_language view can be used to change the session language. | ||
| The user is redirected to user redirect if the "next" argument is invalid. | ||
| """ | ||
| lang_code = self._get_inactive_language_code() | ||
| response = self.client.post(reverse("set_language"), self.set_post_data(lang_code), content_type='application/json') | ||
| self.assertEqual(response.status_code, 200) | ||
| self.assertEqual( | ||
| response.content.decode("utf-8"), | ||
| translate_url(reverse("base"), lang_code), | ||
| ) | ||
| self.assertEqual(self.client.session[LANGUAGE_SESSION_KEY], lang_code) | ||
| next_url = "/not/a/real/url" | ||
| lang_code = None | ||
| response = self.client.post(reverse("set_language"), self.set_post_data(lang_code, next_url), content_type='application/json') | ||
| self.assertEqual(response.status_code, 200) | ||
| self.assertEqual( | ||
| response.content.decode("utf-8"), | ||
| translate_url(reverse("base"), "en"), | ||
| ) | ||
| self.assertFalse(LANGUAGE_SESSION_KEY in self.client.session) | ||
|
|
||
| def test_setlang_get(self): | ||
| """ | ||
| The set_language view is forbidden to be accessed via GET | ||
| """ | ||
| lang_code = self._get_inactive_language_code() | ||
| response = self.client.get(reverse("set_language"), params=self.set_post_data(lang_code), content_type='application/json') | ||
| self.assertEqual(type(response), HttpResponseNotAllowed) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add/replace the doc string here noting where this code came from and what you changed?