Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: render library assets named xblock-... #35910

Merged
merged 5 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -449,9 +450,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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to fix assets with a space / %20 in the URL right? What would break if we change to always unquote? I'd prefer to be consistent in how we handle things, and if we can quickly backport this to Sumac, it's not too late for a change like that. Nobody is using it in production yet.

CC @ormsbee

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that we should prioritize simplicity/consistency over compatibility given that nobody's running this in production. My main question is where this translation happens. I would have expected this unquoting to happen higher up in the Django request processing–though I don't remember the specifics of how this is called, so maybe there's something preventing that? I just want to make sure that we're quoting in one place and unquoting in one place during request processing, and that we're not throwing around a bunch of paths that may or may not be quoted at various layers of the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm testing this now. Turns out it still fails to render the images in the LMS. What do you guys think about splitting the whitespace-fixing code into another PR so I can test that further without affecting the "xblock-" fix?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please open a separate PR for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alright. i reverted the change and made a separate draft PR here: #35974

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)
Loading