Skip to content

Commit

Permalink
feat: allow to embed translated dashboard
Browse files Browse the repository at this point in the history
fix: allow to translate Analytics tab title

test: fix tests
  • Loading branch information
Ian2012 committed Apr 8, 2024
1 parent 1cfaf56 commit 47a0e3b
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 99 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ omit =
*/static/*
*/templates/*
**/tests/*
*/settings/*
10 changes: 8 additions & 2 deletions platform_plugin_aspects/extensions/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from openedx_filters import PipelineStep
from web_fragments.fragment import Fragment

from platform_plugin_aspects.utils import generate_superset_context
from platform_plugin_aspects.utils import _, generate_superset_context, get_model

TEMPLATE_ABSOLUTE_PATH = "/instructor_dashboard/"
BLOCK_CATEGORY = "aspects"
Expand Down Expand Up @@ -42,11 +42,17 @@ def run_filter(

user = get_current_user()

language = get_model("user_preference").get_value(user, "pref-lang") or "en_US"
formatted_language = language.replace("-", "_")
if formatted_language not in settings.SUPERSET_DASHBOARD_LOCALES:
formatted_language = None

context = generate_superset_context(
context,
user,
dashboards=dashboards,
filters=filters,
language=formatted_language,
)

template = Template(self.resource_string("static/html/superset.html"))
Expand All @@ -57,7 +63,7 @@ def run_filter(
section_data = {
"fragment": frag,
"section_key": BLOCK_CATEGORY,
"section_display_name": BLOCK_CATEGORY.title(),
"section_display_name": _("Analytics"),
"course_id": str(course.id),
"superset_url": str(context.get("superset_url")),
"template_path_prefix": TEMPLATE_ABSOLUTE_PATH,
Expand Down
44 changes: 41 additions & 3 deletions platform_plugin_aspects/extensions/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
Tests for the filters module.
"""

from unittest import TestCase
from unittest.mock import Mock, patch

from django.conf import settings
from django.test import TestCase

from platform_plugin_aspects.extensions.filters import BLOCK_CATEGORY, AddSupersetTab


Expand All @@ -22,7 +24,10 @@ def setUp(self) -> None:
self.context = {"course": Mock()}

@patch("platform_plugin_aspects.extensions.filters.generate_superset_context")
def test_run_filter(self, mock_generate_superset_context):
@patch("platform_plugin_aspects.extensions.filters.get_model")
def test_run_filter_with_language(
self, mock_get_model, mock_generate_superset_context
):
"""
Check the filter is not executed when there are no LimeSurvey blocks in the course.
Expand All @@ -34,13 +39,46 @@ def test_run_filter(self, mock_generate_superset_context):
"superset_url": "http://superset.testing",
}

mock_get_model.return_value.get_value.return_value = "not-a-language"

context = self.filter.run_filter(self.context, self.template_name)

self.assertDictContainsSubset(
{
"course_id": str(self.context["course"].id),
"section_key": BLOCK_CATEGORY,
"section_display_name": "Analytics",
"superset_url": "http://superset.testing",
"template_path_prefix": "/instructor_dashboard/",
},
context["context"]["sections"][0],
)

@patch("platform_plugin_aspects.extensions.filters.generate_superset_context")
@patch("platform_plugin_aspects.extensions.filters.get_model")
def test_run_filter_without_language(
self, mock_get_model, mock_generate_superset_context
):
"""
Check the filter is not executed when there are no LimeSurvey blocks in the course.
Expected result:
- The context is returned without modifications.
"""
mock_generate_superset_context.return_value = {
"sections": [],
"superset_url": "http://superset.testing",
}

mock_get_model.return_value.get_value.return_value = None

context = self.filter.run_filter(self.context, self.template_name)

self.assertDictContainsSubset(
{
"course_id": str(self.context["course"].id),
"section_key": BLOCK_CATEGORY,
"section_display_name": BLOCK_CATEGORY.title(),
"section_display_name": "Analytics",
"superset_url": "http://superset.testing",
"template_path_prefix": "/instructor_dashboard/",
},
Expand Down
7 changes: 7 additions & 0 deletions platform_plugin_aspects/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ def plugin_settings(settings):
settings.ASPECTS_INSTRUCTOR_DASHBOARDS = [
{
"name": "Instructor Dashboard",
"slug": "instructor-dashboard",
"uuid": "1d6bf904-f53f-47fd-b1c9-6cd7e284d286",
"allow_translations": True,
},
]
settings.SUPERSET_EXTRA_FILTERS_FORMAT = []
settings.SUPERSET_DASHBOARD_LOCALES = ["en_US"]
settings.EVENT_SINK_CLICKHOUSE_BACKEND_CONFIG = {
# URL to a running ClickHouse server's HTTP interface. ex: https://foo.openedx.org:8443/ or
# http://foo.openedx.org:8123/ . Note that we only support the ClickHouse HTTP interface
Expand Down Expand Up @@ -64,4 +67,8 @@ def plugin_settings(settings):
"module": "lms.djangoapps.ccx.models",
"model": "CustomCourseForEdX",
},
"user_preference": {
"module": "openedx.core.djangoapps.user_api.models",
"model": "UserPreference",
},
}
3 changes: 3 additions & 0 deletions platform_plugin_aspects/settings/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def plugin_settings(settings):
settings.SUPERSET_EXTRA_FILTERS_FORMAT = getattr(settings, "ENV_TOKENS", {}).get(
"SUPERSET_EXTRA_FILTERS_FORMAT", settings.SUPERSET_EXTRA_FILTERS_FORMAT
)
settings.SUPERSET_DASHBOARD_LOCALES = getattr(settings, "ENV_TOKENS", {}).get(
"SUPERSET_DASHBOARD_LOCALES", settings.SUPERSET_DASHBOARD_LOCALES
)
settings.EVENT_SINK_CLICKHOUSE_BACKEND_CONFIG = settings.ENV_TOKENS.get(
"EVENT_SINK_CLICKHOUSE_BACKEND_CONFIG",
settings.EVENT_SINK_CLICKHOUSE_BACKEND_CONFIG,
Expand Down
85 changes: 0 additions & 85 deletions platform_plugin_aspects/settings/tests/test_settings.py

This file was deleted.

14 changes: 12 additions & 2 deletions platform_plugin_aspects/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"""

from collections import namedtuple
from unittest import TestCase
from unittest.mock import Mock, patch

from django.conf import settings
from django.test import TestCase

from platform_plugin_aspects.utils import (
generate_superset_context,
Expand Down Expand Up @@ -113,14 +113,24 @@ def test_generate_superset_context(self, mock_generate_guest_token):
filter_mock = Mock()
user_mock = Mock()
context = {"course": course_mock}
dashboards = [{"name": "test", "uuid": "test-dashboard-uuid"}]
dashboards = settings.ASPECTS_INSTRUCTOR_DASHBOARDS

dashboards.append(
{
"slug": "test-slug",
"uuid": "3ea6e738-989d-4325-8f93-82bb684dab5c",
"allow_translations": False,
}
)

mock_generate_guest_token.return_value = ("test-token", dashboards)

context = generate_superset_context(
context,
user_mock,
dashboards=dashboards,
filters=[filter_mock],
language="en_US",
)

self.assertEqual(context["superset_token"], "test-token")
Expand Down
19 changes: 19 additions & 0 deletions platform_plugin_aspects/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import logging
import os
import uuid
from importlib import import_module

from django.conf import settings
Expand All @@ -30,6 +31,7 @@ def generate_superset_context( # pylint: disable=dangerous-default-value
user,
dashboards,
filters=[],
language=None,
):
"""
Update context with superset token and dashboard id.
Expand All @@ -40,10 +42,18 @@ def generate_superset_context( # pylint: disable=dangerous-default-value
superset_config (dict): superset config.
dashboards (list): list of superset dashboard uuid.
filters (list): list of filters to apply to the dashboard.
language (str): the language code of the end user.
"""
course = context["course"]
superset_config = settings.SUPERSET_CONFIG

if language:
for dashboard in dashboards:
if not dashboard["allow_translations"]:
continue
dashboard["slug"] = f"{dashboard['slug']}-{language}"
dashboard["uuid"] = str(get_uuid5(dashboard["uuid"], language))

superset_token, dashboards = _generate_guest_token(
user=user,
course=course,
Expand Down Expand Up @@ -220,3 +230,12 @@ def get_ccx_courses(course_id):
if settings.FEATURES.get("CUSTOM_COURSES_EDX"):
return get_model("custom_course_edx").objects.filter(course_id=course_id)
return []


def get_uuid5(base_uuid, language):
"""
Generate an idempotent uuid.
"""
base_uuid = uuid.UUID(base_uuid)
base_namespace = uuid.uuid5(base_uuid, "superset")
return uuid.uuid5(base_namespace, language)
31 changes: 24 additions & 7 deletions test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,6 @@
}
]

ASPECTS_INSTRUCTOR_DASHBOARDS = [
{
"name": "Instructor Dashboard",
"uuid": "1d6bf904-f53f-47fd-b1c9-6cd7e284d286",
},
]

SUPERSET_EXTRA_FILTERS_FORMAT = []

SUPERSET_CONFIG = {
Expand All @@ -73,3 +66,27 @@
"username": "superset",
"password": "superset",
}

SUPERSET_CONFIG = {
"internal_service_url": "http://superset:8088",
"service_url": "http://superset.local.overhang.io",
"username": "superset",
"password": "superset",
}
ASPECTS_INSTRUCTOR_DASHBOARDS = [
{
"name": "Instructor Dashboard",
"slug": "instructor-dashboard",
"uuid": "1d6bf904-f53f-47fd-b1c9-6cd7e284d286",
"allow_translations": True,
}
]
EVENT_SINK_CLICKHOUSE_BACKEND_CONFIG = {
"url": "https://foo.bar",
"username": "bob",
"password": "secret",
"database": "cool_data",
"timeout_secs": 1,
}

SUPERSET_DASHBOARD_LOCALES = ["en_US", "es_419"]

0 comments on commit 47a0e3b

Please sign in to comment.