Skip to content

Commit

Permalink
fix: fixed namespace issue with urls
Browse files Browse the repository at this point in the history
so that reverse("platform_plugin_aspects:superset_guest_token") works
whether we're in standalone tests or a plugin to the platform.
  • Loading branch information
pomegranited committed Apr 11, 2024
1 parent d804eb0 commit 569f395
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 38 deletions.
2 changes: 1 addition & 1 deletion platform_plugin_aspects/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class PlatformPluginAspectsConfig(AppConfig):
plugin_app = {
PluginURLs.CONFIG: {
"lms.djangoapp": {
PluginURLs.NAMESPACE: name,
PluginURLs.NAMESPACE: "",
PluginURLs.REGEX: r"^aspects/",
PluginURLs.RELATIVE_PATH: "urls",
},
Expand Down
11 changes: 2 additions & 9 deletions platform_plugin_aspects/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from django.contrib.auth import get_user_model
from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
from django.test import TestCase
from django.urls import reverse
from opaque_keys.edx.keys import CourseKey
from rest_framework.test import APIClient

Expand All @@ -29,10 +28,7 @@ def setUp(self):
"""
super().setUp()
self.client = APIClient()
self.superset_guest_token_url = reverse(
"superset_guest_token",
kwargs={"course_id": COURSE_ID},
)
self.superset_guest_token_url = f"/superset_guest_token/{COURSE_ID}"
self.user = User.objects.create(
username="user",
email="[email protected]",
Expand All @@ -50,10 +46,7 @@ def test_guest_token_requires_course_access(self):
self.assertEqual(response.status_code, 403)

def test_guest_token_invalid_course_id(self):
superset_guest_token_url = reverse(
"superset_guest_token",
kwargs={"course_id": "block-v1:org+course+run"},
)
superset_guest_token_url = "/superset_guest_token/block-v1:org+course+run"
self.client.login(username="user", password="password")
response = self.client.post(superset_guest_token_url)
self.assertEqual(response.status_code, 404)
Expand Down
19 changes: 12 additions & 7 deletions platform_plugin_aspects/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@
Urls for the Aspects plugin.
"""

from django.urls import re_path
from django.urls import include, path, re_path

from . import views

# Copied from openedx.core.constants
COURSE_ID_PATTERN = r"(?P<course_id>[^/+]+(/|\+)[^/+]+(/|\+)[^/?]+)"

app_name = "platform_plugin_aspects"
app_url_patterns = (
[
re_path(
rf"superset_guest_token/{COURSE_ID_PATTERN}/?$",
views.SupersetView.as_view(),
name="superset_guest_token",
),
],
"platform_plugin_aspects",
)

urlpatterns = [
re_path(
rf"superset_guest_token/{COURSE_ID_PATTERN}/?$",
views.SupersetView.as_view(),
name="superset_guest_token",
),
path("", include(app_url_patterns)),
]
25 changes: 5 additions & 20 deletions platform_plugin_aspects/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.urls import NoReverseMatch, reverse
from django.urls import reverse
from supersetapiclient.client import SupersetClient
from xblock.reference.user_service import XBlockUser

Expand Down Expand Up @@ -56,25 +56,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": course},
)

context.update(
{
Expand Down
3 changes: 2 additions & 1 deletion platform_plugin_aspects/xblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from django.utils import translation
from web_fragments.fragment import Fragment
from webob import Response
from xblock.core import JsonHandlerError, XBlock
from xblock.core import XBlock
from xblock.exceptions import JsonHandlerError
from xblock.fields import List, Scope, String
from xblock.utils.resources import ResourceLoader
from xblock.utils.studio_editable import StudioEditableXBlockMixin
Expand Down

0 comments on commit 569f395

Please sign in to comment.