Skip to content

Commit

Permalink
feat!: use replace_urls service instead of runtime property
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This breaks Maple compatibility.
  • Loading branch information
Agrendalath committed Apr 21, 2023
1 parent a95a9b5 commit 62ab205
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
7 changes: 7 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Drag and Drop XBlock changelog
==============================

Version 3.2.0 (2023-04-20)
---------------------------

* Use the `replace_urls` service instead of a runtime property.

BREAKING CHANGE: This version is no longer compatible with Maple.

Version 3.1.3 (2023-04-15)
--------------------------

Expand Down
2 changes: 1 addition & 1 deletion drag_and_drop_v2/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Drag and Drop v2 XBlock """
from .drag_and_drop_v2 import DragAndDropBlock

__version__ = "3.1.3"
__version__ = "3.2.0"
14 changes: 3 additions & 11 deletions drag_and_drop_v2/drag_and_drop_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,16 +657,8 @@ def show_answer(self, data, suffix=''):
if explanation := (self.data.get('explanation') or '').strip():
if replace_urls_service := self.runtime.service(self, 'replace_urls'):
explanation = replace_urls_service.replace_urls(explanation)

# pylint: disable=fixme
# TODO: No longer needed after Maple.
else:
try:
explanation = self.runtime.replace_urls(explanation)
explanation = self.runtime.replace_course_urls(explanation)
explanation = self.runtime.replace_jump_to_id_urls(explanation)
except (TypeError, AttributeError):
logger.debug('Unable to perform URL substitution on the explanation: %s', explanation)
logger.debug('Unable to perform URL substitution on the explanation: %s', explanation)

answer['explanation'] = sanitize_html(explanation)
return answer
Expand Down Expand Up @@ -1056,8 +1048,8 @@ def _expand_static_url(self, url):
This method is unfortunately a bit hackish since XBlock does not provide a low-level API
for this.
"""
if hasattr(self.runtime, 'replace_urls'):
url = self.runtime.replace_urls(u'"{}"'.format(url))[1:-1]
if replace_urls_service := self.runtime.service(self, 'replace_urls'):
url = replace_urls_service.replace_urls(f'"{url}"')[1:-1]
elif hasattr(self.runtime, 'course_id'):
# edX Studio uses a different runtime for 'studio_view' than 'student_view',
# and the 'studio_view' runtime doesn't provide the replace_urls API.
Expand Down
11 changes: 6 additions & 5 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ def make_block():
field_data = KvsFieldData(key_store)
runtime = WorkbenchRuntime()
runtime.course_id = "dummy_course_id"
# noinspection PyProtectedMember
runtime._services['replace_urls'] = Mock( # pylint: disable=protected-access
replace_urls=lambda html, static_replace_only=False: re.sub(
r'"/static/([^"]*)"', r'"/course/test-course/assets/\1"', html
)
)
def_id = runtime.id_generator.create_definition(block_type)
usage_id = runtime.id_generator.create_usage(def_id)
scope_ids = ScopeIds('user', block_type, def_id, usage_id)
Expand Down Expand Up @@ -60,11 +66,6 @@ def patch_workbench(self):
'workbench.runtime.WorkbenchRuntime.local_resource_url',
lambda _, _block, path: '/expanded/url/to/drag_and_drop_v2/' + path
)
self.apply_patch(
'workbench.runtime.WorkbenchRuntime.replace_urls',
lambda _, html: re.sub(r'"/static/([^"]*)"', r'"/course/test-course/assets/\1"', html),
create=True,
)
self.apply_patch(
'drag_and_drop_v2.drag_and_drop_v2.get_grading_ignore_decoys_waffle_flag',
lambda: Mock(is_enabled=lambda _: False),
Expand Down

0 comments on commit 62ab205

Please sign in to comment.