Skip to content

Commit

Permalink
tmp: some fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian2012 committed Apr 10, 2024
1 parent 029d3bd commit b91892f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 33 deletions.
1 change: 1 addition & 0 deletions platform_plugin_aspects/extensions/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def run_filter(
formatted_language = "en_US"

context = generate_superset_context(
course.id,
context,
dashboards=dashboards,
language=formatted_language,
Expand Down
42 changes: 29 additions & 13 deletions platform_plugin_aspects/static/js/embed_dashboard.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
function getCookie(name) {
let cookieValue = null;
if (document.cookie && document.cookie !== "") {
const cookies = document.cookie.split(";");
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].trim();
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === name + "=") {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}

async function fetchGuestToken() {
// Fetch the guest token from your backend
const response = await fetch(guest_token_url, {
method: 'POST',
body: JSON.stringify({
"X-CSRFToken": getCookie("csrftoken"),
})
});
const data = await response.json();
return data.guestToken;
}

function embedDashboard(dashboard_uuid, superset_url, guest_token_url, xblock_id) {
xblock_id = xblock_id || "";

async fetchGuestToken() {
// Fetch the guest token from your backend
const response = await fetch(guest_token_url, {
method: 'POST',
body: JSON.stringify({
// TODO csrf_token: csrf_token,
})
});
const data = await response.json();
return data.guestToken;
}

window.supersetEmbeddedSdk
.embedDashboard({
id: dashboard_uuid, // given by the Superset embedding UI
Expand Down Expand Up @@ -41,6 +57,6 @@ function embedDashboard(dashboard_uuid, superset_url, guest_token_url, xblock_id

if (window.superset_dashboards !== undefined) {
window.superset_dashboards.forEach(function(dashboard) {
embedDashboard(dashboard.uuid, window.superset_url, window.superset_guest_token_url);
embedDashboard(dashboard.uuid, window.superset_url, window.superset_guest_token_url, dashboard.uuid);
});
}
25 changes: 5 additions & 20 deletions platform_plugin_aspects/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def _(text):


def generate_superset_context(
course,
context,
dashboards,
language=None,
Expand All @@ -43,7 +44,6 @@ def generate_superset_context(
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:
Expand All @@ -55,25 +55,10 @@ def generate_superset_context(

superset_url = _fix_service_url(superset_config.get("service_url"))

# FIXME -- namespace issue with plugin-registered urls?
try:
guest_token_url = reverse(
"platform_plugin_aspects:superset_guest_token",
kwargs={"course_id": course},
)
except NoReverseMatch:
logger.error(
"Error reversing platform_plugin_aspects:superset_guest_token, trying without namespace"
)
try:
guest_token_url = reverse(
"superset_guest_token",
kwargs={"course_id": course},
)
logger.info("Reversing superset_guest_token worked")
except NoReverseMatch:
logger.critical("Error reversing superset_guest_token, giving up")
guest_token_url = ""
guest_token_url = reverse(
"platform_plugin_aspects:superset_guest_token",
kwargs={"course_id": str(course)},
)

context.update(
{
Expand Down
1 change: 1 addition & 0 deletions platform_plugin_aspects/xblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def student_view(self, context=None):
return frag

context = generate_superset_context(
course=self.runtime.course_id,
context=context,
dashboards=self.dashboards(),
)
Expand Down

0 comments on commit b91892f

Please sign in to comment.