Skip to content

Commit

Permalink
fix: render library assets with whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielVZ96 committed Dec 5, 2024
1 parent 0bd0e6f commit 1c2ccdb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion common/djangoapps/static_replace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from xmodule.contentstore.content import StaticContent

log = logging.getLogger(__name__)
XBLOCK_STATIC_RESOURCE_PREFIX = '/static/xblock'
XBLOCK_STATIC_RESOURCE_PREFIX = '/static/xblock/'


def _url_replace_regex(prefix):
Expand Down
10 changes: 10 additions & 0 deletions common/djangoapps/static_replace/test/test_static_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ def processor(__, prefix, quote, rest): # pylint: disable=redefined-outer-name
assert process_static_urls(STATIC_SOURCE, processor) == '"test/static/file.png"'


def test_process_url_no_match_starts_with_xblock():
def processor(original, prefix, quote, rest): # pylint: disable=unused-argument, redefined-outer-name
return quote + 'test' + prefix + rest + quote
assert process_static_urls(
'"/static/xblock-file.png"',
processor,
data_dir=DATA_DIRECTORY
) == '"test/static/xblock-file.png"'


@patch('django.http.HttpRequest', autospec=True)
def test_static_urls(mock_request):
mock_request.build_absolute_uri = lambda url: 'http://' + url
Expand Down
20 changes: 16 additions & 4 deletions openedx/core/djangoapps/xblock/runtime/learning_core_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging
from collections import defaultdict
from datetime import datetime, timezone
from urllib.parse import unquote

from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.db.transaction import atomic
Expand Down Expand Up @@ -446,9 +447,20 @@ def _lookup_asset_url(self, block: XBlock, asset_path: str) -> str | None:
.get(key=f"static/{asset_path}")
)
except ObjectDoesNotExist:
# This means we see a path that _looks_ like it should be a static
# asset for this Component, but that static asset doesn't really
# exist.
return None
try:
# Retry with unquoted path. We don't always unquote because it would not
# be backwards-compatible, but we need to try both.
asset_path = unquote(asset_path)
content = (
component_version
.componentversioncontent_set
.filter(content__has_file=True)
.get(key=f"static/{asset_path}")
)
except ObjectDoesNotExist:
# This means we see a path that _looks_ like it should be a static
# asset for this Component, but that static asset doesn't really
# exist.
return None

return self._absolute_url_for_asset(component_version, asset_path)

0 comments on commit 1c2ccdb

Please sign in to comment.