From 17b3dfcf3b331b76fa5a416002faaf9866ed681a Mon Sep 17 00:00:00 2001
From: Prathamesh Desai
Date: Mon, 3 Oct 2022 19:31:37 +0530
Subject: [PATCH 1/7] Work in progress
---
.../frontend/shared/languageSwitcher/mixin.js | 1 +
.../frontend/shared/views/AppBar.vue | 27 +++-
contentcuration/contentcuration/urls.py | 2 +-
contentcuration/contentcuration/views/base.py | 149 +++++++++++++-----
4 files changed, 133 insertions(+), 46 deletions(-)
diff --git a/contentcuration/contentcuration/frontend/shared/languageSwitcher/mixin.js b/contentcuration/contentcuration/frontend/shared/languageSwitcher/mixin.js
index 8b75b03191..682c676eae 100644
--- a/contentcuration/contentcuration/frontend/shared/languageSwitcher/mixin.js
+++ b/contentcuration/contentcuration/frontend/shared/languageSwitcher/mixin.js
@@ -1,4 +1,5 @@
import { availableLanguages, currentLanguage, sortLanguages } from '../i18n';
+
import client from 'shared/client';
export default {
diff --git a/contentcuration/contentcuration/frontend/shared/views/AppBar.vue b/contentcuration/contentcuration/frontend/shared/views/AppBar.vue
index e018472528..f437e06e51 100644
--- a/contentcuration/contentcuration/frontend/shared/views/AppBar.vue
+++ b/contentcuration/contentcuration/frontend/shared/views/AppBar.vue
@@ -75,9 +75,30 @@
-
- {{ $tr('logIn') }}
-
+
+
+
diff --git a/contentcuration/contentcuration/urls.py b/contentcuration/contentcuration/urls.py
index 4f0e5358e6..0eb12e45b2 100644
--- a/contentcuration/contentcuration/urls.py
+++ b/contentcuration/contentcuration/urls.py
@@ -156,7 +156,7 @@ def get_redirect_url(self, *args, **kwargs):
# Include all URLS prefixed by language
urlpatterns += i18n_patterns(
re_path(r'^$', views.base, name='base'),
- re_path(r"^i18n/setlang/$", views.set_language, name="set_language"),
+ # re_path(r"^i18n/setlang/$", views.set_language, name="set_language"),
re_path(r'^channels/$', views.channel_list, name='channels'),
# Redirect deprecated staging URL to new URL
re_path(r'^channels/(?P[^/]{32})/staging/$', StagingPageRedirectView.as_view(), name='staging_redirect'),
diff --git a/contentcuration/contentcuration/views/base.py b/contentcuration/contentcuration/views/base.py
index 3347513239..92fbdefc77 100644
--- a/contentcuration/contentcuration/views/base.py
+++ b/contentcuration/contentcuration/views/base.py
@@ -16,12 +16,15 @@
from django.http import HttpResponse
from django.http import HttpResponseForbidden
from django.http import HttpResponseNotFound
+from django.http import HttpResponseRedirect
from django.shortcuts import redirect
from django.shortcuts import render
from django.urls import is_valid_path
from django.urls import reverse
from django.urls import reverse_lazy
from django.urls import translate_url
+from django.utils.http import url_has_allowed_host_and_scheme
+from django.utils.translation import check_for_language
from django.utils.translation import get_language
from django.utils.translation import LANGUAGE_SESSION_KEY
from django.views.decorators.http import require_POST
@@ -341,54 +344,116 @@ def activate_channel_endpoint(request):
return HttpResponse(json.dumps({"success": True}))
-
# Taken from kolibri.core.views which was
# modified from django.views.i18n
-@require_POST
+# @require_POST
+# 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.
+# """
+# payload = json.loads(request.body)
+# lang_code = payload.get(LANGUAGE_QUERY_PARAMETER)
+# print(lang_code)
+# next_url = urlsplit(payload.get("next")) if payload.get("next") else None
+# if lang_code and lang_code in SUPPORTED_LANGUAGES:
+# if next_url and is_valid_path(next_url.path):
+# # If it is a recognized 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:
+# # Just redirect to the base URL w/ the lang_code
+# next_path = translate_url(reverse('base'), lang_code)
+# response = HttpResponse(next_path)
+# if hasattr(request, "session"):
+# request.session[LANGUAGE_SESSION_KEY] = lang_code
+# 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,
+# secure=settings.LANGUAGE_COOKIE_SECURE,
+# httponly=settings.LANGUAGE_COOKIE_HTTPONLY,
+# samesite=settings.LANGUAGE_COOKIE_SAMESITE,
+# )
+# else:
+# lang_code = get_language()
+# if next_url and is_valid_path(next_url.path):
+# # If it is a recognized 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:
+# # Just redirect to the base URL w/ the lang_code, likely the default language
+# next_path = translate_url(reverse('base'), 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
+
+
+@api_view(['POST', 'GET'])
def set_language(request):
"""
+ Redirect to a given URL while setting the chosen language in the session
+ (if enabled) and in a cookie. The URL and the language code need to be
+ specified in the request parameters.
+
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.
+ redirect to the page in the request (the 'next' parameter) without changing
+ any state.
"""
- payload = json.loads(request.body)
- lang_code = payload.get(LANGUAGE_QUERY_PARAMETER)
- next_url = urlsplit(payload.get("next")) if payload.get("next") else None
- if lang_code and lang_code in SUPPORTED_LANGUAGES:
- if next_url and is_valid_path(next_url.path):
- # If it is a recognized 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:
- # Just redirect to the base URL w/ the lang_code
- next_path = translate_url(reverse('base'), lang_code)
- response = HttpResponse(next_path)
- if hasattr(request, "session"):
- request.session[LANGUAGE_SESSION_KEY] = lang_code
- else:
- lang_code = get_language()
- if next_url and is_valid_path(next_url.path):
- # If it is a recognized 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],
- )
+ next_url = request.POST.get('next', request.GET.get('next'))
+ if (
+ (next_url or request.accepts('text/html')) and
+ not url_has_allowed_host_and_scheme(
+ url=next_url,
+ allowed_hosts={request.get_host()},
+ require_https=request.is_secure(),
+ )
+ ):
+ next_url = request.META.get('HTTP_REFERER')
+ if not url_has_allowed_host_and_scheme(
+ url=next_url,
+ allowed_hosts={request.get_host()},
+ require_https=request.is_secure(),
+ ):
+ next_url = '/'
+ response = HttpResponseRedirect(next_url) if next_url else HttpResponse(status=204)
+ if request.method == 'POST':
+ lang_code = request.POST.get(LANGUAGE_QUERY_PARAMETER)
+ if lang_code and check_for_language(lang_code):
+ if next_url:
+ next_trans = translate_url(next_url, lang_code)
+ if next_trans != next_url:
+ response = HttpResponseRedirect(next_trans)
+ if hasattr(request, 'session'):
+ # Storing the language in the session is deprecated.
+ # (RemovedInDjango40Warning)
+ request.session[LANGUAGE_SESSION_KEY] = lang_code
+ 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,
+ secure=settings.LANGUAGE_COOKIE_SECURE,
+ httponly=settings.LANGUAGE_COOKIE_HTTPONLY,
+ samesite=settings.LANGUAGE_COOKIE_SAMESITE,
)
- else:
- # Just redirect to the base URL w/ the lang_code, likely the default language
- next_path = translate_url(reverse('base'), lang_code)
- response = HttpResponse(next_path)
- if hasattr(request, "session"):
- request.session.pop(LANGUAGE_SESSION_KEY, "")
return response
From 40513c0bcaa2d5517696f8d65377abc61d3747ed Mon Sep 17 00:00:00 2001
From: Prathamesh Desai
Date: Thu, 6 Oct 2022 22:10:47 +0530
Subject: [PATCH 2/7] final implementation with tests
---
.../contentcuration/tests/test_setlanguage.py | 150 ++++++++++++++++++
contentcuration/contentcuration/urls.py | 2 +-
contentcuration/contentcuration/views/base.py | 102 +++---------
3 files changed, 174 insertions(+), 80 deletions(-)
create mode 100644 contentcuration/contentcuration/tests/test_setlanguage.py
diff --git a/contentcuration/contentcuration/tests/test_setlanguage.py b/contentcuration/contentcuration/tests/test_setlanguage.py
new file mode 100644
index 0000000000..941db98f5d
--- /dev/null
+++ b/contentcuration/contentcuration/tests/test_setlanguage.py
@@ -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)
diff --git a/contentcuration/contentcuration/urls.py b/contentcuration/contentcuration/urls.py
index 0eb12e45b2..4f0e5358e6 100644
--- a/contentcuration/contentcuration/urls.py
+++ b/contentcuration/contentcuration/urls.py
@@ -156,7 +156,7 @@ def get_redirect_url(self, *args, **kwargs):
# Include all URLS prefixed by language
urlpatterns += i18n_patterns(
re_path(r'^$', views.base, name='base'),
- # re_path(r"^i18n/setlang/$", views.set_language, name="set_language"),
+ re_path(r"^i18n/setlang/$", views.set_language, name="set_language"),
re_path(r'^channels/$', views.channel_list, name='channels'),
# Redirect deprecated staging URL to new URL
re_path(r'^channels/(?P[^/]{32})/staging/$', StagingPageRedirectView.as_view(), name='staging_redirect'),
diff --git a/contentcuration/contentcuration/views/base.py b/contentcuration/contentcuration/views/base.py
index 92fbdefc77..ac4d93cbf8 100644
--- a/contentcuration/contentcuration/views/base.py
+++ b/contentcuration/contentcuration/views/base.py
@@ -1,7 +1,5 @@
import json
from builtins import str
-from urllib.parse import urlsplit
-from urllib.parse import urlunsplit
from django.conf import settings
from django.contrib.auth.decorators import login_required
@@ -16,7 +14,6 @@
from django.http import HttpResponse
from django.http import HttpResponseForbidden
from django.http import HttpResponseNotFound
-from django.http import HttpResponseRedirect
from django.shortcuts import redirect
from django.shortcuts import render
from django.urls import is_valid_path
@@ -54,7 +51,6 @@
from contentcuration.models import License
from contentcuration.models import TaskResult
from contentcuration.serializers import SimplifiedChannelProbeCheckSerializer
-from contentcuration.utils.i18n import SUPPORTED_LANGUAGES
from contentcuration.utils.messages import get_messages
from contentcuration.viewsets.channelset import PublicChannelSetSerializer
@@ -344,82 +340,19 @@ def activate_channel_endpoint(request):
return HttpResponse(json.dumps({"success": True}))
-# Taken from kolibri.core.views which was
-# modified from django.views.i18n
-# @require_POST
-# 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.
-# """
-# payload = json.loads(request.body)
-# lang_code = payload.get(LANGUAGE_QUERY_PARAMETER)
-# print(lang_code)
-# next_url = urlsplit(payload.get("next")) if payload.get("next") else None
-# if lang_code and lang_code in SUPPORTED_LANGUAGES:
-# if next_url and is_valid_path(next_url.path):
-# # If it is a recognized 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:
-# # Just redirect to the base URL w/ the lang_code
-# next_path = translate_url(reverse('base'), lang_code)
-# response = HttpResponse(next_path)
-# if hasattr(request, "session"):
-# request.session[LANGUAGE_SESSION_KEY] = lang_code
-# 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,
-# secure=settings.LANGUAGE_COOKIE_SECURE,
-# httponly=settings.LANGUAGE_COOKIE_HTTPONLY,
-# samesite=settings.LANGUAGE_COOKIE_SAMESITE,
-# )
-# else:
-# lang_code = get_language()
-# if next_url and is_valid_path(next_url.path):
-# # If it is a recognized 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:
-# # Just redirect to the base URL w/ the lang_code, likely the default language
-# next_path = translate_url(reverse('base'), 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
-
-
-@api_view(['POST', 'GET'])
+
+@require_POST
+# flake8: noqa: C901
def set_language(request):
"""
- Redirect to a given URL while setting the chosen language in the session
- (if enabled) and in a cookie. The URL and the language code need to be
- specified in the request parameters.
-
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
- redirect to the page in the request (the 'next' parameter) without changing
- any state.
+ error.
"""
- next_url = request.POST.get('next', request.GET.get('next'))
+ payload = json.loads(request.body)
+ lang_code = payload.get(LANGUAGE_QUERY_PARAMETER)
+ next_url = payload.get("next")
+
if (
(next_url or request.accepts('text/html')) and
not url_has_allowed_host_and_scheme(
@@ -434,15 +367,16 @@ def set_language(request):
allowed_hosts={request.get_host()},
require_https=request.is_secure(),
):
- next_url = '/'
- response = HttpResponseRedirect(next_url) if next_url else HttpResponse(status=204)
+ next_url = translate_url(reverse('base'), lang_code)
+ if next_url and not is_valid_path(next_url):
+ next_url = translate_url(reverse('base'), lang_code)
+ response = HttpResponse(next_url) if next_url else HttpResponse(status=204)
if request.method == 'POST':
- lang_code = request.POST.get(LANGUAGE_QUERY_PARAMETER)
if lang_code and check_for_language(lang_code):
if next_url:
next_trans = translate_url(next_url, lang_code)
if next_trans != next_url:
- response = HttpResponseRedirect(next_trans)
+ response = HttpResponse(next_trans)
if hasattr(request, 'session'):
# Storing the language in the session is deprecated.
# (RemovedInDjango40Warning)
@@ -456,4 +390,14 @@ def set_language(request):
httponly=settings.LANGUAGE_COOKIE_HTTPONLY,
samesite=settings.LANGUAGE_COOKIE_SAMESITE,
)
+ else:
+ lang_code = get_language()
+ if lang_code and check_for_language(lang_code):
+ if next_url:
+ next_trans = translate_url(next_url, lang_code)
+ if next_trans != next_url:
+ response = HttpResponse(next_trans)
+ if hasattr(request, "session"):
+ request.session.pop(LANGUAGE_SESSION_KEY, "")
+
return response
From b558ca7e421f3ea33139adca34e010745ba32b40 Mon Sep 17 00:00:00 2001
From: Prathamesh Desai
Date: Thu, 6 Oct 2022 23:56:59 +0530
Subject: [PATCH 3/7] set language cookie age
---
contentcuration/contentcuration/settings.py | 2 ++
1 file changed, 2 insertions(+)
diff --git a/contentcuration/contentcuration/settings.py b/contentcuration/contentcuration/settings.py
index bd857d651b..1c7d962d9f 100644
--- a/contentcuration/contentcuration/settings.py
+++ b/contentcuration/contentcuration/settings.py
@@ -427,3 +427,5 @@ def gettext(s):
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
+
+LANGUAGE_COOKIE_AGE = 3600 * 24 * 14
From e41eed0e39fd43c0d39c36e52d5fb37b4ef62910 Mon Sep 17 00:00:00 2001
From: Prathamesh Desai
Date: Wed, 12 Oct 2022 21:10:54 +0530
Subject: [PATCH 4/7] fix valid_path bug
---
.../contentcuration/frontend/serviceWorker/index.js | 1 +
contentcuration/contentcuration/views/base.py | 5 ++++-
2 files changed, 5 insertions(+), 1 deletion(-)
diff --git a/contentcuration/contentcuration/frontend/serviceWorker/index.js b/contentcuration/contentcuration/frontend/serviceWorker/index.js
index 95b5fbfb8c..1c6108064e 100644
--- a/contentcuration/contentcuration/frontend/serviceWorker/index.js
+++ b/contentcuration/contentcuration/frontend/serviceWorker/index.js
@@ -12,6 +12,7 @@
*/
import { precacheAndRoute } from 'workbox-precaching';
+console.log(self.__WB_MANIFEST);
precacheAndRoute(self.__WB_MANIFEST);
addEventListener('message', event => {
diff --git a/contentcuration/contentcuration/views/base.py b/contentcuration/contentcuration/views/base.py
index ac4d93cbf8..bbdce92823 100644
--- a/contentcuration/contentcuration/views/base.py
+++ b/contentcuration/contentcuration/views/base.py
@@ -1,5 +1,6 @@
import json
from builtins import str
+from urllib.parse import urlsplit
from django.conf import settings
from django.contrib.auth.decorators import login_required
@@ -352,6 +353,7 @@ def set_language(request):
payload = json.loads(request.body)
lang_code = payload.get(LANGUAGE_QUERY_PARAMETER)
next_url = payload.get("next")
+ # next_url = urlsplit(payload.get("next")) if payload.get("next") else None
if (
(next_url or request.accepts('text/html')) and
@@ -368,7 +370,8 @@ def set_language(request):
require_https=request.is_secure(),
):
next_url = translate_url(reverse('base'), lang_code)
- if next_url and not is_valid_path(next_url):
+ next_url_split = urlsplit(next_url) if next_url else None
+ if next_url and not is_valid_path(next_url_split.path):
next_url = translate_url(reverse('base'), lang_code)
response = HttpResponse(next_url) if next_url else HttpResponse(status=204)
if request.method == 'POST':
From 140b644f2fbaa4c0a9123533dc8db878a1db8b11 Mon Sep 17 00:00:00 2001
From: Prathamesh Desai
Date: Wed, 12 Oct 2022 21:19:58 +0530
Subject: [PATCH 5/7] Remove console log
---
contentcuration/contentcuration/frontend/serviceWorker/index.js | 1 -
1 file changed, 1 deletion(-)
diff --git a/contentcuration/contentcuration/frontend/serviceWorker/index.js b/contentcuration/contentcuration/frontend/serviceWorker/index.js
index 1c6108064e..95b5fbfb8c 100644
--- a/contentcuration/contentcuration/frontend/serviceWorker/index.js
+++ b/contentcuration/contentcuration/frontend/serviceWorker/index.js
@@ -12,7 +12,6 @@
*/
import { precacheAndRoute } from 'workbox-precaching';
-console.log(self.__WB_MANIFEST);
precacheAndRoute(self.__WB_MANIFEST);
addEventListener('message', event => {
From aa6f998d095aad6215070ed869980a72f2b09f2b Mon Sep 17 00:00:00 2001
From: Prathamesh Desai
Date: Fri, 14 Oct 2022 18:28:31 +0530
Subject: [PATCH 6/7] broken change language in main app drawer
---
.../shared/views/MainNavigationDrawer.vue | 24 +++++++++++++++++++
.../templates/pwa/service_worker.js | 1 -
contentcuration/contentcuration/views/base.py | 1 -
3 files changed, 24 insertions(+), 2 deletions(-)
diff --git a/contentcuration/contentcuration/frontend/shared/views/MainNavigationDrawer.vue b/contentcuration/contentcuration/frontend/shared/views/MainNavigationDrawer.vue
index 32f4a95ed6..c5065c49ac 100644
--- a/contentcuration/contentcuration/frontend/shared/views/MainNavigationDrawer.vue
+++ b/contentcuration/contentcuration/frontend/shared/views/MainNavigationDrawer.vue
@@ -32,6 +32,7 @@
{{ $tr('administrationLink') }}
+
@@ -41,6 +42,14 @@
{{ $tr('settingsLink') }}
+
+
+ language
+
+
+
+
+
open_in_new
@@ -73,8 +82,15 @@
/>
+
+
+
@@ -84,11 +100,13 @@
import { mapActions, mapState } from 'vuex';
import KolibriLogo from './KolibriLogo';
+ import LanguageSwitcherModal from 'shared/languageSwitcher/LanguageSwitcherModal';
export default {
name: 'MainNavigationDrawer',
components: {
KolibriLogo,
+ LanguageSwitcherModal,
},
props: {
value: {
@@ -96,6 +114,11 @@
default: false,
},
},
+ data() {
+ return {
+ showLanguageModal: false,
+ };
+ },
computed: {
...mapState({
user: state => state.session.currentUser,
@@ -131,6 +154,7 @@
channelsLink: 'Channels',
administrationLink: 'Administration',
settingsLink: 'Settings',
+ changeLanguage: 'Change language',
helpLink: 'Help and support',
logoutLink: 'Sign out',
copyright: '© {year} Learning Equality',
diff --git a/contentcuration/contentcuration/templates/pwa/service_worker.js b/contentcuration/contentcuration/templates/pwa/service_worker.js
index 583a43bdf2..f69abd79bf 100644
--- a/contentcuration/contentcuration/templates/pwa/service_worker.js
+++ b/contentcuration/contentcuration/templates/pwa/service_worker.js
@@ -1,6 +1,5 @@
{% load js_reverse %}
-{% js_reverse_inline %}
{% autoescape off %}
{{ webpack_service_worker }}
diff --git a/contentcuration/contentcuration/views/base.py b/contentcuration/contentcuration/views/base.py
index bbdce92823..d415778aab 100644
--- a/contentcuration/contentcuration/views/base.py
+++ b/contentcuration/contentcuration/views/base.py
@@ -353,7 +353,6 @@ def set_language(request):
payload = json.loads(request.body)
lang_code = payload.get(LANGUAGE_QUERY_PARAMETER)
next_url = payload.get("next")
- # next_url = urlsplit(payload.get("next")) if payload.get("next") else None
if (
(next_url or request.accepts('text/html')) and
From 8d9d5036d113479e39a5388a7f56b40bff9a4088 Mon Sep 17 00:00:00 2001
From: ozer550
Date: Wed, 2 Nov 2022 23:52:16 +0530
Subject: [PATCH 7/7] update docstring
---
contentcuration/contentcuration/views/base.py | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/contentcuration/contentcuration/views/base.py b/contentcuration/contentcuration/views/base.py
index d415778aab..9a7339481c 100644
--- a/contentcuration/contentcuration/views/base.py
+++ b/contentcuration/contentcuration/views/base.py
@@ -346,6 +346,12 @@ def activate_channel_endpoint(request):
# flake8: noqa: C901
def set_language(request):
"""
+ We are using set_language from official Django set_language redirect view
+ https://docs.djangoproject.com/en/3.2/_modules/django/views/i18n/#set_language
+ and slighty modifying it according to our use case as we donot use AJAX, hence we donot
+ redirect rather just respond the required URL!
+
+
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.