platform-plugin-aspects 2.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- platform_plugin_aspects/__init__.py +11 -0
- platform_plugin_aspects/apps.py +77 -0
- platform_plugin_aspects/conf/locale/babel_mako.cfg +10 -0
- platform_plugin_aspects/conf/locale/config.yaml +85 -0
- platform_plugin_aspects/conf/locale/en/LC_MESSAGES/django.mo +0 -0
- platform_plugin_aspects/conf/locale/en/LC_MESSAGES/django.po +99 -0
- platform_plugin_aspects/conf/locale/en/LC_MESSAGES/mako.mo +0 -0
- platform_plugin_aspects/conf/locale/en/LC_MESSAGES/mako.po +23 -0
- platform_plugin_aspects/conf/locale/eo/LC_MESSAGES/django.mo +0 -0
- platform_plugin_aspects/conf/locale/eo/LC_MESSAGES/django.po +70 -0
- platform_plugin_aspects/conf/locale/eo/LC_MESSAGES/mako.mo +0 -0
- platform_plugin_aspects/conf/locale/eo/LC_MESSAGES/mako.po +23 -0
- platform_plugin_aspects/extensions/__init__.py +0 -0
- platform_plugin_aspects/extensions/filters.py +108 -0
- platform_plugin_aspects/management/__init__.py +0 -0
- platform_plugin_aspects/management/commands/__init__.py +0 -0
- platform_plugin_aspects/management/commands/dump_data_to_clickhouse.py +214 -0
- platform_plugin_aspects/management/commands/load_test_tracking_events.py +310 -0
- platform_plugin_aspects/management/commands/monitor_load_test_tracking.py +440 -0
- platform_plugin_aspects/settings/__init__.py +0 -0
- platform_plugin_aspects/settings/common.py +147 -0
- platform_plugin_aspects/settings/production.py +38 -0
- platform_plugin_aspects/signals.py +305 -0
- platform_plugin_aspects/sinks/__init__.py +11 -0
- platform_plugin_aspects/sinks/base_sink.py +381 -0
- platform_plugin_aspects/sinks/course_enrollment_sink.py +20 -0
- platform_plugin_aspects/sinks/course_overview_sink.py +286 -0
- platform_plugin_aspects/sinks/external_id_sink.py +20 -0
- platform_plugin_aspects/sinks/serializers.py +274 -0
- platform_plugin_aspects/sinks/tag_sink.py +47 -0
- platform_plugin_aspects/sinks/user_profile_sink.py +20 -0
- platform_plugin_aspects/sinks/user_retire_sink.py +50 -0
- platform_plugin_aspects/static/css/superset.css +53 -0
- platform_plugin_aspects/static/html/superset.html +39 -0
- platform_plugin_aspects/static/html/superset_student.html +5 -0
- platform_plugin_aspects/static/js/embed_dashboard.js +94 -0
- platform_plugin_aspects/static/js/install_required.js +11 -0
- platform_plugin_aspects/static/js/superset.js +38 -0
- platform_plugin_aspects/tasks.py +64 -0
- platform_plugin_aspects/templates/instructor_dashboard/aspects.html +22 -0
- platform_plugin_aspects/urls.py +39 -0
- platform_plugin_aspects/utils.py +359 -0
- platform_plugin_aspects/views.py +296 -0
- platform_plugin_aspects/waffle.py +5 -0
- platform_plugin_aspects/xblock.py +239 -0
- platform_plugin_aspects-2.0.1.dist-info/METADATA +335 -0
- platform_plugin_aspects-2.0.1.dist-info/RECORD +51 -0
- platform_plugin_aspects-2.0.1.dist-info/WHEEL +5 -0
- platform_plugin_aspects-2.0.1.dist-info/entry_points.txt +8 -0
- platform_plugin_aspects-2.0.1.dist-info/licenses/LICENSE +201 -0
- platform_plugin_aspects-2.0.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Utilities for the Aspects app.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import copy
|
|
8
|
+
import logging
|
|
9
|
+
import os
|
|
10
|
+
import uuid
|
|
11
|
+
from importlib import import_module
|
|
12
|
+
from urllib.parse import urljoin
|
|
13
|
+
|
|
14
|
+
from django.conf import settings
|
|
15
|
+
from django.core.exceptions import ImproperlyConfigured
|
|
16
|
+
from django.urls import reverse
|
|
17
|
+
from requests.exceptions import HTTPError
|
|
18
|
+
from supersetapiclient.client import SupersetClient
|
|
19
|
+
from xblock.reference.user_service import XBlockUser
|
|
20
|
+
|
|
21
|
+
logger = logging.getLogger(__name__)
|
|
22
|
+
|
|
23
|
+
if settings.DEBUG: # pragma: no cover
|
|
24
|
+
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _(text):
|
|
28
|
+
"""
|
|
29
|
+
Define a dummy `gettext` replacement to make string extraction tools scrape strings marked for translation.
|
|
30
|
+
"""
|
|
31
|
+
return text
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
DEFAULT_FILTERS_FORMAT = [
|
|
35
|
+
"org = '{course_id.org}'",
|
|
36
|
+
"course_key = '{course_id}'",
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
DEFAULT_USER_LANGUAGE = "en"
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def generate_superset_context(
|
|
44
|
+
context,
|
|
45
|
+
dashboards,
|
|
46
|
+
language=None,
|
|
47
|
+
) -> dict:
|
|
48
|
+
"""
|
|
49
|
+
Update context with superset token and dashboard id.
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
context (dict): the context for the instructor dashboard. It must include a course_id.
|
|
53
|
+
user (XBlockUser or User): the current user.
|
|
54
|
+
superset_config (dict): superset config.
|
|
55
|
+
dashboards (list): list of superset dashboard uuid.
|
|
56
|
+
filters (list): list of filters to apply to the dashboard.
|
|
57
|
+
language (str): the language code of the end user.
|
|
58
|
+
"""
|
|
59
|
+
course_id = context["course_id"]
|
|
60
|
+
superset_config = settings.SUPERSET_CONFIG
|
|
61
|
+
|
|
62
|
+
# We're modifying this, keep a local copy
|
|
63
|
+
rtn_dashboards = copy.deepcopy(dashboards)
|
|
64
|
+
|
|
65
|
+
if language:
|
|
66
|
+
for dashboard in rtn_dashboards:
|
|
67
|
+
if not dashboard.get("allow_translations"):
|
|
68
|
+
continue
|
|
69
|
+
dashboard["slug"] = f"{dashboard['slug']}-{language}"
|
|
70
|
+
dashboard["uuid"] = get_localized_uuid(dashboard["uuid"], language)
|
|
71
|
+
|
|
72
|
+
superset_url = _fix_service_url(superset_config.get("service_url"))
|
|
73
|
+
|
|
74
|
+
# Use an absolute LMS URL here, just in case we're being rendered in an MFE.
|
|
75
|
+
guest_token_url = urljoin(
|
|
76
|
+
settings.LMS_ROOT_URL,
|
|
77
|
+
reverse(
|
|
78
|
+
"platform_plugin_aspects:superset_guest_token",
|
|
79
|
+
kwargs={"course_id": course_id},
|
|
80
|
+
),
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
context.update(
|
|
84
|
+
{
|
|
85
|
+
"superset_dashboards": rtn_dashboards,
|
|
86
|
+
"superset_url": superset_url,
|
|
87
|
+
"superset_guest_token_url": guest_token_url,
|
|
88
|
+
}
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
return context
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def generate_guest_token(user, course, dashboards, filters) -> str:
|
|
95
|
+
"""
|
|
96
|
+
Generate and return a Superset guest token for the user.
|
|
97
|
+
|
|
98
|
+
Raise ImproperlyConfigured if the Superset API client request fails for any reason.
|
|
99
|
+
|
|
100
|
+
Args:
|
|
101
|
+
user: User object.
|
|
102
|
+
course: Course object, used to populate `filters` template strings.
|
|
103
|
+
dashboards: list of dashboard UUIDs to grant access to.
|
|
104
|
+
filters: list of string filters to apply.
|
|
105
|
+
|
|
106
|
+
Returns:
|
|
107
|
+
tuple: Superset guest token and dashboard id.
|
|
108
|
+
or None, exception if Superset is missconfigured or cannot generate guest token.
|
|
109
|
+
"""
|
|
110
|
+
superset_config = settings.SUPERSET_CONFIG
|
|
111
|
+
superset_internal_host = _fix_service_url(
|
|
112
|
+
superset_config.get("internal_service_url")
|
|
113
|
+
or superset_config.get("service_url")
|
|
114
|
+
)
|
|
115
|
+
superset_username = superset_config.get("username")
|
|
116
|
+
superset_password = superset_config.get("password")
|
|
117
|
+
|
|
118
|
+
formatted_filters = [
|
|
119
|
+
filter.format(course_id=course, user=user) for filter in filters
|
|
120
|
+
]
|
|
121
|
+
|
|
122
|
+
resources = []
|
|
123
|
+
|
|
124
|
+
# Get permissions for all localized versions of the dashboards
|
|
125
|
+
for dashboard in dashboards:
|
|
126
|
+
resources.append({"type": "dashboard", "id": dashboard["uuid"]})
|
|
127
|
+
|
|
128
|
+
if dashboard.get("allow_translations"):
|
|
129
|
+
for locale in settings.SUPERSET_DASHBOARD_LOCALES:
|
|
130
|
+
resources.append(
|
|
131
|
+
{
|
|
132
|
+
"type": "dashboard",
|
|
133
|
+
"id": get_localized_uuid(dashboard["uuid"], locale),
|
|
134
|
+
}
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
data = {
|
|
138
|
+
"user": _superset_user_data(user),
|
|
139
|
+
"resources": resources,
|
|
140
|
+
"rls": [{"clause": filter} for filter in formatted_filters],
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
try:
|
|
144
|
+
client = SupersetClient(
|
|
145
|
+
host=superset_internal_host,
|
|
146
|
+
username=superset_username,
|
|
147
|
+
password=superset_password,
|
|
148
|
+
)
|
|
149
|
+
response = client.session.post(
|
|
150
|
+
url=f"{superset_internal_host}api/v1/security/guest_token/",
|
|
151
|
+
json=data,
|
|
152
|
+
headers={"Content-Type": "application/json"},
|
|
153
|
+
)
|
|
154
|
+
response.raise_for_status()
|
|
155
|
+
token = response.json().get("token")
|
|
156
|
+
return token
|
|
157
|
+
|
|
158
|
+
# We manually log the error message here because ImproperlyConfigured is eaten up the stack
|
|
159
|
+
except HTTPError as exc:
|
|
160
|
+
err = f"""Unable to fetch Superset guest token, mostly likely due to invalid
|
|
161
|
+
settings.SUPERSET_CONFIG or because one of the dashboard UUIDs requested does not
|
|
162
|
+
exist in Superset.\n
|
|
163
|
+
{exc.response.status_code} {exc.response.json()} for url:
|
|
164
|
+
{exc.response.url}, data: {data}\n\n dashboards: {dashboards}"""
|
|
165
|
+
|
|
166
|
+
logger.error(err)
|
|
167
|
+
raise ImproperlyConfigured(err) from exc
|
|
168
|
+
|
|
169
|
+
except Exception as exc:
|
|
170
|
+
err = f"""Error trying to fetch Superset guest token, mostly likely due to invalid
|
|
171
|
+
settings.SUPERSET_CONFIG or because one of the dashboard UUIDs requested does not
|
|
172
|
+
exist in Superset. data: {data} \n\n exception: {exc}"""
|
|
173
|
+
logger.error(err)
|
|
174
|
+
raise ImproperlyConfigured(err) from exc
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def _fix_service_url(url: str) -> str:
|
|
178
|
+
"""
|
|
179
|
+
Append a trailing slash to the given url, if missing.
|
|
180
|
+
|
|
181
|
+
SupersetClient requires a trailing slash for service URLs.
|
|
182
|
+
"""
|
|
183
|
+
if url and url[-1] != "/":
|
|
184
|
+
url += "/"
|
|
185
|
+
return url
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def _superset_user_data(user: XBlockUser) -> dict:
|
|
189
|
+
"""
|
|
190
|
+
Return the user properties sent to the Superset API.
|
|
191
|
+
"""
|
|
192
|
+
# We can send more info about the user to superset
|
|
193
|
+
# but Open edX only provides the full name. For now is not needed
|
|
194
|
+
# and doesn't add any value so we don't send it.
|
|
195
|
+
# {
|
|
196
|
+
# "first_name": "John",
|
|
197
|
+
# "last_name": "Doe",
|
|
198
|
+
# }
|
|
199
|
+
username = None
|
|
200
|
+
# Django User
|
|
201
|
+
if hasattr(user, "username"):
|
|
202
|
+
username = user.username
|
|
203
|
+
else:
|
|
204
|
+
assert isinstance(user, XBlockUser)
|
|
205
|
+
username = user.opt_attrs.get("edx-platform.username")
|
|
206
|
+
|
|
207
|
+
return {
|
|
208
|
+
"username": username,
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def get_model(model_setting):
|
|
213
|
+
"""Load a model from a setting."""
|
|
214
|
+
MODEL_CONFIG = getattr(settings, "EVENT_SINK_CLICKHOUSE_MODEL_CONFIG", {})
|
|
215
|
+
|
|
216
|
+
model_config = MODEL_CONFIG.get(model_setting)
|
|
217
|
+
if not model_config:
|
|
218
|
+
logger.error("Unable to find model config for %s", model_setting)
|
|
219
|
+
return None
|
|
220
|
+
|
|
221
|
+
module = model_config.get("module")
|
|
222
|
+
if not module:
|
|
223
|
+
logger.error("Module was not specified in %s", model_setting)
|
|
224
|
+
return None
|
|
225
|
+
|
|
226
|
+
model_name = model_config.get("model")
|
|
227
|
+
if not model_name:
|
|
228
|
+
logger.error("Model was not specified in %s", model_setting)
|
|
229
|
+
return None
|
|
230
|
+
|
|
231
|
+
try:
|
|
232
|
+
model = getattr(import_module(module), model_name)
|
|
233
|
+
return model
|
|
234
|
+
except (ImportError, AttributeError, ModuleNotFoundError):
|
|
235
|
+
logger.error("Unable to load model %s.%s", module, model_name)
|
|
236
|
+
|
|
237
|
+
return None
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def get_modulestore(): # pragma: no cover
|
|
241
|
+
"""
|
|
242
|
+
Import and return modulestore.
|
|
243
|
+
|
|
244
|
+
Placed here to avoid model import at startup and to facilitate mocking them in testing.
|
|
245
|
+
"""
|
|
246
|
+
# pylint: disable=import-outside-toplevel,import-error
|
|
247
|
+
from xmodule.modulestore.django import modulestore
|
|
248
|
+
|
|
249
|
+
return modulestore()
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def get_detached_xblock_types(): # pragma: no cover
|
|
253
|
+
"""
|
|
254
|
+
Import and return DETACHED_XBLOCK_TYPES.
|
|
255
|
+
|
|
256
|
+
Placed here to avoid model import at startup and to facilitate mocking them in testing.
|
|
257
|
+
"""
|
|
258
|
+
# pylint: disable=import-outside-toplevel,import-error
|
|
259
|
+
from xmodule.modulestore.store_utilities import DETACHED_XBLOCK_TYPES
|
|
260
|
+
|
|
261
|
+
return DETACHED_XBLOCK_TYPES
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def get_ccx_courses(course_id):
|
|
265
|
+
"""
|
|
266
|
+
Get the CCX courses for a given course.
|
|
267
|
+
"""
|
|
268
|
+
if settings.FEATURES.get("CUSTOM_COURSES_EDX"):
|
|
269
|
+
return get_model("custom_course_edx").objects.filter(course_id=course_id)
|
|
270
|
+
return []
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def get_localized_uuid(base_uuid, language):
|
|
274
|
+
"""
|
|
275
|
+
Generate an idempotent uuid.
|
|
276
|
+
"""
|
|
277
|
+
base_uuid = uuid.UUID(base_uuid)
|
|
278
|
+
base_namespace = uuid.uuid5(base_uuid, "superset")
|
|
279
|
+
normalized_language = language.lower().replace("-", "_")
|
|
280
|
+
return str(uuid.uuid5(base_namespace, normalized_language))
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
def _get_object_tags(usage_key): # pragma: no cover
|
|
284
|
+
"""
|
|
285
|
+
Wrap the Open edX tagging API method get_object_tags.
|
|
286
|
+
"""
|
|
287
|
+
try:
|
|
288
|
+
# pylint: disable=import-outside-toplevel
|
|
289
|
+
from openedx.core.djangoapps.content_tagging.api import get_object_tags
|
|
290
|
+
|
|
291
|
+
return get_object_tags(object_id=str(usage_key))
|
|
292
|
+
# Pre-Redwood versions of Open edX don't have this API
|
|
293
|
+
except ImportError:
|
|
294
|
+
return {}
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
def get_tags_for_block(usage_key) -> set:
|
|
298
|
+
"""
|
|
299
|
+
Return all the tags (and their parent tags) applied to the given block.
|
|
300
|
+
|
|
301
|
+
Returns a set of tag id: [1, 2, 3]
|
|
302
|
+
"""
|
|
303
|
+
object_tags = _get_object_tags(usage_key)
|
|
304
|
+
serialized_tags = set()
|
|
305
|
+
for object_tag in object_tags:
|
|
306
|
+
tag = object_tag.tag
|
|
307
|
+
while tag:
|
|
308
|
+
serialized_tags.add(tag.id)
|
|
309
|
+
tag = tag.parent
|
|
310
|
+
|
|
311
|
+
return list(serialized_tags)
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
def get_user_dashboard_locale(user):
|
|
315
|
+
"""
|
|
316
|
+
|
|
317
|
+
Get the Superset dashboard locale from the user preference.
|
|
318
|
+
|
|
319
|
+
"""
|
|
320
|
+
try:
|
|
321
|
+
user_language = (
|
|
322
|
+
get_model("user_preference").get_value(user, "pref-lang")
|
|
323
|
+
or DEFAULT_USER_LANGUAGE
|
|
324
|
+
)
|
|
325
|
+
# If there is no user_preferences model defined this will get thrown
|
|
326
|
+
except AttributeError:
|
|
327
|
+
user_language = DEFAULT_USER_LANGUAGE
|
|
328
|
+
|
|
329
|
+
formatted_language = user_language.lower().replace("-", "_")
|
|
330
|
+
if formatted_language not in [
|
|
331
|
+
loc.lower().replace("-", "_") for loc in settings.SUPERSET_DASHBOARD_LOCALES
|
|
332
|
+
]:
|
|
333
|
+
formatted_language = DEFAULT_USER_LANGUAGE
|
|
334
|
+
|
|
335
|
+
return formatted_language
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
def build_filter(filter_id, column, operator, value):
|
|
339
|
+
"""
|
|
340
|
+
|
|
341
|
+
Build a Superset native filter option.
|
|
342
|
+
|
|
343
|
+
"""
|
|
344
|
+
return {
|
|
345
|
+
"id": filter_id,
|
|
346
|
+
"extraFormData": {
|
|
347
|
+
"filters": [
|
|
348
|
+
{
|
|
349
|
+
"col": column,
|
|
350
|
+
"op": operator,
|
|
351
|
+
"val": value,
|
|
352
|
+
},
|
|
353
|
+
],
|
|
354
|
+
},
|
|
355
|
+
"filterState": {
|
|
356
|
+
"value": value,
|
|
357
|
+
},
|
|
358
|
+
"ownState": {},
|
|
359
|
+
}
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Endpoints for the Aspects platform plugin.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from collections import namedtuple
|
|
6
|
+
|
|
7
|
+
import prison
|
|
8
|
+
from django.conf import settings
|
|
9
|
+
from django.core.exceptions import ImproperlyConfigured
|
|
10
|
+
from opaque_keys import InvalidKeyError
|
|
11
|
+
from opaque_keys.edx.keys import CourseKey, UsageKey
|
|
12
|
+
from rest_framework import permissions
|
|
13
|
+
from rest_framework.authentication import SessionAuthentication
|
|
14
|
+
from rest_framework.exceptions import APIException, NotFound
|
|
15
|
+
from rest_framework.generics import GenericAPIView
|
|
16
|
+
from rest_framework.response import Response
|
|
17
|
+
|
|
18
|
+
from .utils import (
|
|
19
|
+
DEFAULT_FILTERS_FORMAT,
|
|
20
|
+
_,
|
|
21
|
+
build_filter,
|
|
22
|
+
generate_guest_token,
|
|
23
|
+
generate_superset_context,
|
|
24
|
+
get_localized_uuid,
|
|
25
|
+
get_model,
|
|
26
|
+
get_user_dashboard_locale,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
try:
|
|
30
|
+
from openedx.core.lib.api.permissions import (
|
|
31
|
+
IsCourseStaffInstructor,
|
|
32
|
+
)
|
|
33
|
+
except ImportError:
|
|
34
|
+
|
|
35
|
+
class IsCourseStaffInstructor(permissions.BasePermission):
|
|
36
|
+
"""
|
|
37
|
+
Permission class to use during tests.
|
|
38
|
+
|
|
39
|
+
Importing from edx-platform doesn't work when running tests,
|
|
40
|
+
so we declare our own permission class here.
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
def has_object_permission(self, request, view, obj):
|
|
44
|
+
"""
|
|
45
|
+
Return False for security; mock this out during tests.
|
|
46
|
+
"""
|
|
47
|
+
return False
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
# Course fields:
|
|
51
|
+
# * course_id: CourseKey; required by IsCourseStaffInstructor
|
|
52
|
+
# * display_name: str; optionally fetched from CourseOverview
|
|
53
|
+
Course = namedtuple("Course", ["course_id", "display_name"])
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _get_course(course_key):
|
|
57
|
+
"""
|
|
58
|
+
Return a Course-like object for the requested course id.
|
|
59
|
+
|
|
60
|
+
Raise NotFound if the corresponding course does not exist.
|
|
61
|
+
"""
|
|
62
|
+
# Fetch the CourseOverview (if we're running in edx-platform)
|
|
63
|
+
display_name = ""
|
|
64
|
+
CourseOverview = get_model("course_overviews")
|
|
65
|
+
if CourseOverview:
|
|
66
|
+
try:
|
|
67
|
+
course_overview = CourseOverview.objects.only("display_name").get(
|
|
68
|
+
id=course_key
|
|
69
|
+
)
|
|
70
|
+
display_name = course_overview.display_name
|
|
71
|
+
except CourseOverview.DoesNotExist as exc:
|
|
72
|
+
raise NotFound(
|
|
73
|
+
_("Course not found: '{course_id}'").format(course_id=course_key)
|
|
74
|
+
) from exc
|
|
75
|
+
return Course(course_id=course_key, display_name=display_name)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class SupersetTokenView(GenericAPIView):
|
|
79
|
+
"""
|
|
80
|
+
Superset guest token endpoint.
|
|
81
|
+
"""
|
|
82
|
+
|
|
83
|
+
authentication_classes = (SessionAuthentication,)
|
|
84
|
+
permission_classes = (
|
|
85
|
+
permissions.IsAuthenticated,
|
|
86
|
+
permissions.IsAdminUser | IsCourseStaffInstructor,
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
lookup_field = "course_id"
|
|
90
|
+
|
|
91
|
+
def get_object(self):
|
|
92
|
+
"""
|
|
93
|
+
Return a Course-like object for the requested course_id.
|
|
94
|
+
"""
|
|
95
|
+
course_id = self.kwargs.get(self.lookup_field, "")
|
|
96
|
+
try:
|
|
97
|
+
course_key = CourseKey.from_string(course_id)
|
|
98
|
+
except InvalidKeyError as exc:
|
|
99
|
+
raise NotFound(
|
|
100
|
+
_("Invalid course id: '{course_id}'").format(course_id=course_id)
|
|
101
|
+
) from exc
|
|
102
|
+
|
|
103
|
+
course = _get_course(course_key)
|
|
104
|
+
|
|
105
|
+
# May raise a permission denied
|
|
106
|
+
self.check_object_permissions(self.request, course)
|
|
107
|
+
|
|
108
|
+
return course_key
|
|
109
|
+
|
|
110
|
+
def get(self, request, *args, **kwargs):
|
|
111
|
+
"""
|
|
112
|
+
Return a guest token for accessing the Superset API.
|
|
113
|
+
"""
|
|
114
|
+
course = self.get_object()
|
|
115
|
+
|
|
116
|
+
dashboards = settings.ASPECTS_INSTRUCTOR_DASHBOARDS.copy()
|
|
117
|
+
|
|
118
|
+
# Only include these dashboards if in-context metrics are on,
|
|
119
|
+
# otherwise this call will always fail in older releases without
|
|
120
|
+
# admin intervention. This can be removed when we stop supporting
|
|
121
|
+
# < Sumac.
|
|
122
|
+
if settings.ASPECTS_ENABLE_STUDIO_IN_CONTEXT_METRICS:
|
|
123
|
+
dashboards.extend(settings.ASPECTS_IN_CONTEXT_DASHBOARDS.values())
|
|
124
|
+
|
|
125
|
+
extra_filters_format = settings.SUPERSET_EXTRA_FILTERS_FORMAT
|
|
126
|
+
|
|
127
|
+
try:
|
|
128
|
+
guest_token = generate_guest_token(
|
|
129
|
+
user=request.user,
|
|
130
|
+
course=course,
|
|
131
|
+
dashboards=dashboards,
|
|
132
|
+
filters=DEFAULT_FILTERS_FORMAT + extra_filters_format,
|
|
133
|
+
)
|
|
134
|
+
except ImproperlyConfigured as exc:
|
|
135
|
+
raise APIException() from exc
|
|
136
|
+
|
|
137
|
+
return Response({"guestToken": guest_token})
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
class SupersetInContextDashboardView(GenericAPIView):
|
|
141
|
+
"""
|
|
142
|
+
Endpoint for in-context analytics embedded Superset dashboard parameters.
|
|
143
|
+
"""
|
|
144
|
+
|
|
145
|
+
authentication_classes = (SessionAuthentication,)
|
|
146
|
+
permission_classes = (
|
|
147
|
+
permissions.IsAuthenticated,
|
|
148
|
+
permissions.IsAdminUser | IsCourseStaffInstructor,
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
lookup_field = "usage_id"
|
|
152
|
+
|
|
153
|
+
def get_object(self):
|
|
154
|
+
"""
|
|
155
|
+
Return a usage key or course key for the requested usage_key.
|
|
156
|
+
"""
|
|
157
|
+
usage_id = self.kwargs.get(self.lookup_field, "")
|
|
158
|
+
try:
|
|
159
|
+
usage_key = UsageKey.from_string(usage_id)
|
|
160
|
+
except InvalidKeyError as exc:
|
|
161
|
+
try:
|
|
162
|
+
course_key = usage_key = CourseKey.from_string(usage_id)
|
|
163
|
+
except InvalidKeyError:
|
|
164
|
+
raise NotFound(
|
|
165
|
+
_("Invalid usage id: '{usage_id}'").format(usage_id=usage_id)
|
|
166
|
+
) from exc
|
|
167
|
+
else:
|
|
168
|
+
course_key = usage_key.course_key
|
|
169
|
+
|
|
170
|
+
course = _get_course(course_key)
|
|
171
|
+
self.check_object_permissions(self.request, course)
|
|
172
|
+
|
|
173
|
+
return usage_key
|
|
174
|
+
|
|
175
|
+
def get(self, request, *args, **kwargs):
|
|
176
|
+
"""
|
|
177
|
+
Return Superset context for embedding the dashboard for the requested block.
|
|
178
|
+
"""
|
|
179
|
+
usage_key = self.get_object()
|
|
180
|
+
if isinstance(usage_key, CourseKey):
|
|
181
|
+
block_type = "course"
|
|
182
|
+
course_key = usage_key
|
|
183
|
+
else:
|
|
184
|
+
block_type = usage_key.block_type
|
|
185
|
+
course_key = usage_key.course_key
|
|
186
|
+
|
|
187
|
+
course_key_col = settings.IN_CONTEXT_DASHBOARD_COURSE_KEY_COLUMN
|
|
188
|
+
block_id_col = settings.IN_CONTEXT_DASHBOARD_BLOCK_ID_COLUMN
|
|
189
|
+
|
|
190
|
+
dashboards = settings.ASPECTS_IN_CONTEXT_DASHBOARDS
|
|
191
|
+
dashboard = dashboards.get(block_type)
|
|
192
|
+
if dashboard is None:
|
|
193
|
+
raise NotFound(
|
|
194
|
+
_("No dashboard for block type: '{block_type}'").format(
|
|
195
|
+
block_type=block_type
|
|
196
|
+
)
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
block_filter = {}
|
|
200
|
+
if not isinstance(usage_key, CourseKey):
|
|
201
|
+
for filter_id in dashboard["block_filter_ids"]:
|
|
202
|
+
block_filter[filter_id] = build_filter(
|
|
203
|
+
filter_id, block_id_col, "IN", [usage_key.block_id]
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
course_runs = {}
|
|
207
|
+
# In the future we might provide other course runs so that the
|
|
208
|
+
# embedding application can apply their corresponding filters
|
|
209
|
+
# instead of needing the Superset filter UI.
|
|
210
|
+
for course_run in [course_key.run]:
|
|
211
|
+
course_filter = block_filter.copy()
|
|
212
|
+
for filter_id in dashboard["course_filter_ids"]:
|
|
213
|
+
course_filter[filter_id] = build_filter(
|
|
214
|
+
filter_id, course_key_col, "IN", [str(course_key)]
|
|
215
|
+
)
|
|
216
|
+
course_runs[course_run] = {
|
|
217
|
+
# Superset filter URL parameter is encoded as Rison.
|
|
218
|
+
"native_filters": prison.dumps(course_filter),
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if dashboard.get("allow_translations"):
|
|
222
|
+
language = get_user_dashboard_locale(request.user)
|
|
223
|
+
dashboard = dashboard.copy()
|
|
224
|
+
dashboard["uuid"] = get_localized_uuid(dashboard["uuid"], language)
|
|
225
|
+
|
|
226
|
+
superset_config = settings.SUPERSET_CONFIG
|
|
227
|
+
superset_url = superset_config.get("service_url")
|
|
228
|
+
|
|
229
|
+
return Response(
|
|
230
|
+
{
|
|
231
|
+
"dashboardId": dashboard["uuid"],
|
|
232
|
+
"supersetUrl": superset_url,
|
|
233
|
+
"courseRuns": course_runs,
|
|
234
|
+
"defaultCourseRun": course_key.run,
|
|
235
|
+
}
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
class SupersetInstructorDashboardView(GenericAPIView):
|
|
240
|
+
"""
|
|
241
|
+
Endpoint for instructor dashboard Superset configuration.
|
|
242
|
+
|
|
243
|
+
Returns the Superset context needed by the frontend app to embed
|
|
244
|
+
the instructor dashboards, including dashboard list, URLs, and
|
|
245
|
+
display settings.
|
|
246
|
+
"""
|
|
247
|
+
|
|
248
|
+
authentication_classes = (SessionAuthentication,)
|
|
249
|
+
permission_classes = (
|
|
250
|
+
permissions.IsAuthenticated,
|
|
251
|
+
permissions.IsAdminUser | IsCourseStaffInstructor,
|
|
252
|
+
)
|
|
253
|
+
|
|
254
|
+
lookup_field = "course_id"
|
|
255
|
+
|
|
256
|
+
def get_object(self):
|
|
257
|
+
"""
|
|
258
|
+
Return a CourseKey for the requested course_id.
|
|
259
|
+
"""
|
|
260
|
+
course_id = self.kwargs.get(self.lookup_field, "")
|
|
261
|
+
try:
|
|
262
|
+
course_key = CourseKey.from_string(course_id)
|
|
263
|
+
except InvalidKeyError as exc:
|
|
264
|
+
raise NotFound(
|
|
265
|
+
_("Invalid course id: '{course_id}'").format(course_id=course_id)
|
|
266
|
+
) from exc
|
|
267
|
+
|
|
268
|
+
course = _get_course(course_key)
|
|
269
|
+
|
|
270
|
+
# May raise a permission denied
|
|
271
|
+
self.check_object_permissions(self.request, course)
|
|
272
|
+
|
|
273
|
+
return course_key
|
|
274
|
+
|
|
275
|
+
def get(self, request, *args, **kwargs):
|
|
276
|
+
"""
|
|
277
|
+
Return Superset context for embedding the instructor dashboards.
|
|
278
|
+
"""
|
|
279
|
+
course_key = self.get_object()
|
|
280
|
+
|
|
281
|
+
language = get_user_dashboard_locale(request.user)
|
|
282
|
+
context = {"course_id": str(course_key)}
|
|
283
|
+
context = generate_superset_context(
|
|
284
|
+
context,
|
|
285
|
+
dashboards=settings.ASPECTS_INSTRUCTOR_DASHBOARDS,
|
|
286
|
+
language=language,
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
return Response(
|
|
290
|
+
{
|
|
291
|
+
"superset_dashboards": context["superset_dashboards"],
|
|
292
|
+
"superset_url": context["superset_url"],
|
|
293
|
+
"superset_guest_token_url": context["superset_guest_token_url"],
|
|
294
|
+
"show_dashboard_link": settings.SUPERSET_SHOW_INSTRUCTOR_DASHBOARD_LINK,
|
|
295
|
+
}
|
|
296
|
+
)
|