-
Notifications
You must be signed in to change notification settings - Fork 4.2k
[BD-13] [BB-5363] refactor: deprecates replace url related properties from ModuleSystem #29880
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
Merged
doctoryes
merged 1 commit into
openedx:master
from
open-craft:kaustav/bd_13_replace_urls
Mar 14, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| """ | ||
| Supports replacement of static/course/jump-to-id URLs to absolute URLs in XBlocks. | ||
| """ | ||
|
|
||
| from xblock.reference.plugins import Service | ||
|
|
||
| from common.djangoapps.static_replace import ( | ||
| replace_course_urls, | ||
| replace_jump_to_id_urls, | ||
| replace_static_urls | ||
| ) | ||
|
|
||
|
|
||
| class ReplaceURLService(Service): | ||
| """ | ||
| A service for replacing static/course/jump-to-id URLs with absolute URLs in XBlocks. | ||
|
|
||
| Args: | ||
| course_id: Course identifier to be used in the absolute URL | ||
| data_directory: (optional) Directory in which course data is stored | ||
| static_asset_path: (optional) Path for static assets, which overrides data_directory and course_id, if nonempty | ||
| static_paths_out: (optional) Array to collect tuples for each static URI found: | ||
| * the original unmodified static URI | ||
| * the updated static URI (will match the original if unchanged) | ||
| jump_to_id_base_url: (optional) Absolute path to the base of the handler that will perform the redirect | ||
| lookup_url_func: Lookup function which returns the correct path of the asset | ||
| """ | ||
| def __init__( | ||
| self, | ||
| data_directory=None, | ||
| course_id=None, | ||
| static_asset_path='', | ||
| static_paths_out=None, | ||
| jump_to_id_base_url=None, | ||
| lookup_asset_url=None, | ||
| **kwargs | ||
| ): | ||
| super().__init__(**kwargs) | ||
| self.data_directory = data_directory | ||
| self.course_id = course_id | ||
| self.static_asset_path = static_asset_path | ||
| self.static_paths_out = static_paths_out | ||
| self.jump_to_id_base_url = jump_to_id_base_url | ||
| self.lookup_asset_url = lookup_asset_url | ||
|
|
||
| def replace_urls(self, text, static_replace_only=False): | ||
| """ | ||
| Replaces all static/course/jump-to-id URLs in provided text/html. | ||
|
|
||
| Args: | ||
| text: String containing the URL to be replaced | ||
| static_replace_only: If True, only static urls will be replaced | ||
| """ | ||
| if self.lookup_asset_url: | ||
| text = replace_static_urls(text, xblock=self.xblock(), lookup_asset_url=self.lookup_asset_url) | ||
Agrendalath marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| else: | ||
| text = replace_static_urls( | ||
| text, | ||
| data_directory=self.data_directory, | ||
| course_id=self.course_id, | ||
| static_asset_path=self.static_asset_path, | ||
| static_paths_out=self.static_paths_out | ||
| ) | ||
| if not static_replace_only: | ||
| text = replace_course_urls(text, self.course_id) | ||
| if self.jump_to_id_base_url: | ||
| text = replace_jump_to_id_urls(text, self.course_id, self.jump_to_id_base_url) | ||
|
|
||
| return text | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| """ | ||
| Wrapper function to replace static/course/jump-to-id URLs in XBlock to absolute URLs | ||
| """ | ||
|
|
||
| from openedx.core.lib.xblock_utils import wrap_fragment | ||
|
|
||
|
|
||
| def replace_urls_wrapper(block, view, frag, context, replace_url_service, static_replace_only=False): # pylint: disable=unused-argument | ||
| """ | ||
| Replace any static/course/jump-to-id URLs in XBlock to absolute URLs | ||
| """ | ||
| return wrap_fragment(frag, replace_url_service.replace_urls(frag.content, static_replace_only)) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that we're adding this service, should we also deprecate these methods?
cc: @bradenmacdonald, as I'm not sure if these aren't used by the blockstore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They're not called by blockstore but they are called by XBlocks, so yes.
@kaustavb12 Please make the same changes to https://github.com/openedx/edx-platform/blob/25b275bca4aee8099ad015648efa8551b272e11a/openedx/core/djangoapps/xblock/runtime/shims.py#L179-L219 - make them print a deprecation warning and call the new service instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bradenmacdonald @Agrendalath
These methods are part of the RuntimeShim class which is used with the XBlockRuntime class.
The XBlockRuntime class is a super class of the BlockstoreXBlockRuntime class
As I understand, blockstore Xblocks are not attached to courses, and so they don't have course id associated with them.
In that case would having replace_course_urls() and replace_jump_to_id_urls() methods in this shim necessary, since replacing both course urls and jump-to-id urls makes use of course ids.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kaustavb12
That's true, and
replace_course_urlsis not needed for that reason. (At least not yet; blockstore will support courses in the future!)But: blockstore XBlocks still use static files and they still need to have their
/static/...asset paths rewritten. The difference is that in Blockstore, static assets are stored with the XBlock not with the course.I actually created a new API for this,
self.runtime.transform_static_paths_to_urls:https://github.com/openedx/edx-platform/blob/4f58ed4f25d6e13250583504df08aa2ef6561fb7/openedx/core/djangoapps/xblock/runtime/runtime.py#L338-L354
And if you look at
shims.py, you can see that the old APIs are just wrappers around the new API.So, if you want this service to be the new API, you'll have to implement it for the blockstore runtime as well, and make sure that when it's called in the blockstore runtime it uses
self.runtime.transform_static_paths_to_urlsto do the rewriting, which will correctly rewrite the URLs to refer to assets from the XBlock's "bundle".The easiest way to see what I'm talking about would be to set up blockstore yourself, e.g. following these instructions and then create within blockstore an HTML block, and upload an image to the HTML block, then set the image src to
/static/image.pngand make sure it's working.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me know if you want more help!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bradenmacdonald, I'm copying the message from @kaustavb12 here.
As I don't know what this change means for blockstore, I could use a second pair of eyes to check the approach from c1c323ce08ac200955e921798039a014688bb264 when you have a moment. For now, we don't need to use the attributes of ReplaceURLService here, so we could make
ReplaceURLService.replace_block_urlsa static method to start using it as an API. If we are going to support courses with blockstore in the future, then we could expand the service to support them, and mergereplace_static_block_urlswithreplace_static_urlswithinstatic_replace/__init__.py. Would it make sense?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Agrendalath Sure, I left my comments on c1c323ce08ac200955e921798039a014688bb264
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bradenmacdonald
Thanks a lot pointing me to the instructions to setup blockstore in devstack and for reviewing the changes. It really helped me gain a lot of context about xblocks and the blockstore runtime.
I have now incorporated all your suggestions, and now there is only one API in ReplaceURLService which can be called by XBlocks as needed in the form of
self.runtime.service(self, 'replace_urls').replace_urls(text)Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bradenmacdonald
Also I have gone ahead and merged the new
replace_static_block_urlsfunction with thereplace_static_urlsfunction in static_replace/init.py similar to what was done for ReplaceURLService to avoid code duplicationcc. @Agrendalath
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @kaustavb12, those changes look great :)