Skip to content

Commit

Permalink
refactor: [AXM-349] refactor generated file pathes and generating tas…
Browse files Browse the repository at this point in the history
…k launching
  • Loading branch information
NiedielnitsevIvan committed Jun 6, 2024
1 parent 44cf855 commit e7a46ae
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 28 deletions.
21 changes: 13 additions & 8 deletions openedx/features/offline_mode/assets_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from xmodule.exceptions import NotFoundError
from xmodule.modulestore.exceptions import ItemNotFoundError

from .constants import MATHJAX_CDN_URL, MATHJAX_STATIC_PATH, OFFLINE_CONTENT_ARCHIVE_NAME
from .constants import MATHJAX_CDN_URL, MATHJAX_STATIC_PATH


log = logging.getLogger(__name__)
Expand Down Expand Up @@ -48,6 +48,9 @@ def save_asset_file(xblock, path, filename):
path (str): The path where the asset is located.
filename (str): The name of the file to be saved.
"""
if filename.endswith('djangojs.js'):
return

try:
if '/' in filename:
static_path = get_static_file_path(filename)
Expand Down Expand Up @@ -75,7 +78,7 @@ def remove_old_files(xblock):
base_path = block_storage_path(xblock)
assets_path = os.path.join(base_path, 'assets')
index_file_path = os.path.join(base_path, 'index.html')
offline_zip_path = os.path.join(base_path, OFFLINE_CONTENT_ARCHIVE_NAME)
offline_zip_path = os.path.join(base_path, f'{xblock.location.block_id}.zip')

# Delete the 'assets' directory if it exists
if os.path.isdir(assets_path):
Expand All @@ -96,18 +99,20 @@ def remove_old_files(xblock):
log.error(f"Error occurred while deleting the files or directory: {e}")


def is_offline_content_present(xblock):
def get_offline_block_content_path(xblock=None, usage_key=None):
"""
Checks whether 'offline_content.zip' file is present in the specified base path directory.
Args:
xblock (XBlock): The XBlock instance
usage_key (UsageKey): The UsageKey of the XBlock
Returns:
bool: True if the file is present, False otherwise
"""
base_path = block_storage_path(xblock)
offline_zip_path = os.path.join(base_path, OFFLINE_CONTENT_ARCHIVE_NAME)
return default_storage.exists(offline_zip_path)
usage_key = usage_key or getattr(xblock, 'location', None)
base_path = block_storage_path(usage_key=usage_key)
offline_zip_path = os.path.join(base_path, f'{usage_key.block_id}.zip')
return offline_zip_path if default_storage.exists(offline_zip_path) else None


def block_storage_path(xblock=None, usage_key=None):
Expand All @@ -123,7 +128,7 @@ def block_storage_path(xblock=None, usage_key=None):
str: The constructed base storage path.
"""
loc = usage_key or getattr(xblock, 'location', None)
return f'{loc.org}/{loc.course}/{loc.block_type}/{loc.block_id}/' if loc else ''
return f'{str(loc.course_key)}/{loc.block_id}/' if loc else ''


def is_modified(xblock):
Expand All @@ -133,7 +138,7 @@ def is_modified(xblock):
Args:
xblock (XBlock): The XBlock instance to check.
"""
file_path = os.path.join(block_storage_path(xblock), OFFLINE_CONTENT_ARCHIVE_NAME)
file_path = os.path.join(block_storage_path(xblock), f'{xblock.location.block_id}.zip')

try:
last_modified = default_storage.get_created_time(file_path)
Expand Down
4 changes: 1 addition & 3 deletions openedx/features/offline_mode/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@
MATHJAX_CDN_URL = f'https://cdn.jsdelivr.net/npm/mathjax@{MATHJAX_VERSION}/MathJax.js'
MATHJAX_STATIC_PATH = os.path.join('assets', 'js', f'MathJax-{MATHJAX_VERSION}.js')

OFFLINE_CONTENT_ARCHIVE_NAME = 'offline_content.zip'

DEFAULT_OFFLINE_SUPPORTED_XBLOCKS = ['html', 'problem']
DEFAULT_OFFLINE_SUPPORTED_XBLOCKS = ['problem']
OFFLINE_SUPPORTED_XBLOCKS = getattr(settings, 'OFFLINE_SUPPORTED_XBLOCKS', DEFAULT_OFFLINE_SUPPORTED_XBLOCKS)
23 changes: 17 additions & 6 deletions openedx/features/offline_mode/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"""
from celery import shared_task
from edx_django_utils.monitoring import set_code_owner_attribute
from opaque_keys.edx.keys import CourseKey
from opaque_keys.edx.keys import CourseKey, UsageKey

from xmodule.modulestore.django import modulestore

from .constants import OFFLINE_SUPPORTED_XBLOCKS
from .renderer import XBlockRenderer
from .utils import generate_offline_content, is_offline_supported_block
from .utils import generate_offline_content


@shared_task
Expand All @@ -19,7 +19,18 @@ def generate_offline_content_for_course(course_id):
Generates offline content for all supported XBlocks in the course.
"""
course_key = CourseKey.from_string(course_id)
for xblock in modulestore().get_items(course_key, qualifiers={'category': OFFLINE_SUPPORTED_XBLOCKS}):
if is_offline_supported_block(xblock):
html_data = XBlockRenderer(str(xblock.id)).render_xblock_from_lms()
generate_offline_content(xblock, html_data)
for offline_supported_block_type in OFFLINE_SUPPORTED_XBLOCKS:
for xblock in modulestore().get_items(course_key, qualifiers={'category': offline_supported_block_type}):
html_data = XBlockRenderer(str(xblock.location)).render_xblock_from_lms()
generate_offline_content_for_block.apply_async([str(xblock.location), html_data])


@shared_task
@set_code_owner_attribute
def generate_offline_content_for_block(block_id, html_data):
"""
Generates offline content for the specified block.
"""
block_usage_key = UsageKey.from_string(block_id)
xblock = modulestore().get_item(block_usage_key)
generate_offline_content(xblock, html_data)
19 changes: 8 additions & 11 deletions openedx/features/offline_mode/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from zipfile import ZipFile

from .assets_management import block_storage_path, remove_old_files, is_modified
from .constants import OFFLINE_CONTENT_ARCHIVE_NAME, OFFLINE_SUPPORTED_XBLOCKS
from .constants import OFFLINE_SUPPORTED_XBLOCKS
from .html_manipulator import HtmlManipulator

User = get_user_model()
Expand Down Expand Up @@ -39,11 +39,15 @@ def add_files_to_zip(zip_file, current_base_path, current_path_in_zip):
zip_file.write(full_path, os.path.join(current_path_in_zip, filename))

for directory in directories:
add_files_to_zip(zip_file, os.path.join(current_base_path, directory),
os.path.join(current_path_in_zip, directory))
add_files_to_zip(
zip_file,
os.path.join(current_base_path, directory),
os.path.join(current_path_in_zip, directory)
)

add_files_to_zip(zf, default_storage.path(base_path + 'assets/'), 'assets')
zf.close()
log.info(f'Offline content for {file_name} has been generated.')


def generate_offline_content(xblock, html_data):
Expand All @@ -66,11 +70,4 @@ def generate_offline_content(xblock, html_data):
os.path.join(base_path, 'index.html'),
ContentFile(updated_html),
)
create_zip_file(base_path, OFFLINE_CONTENT_ARCHIVE_NAME)


def is_offline_supported_block(xblock):
"""
Returns True if the block is supported for offline mode, False otherwise.
"""
return xblock.location.block_type in OFFLINE_SUPPORTED_XBLOCKS
create_zip_file(base_path, f'{xblock.location.block_id}.zip')

0 comments on commit e7a46ae

Please sign in to comment.