Files
edx-platform/common/djangoapps/static_replace/services.py
Agrendalath 71fee4a4a0 feat!: remove block handling from runtime initialization of ReplaceURLService
BREAKING CHANGE: This removes the following deprecated shims from the runtime:
`replace_urls`, `replace_course_urls`, `replace_jump_to_id_urls`. XBlocks need
to use the `replace_urls` service instead.
2023-06-21 20:28:24 +02:00

70 lines
2.8 KiB
Python

"""
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:
block: (optional) An XBlock instance. Used when retrieving the service from the DescriptorSystem.
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,
block=None,
static_asset_path='',
static_paths_out=None,
jump_to_id_base_url=None,
lookup_asset_url=None,
**kwargs
):
super().__init__(**kwargs)
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
# This is needed because the `Service` class initialization expects the XBlock passed as an `xblock` keyword
# argument, but the `service` method from the `DescriptorSystem` passes a `block`.
self._xblock = self.xblock() or block
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
"""
block = self.xblock()
if self.lookup_asset_url:
text = replace_static_urls(text, xblock=block, lookup_asset_url=self.lookup_asset_url)
else:
text = replace_static_urls(
text,
data_directory=getattr(block, 'data_dir', None),
course_id=block.scope_ids.usage_id.context_key,
static_asset_path=self.static_asset_path or block.static_asset_path,
static_paths_out=self.static_paths_out
)
if not static_replace_only:
text = replace_course_urls(text, block.scope_ids.usage_id.context_key)
if self.jump_to_id_base_url:
text = replace_jump_to_id_urls(text, block.scope_ids.usage_id.context_key, self.jump_to_id_base_url)
return text