From 30e2adf4cdbf9cab8d8c6d549f7d7c9954108615 Mon Sep 17 00:00:00 2001 From: monteri Date: Mon, 20 May 2024 13:13:58 +0200 Subject: [PATCH] feat: last update --- lms/djangoapps/discussion/signals/handlers.py | 4 ++ .../mobile_api/offline_mode/signals.py | 14 +++++-- .../mobile_api/offline_mode/tasks.py | 8 ++-- .../offline_mode/utils/xblock_helpers.py | 41 +++++++++++-------- .../html_block/mobile_api_module.py | 2 - 5 files changed, 43 insertions(+), 26 deletions(-) diff --git a/lms/djangoapps/discussion/signals/handlers.py b/lms/djangoapps/discussion/signals/handlers.py index ead8128a0fe0..332517cc13b2 100644 --- a/lms/djangoapps/discussion/signals/handlers.py +++ b/lms/djangoapps/discussion/signals/handlers.py @@ -2,6 +2,7 @@ Signal handlers related to discussions. """ +import six import logging from django.conf import settings @@ -20,6 +21,7 @@ send_thread_created_notification, send_response_endorsed_notifications ) +from lms.djangoapps.mobile_api.offline_mode.tasks import generate_course_media from openedx.core.djangoapps.django_comment_common import signals from openedx.core.djangoapps.site_configuration.models import SiteConfiguration from openedx.core.djangoapps.theming.helpers import get_current_site @@ -46,6 +48,8 @@ def update_discussions_on_course_publish(sender, course_key, **kwargs): # pylin args=[context], countdown=settings.DISCUSSION_SETTINGS['COURSE_PUBLISH_TASK_DELAY'], ) + # import pdb; pdb.set_trace() + generate_course_media(six.text_type(course_key)) @receiver(signals.comment_created) diff --git a/lms/djangoapps/mobile_api/offline_mode/signals.py b/lms/djangoapps/mobile_api/offline_mode/signals.py index 573e5648132e..53cf4679c5b8 100644 --- a/lms/djangoapps/mobile_api/offline_mode/signals.py +++ b/lms/djangoapps/mobile_api/offline_mode/signals.py @@ -1,12 +1,20 @@ import six from django.dispatch import receiver +from openedx_events.content_authoring.signals import ( + XBLOCK_CREATED, + XBLOCK_DELETED, + XBLOCK_DUPLICATED, + XBLOCK_UPDATED, + XBLOCK_PUBLISHED, +) from xmodule.modulestore.django import SignalHandler from .tasks import generate_course_media -@receiver(SignalHandler.course_published) -def hello_world(sender, course_key, **kwargs): +@receiver([XBLOCK_PUBLISHED]) +def hello_world(**kwargs): import pdb; pdb.set_trace() - generate_course_media.delay(six.text_type(course_key)) + pass + # generate_course_media.delay(six.text_type(course_key)) diff --git a/lms/djangoapps/mobile_api/offline_mode/tasks.py b/lms/djangoapps/mobile_api/offline_mode/tasks.py index f4b08a5d2df4..c7abdb62522b 100644 --- a/lms/djangoapps/mobile_api/offline_mode/tasks.py +++ b/lms/djangoapps/mobile_api/offline_mode/tasks.py @@ -2,11 +2,13 @@ from opaque_keys.edx.keys import CourseKey from xmodule.modulestore.django import modulestore +from .utils.xblock_helpers import generate_offline_content, xblock_view_handler, generate_request_with_service_user @shared_task def generate_course_media(course_id): + request = generate_request_with_service_user() course_key = CourseKey.from_string(course_id) - - for xblock_html in modulestore().get_items(course_key, qualifiers={'category': ['html', 'problem']}): - xblock_html.update_info_api() + for xblock in modulestore().get_items(course_key, qualifiers={'category': 'problem'}): + html_data = xblock_view_handler(request, xblock) + generate_offline_content(xblock, html_data) diff --git a/lms/djangoapps/mobile_api/offline_mode/utils/xblock_helpers.py b/lms/djangoapps/mobile_api/offline_mode/utils/xblock_helpers.py index d850944e584c..53f0008cbec8 100644 --- a/lms/djangoapps/mobile_api/offline_mode/utils/xblock_helpers.py +++ b/lms/djangoapps/mobile_api/offline_mode/utils/xblock_helpers.py @@ -1,13 +1,16 @@ from django.conf import settings +from django.contrib.auth import get_user_model from django.core.files.base import ContentFile from django.core.files.storage import default_storage +from django.http import HttpRequest from xmodule.modulestore.django import modulestore -from .utils.html_manipulation import manipulate_html -from .utils.assets_management import save_asset_file, remove_old_files, base_storage_path -from .utils.zip_management import create_zip_file +from .html_manipulator import HtmlManipulator +from .assets_management import save_asset_file, remove_old_files, base_storage_path +from .zip_management import create_zip_file +User = get_user_model() def is_modified(xblock): file_path = f'{base_storage_path(xblock)}content_html.zip' @@ -20,6 +23,12 @@ def is_modified(xblock): return xblock.published_on > last_modified +def generate_request_with_service_user(): + user = User.objects.get(email='edx@example.com') + request = HttpRequest() + request.user = user + return request + def enclosing_sequence_for_gating_checks(block): seq_tags = ['sequential'] if block.location.block_type in seq_tags: @@ -49,8 +58,9 @@ def xblock_view_handler(request, xblock, check_if_enrolled=True, disable_staff_d ) from openedx.core.lib.mobile_utils import is_request_from_mobile_app from openedx.features.course_experience.utils import dates_banner_should_display + from lms.djangoapps.courseware.access import has_access from lms.djangoapps.courseware.masquerade import is_masquerading_as_specific_student, setup_masquerade - from lms.djangoapps.courseware.views.views import get_optimization_flags_for_content + # from lms.djangoapps.courseware.views.views import get_optimization_flags_for_content from lms.djangoapps.edxnotes.helpers import is_feature_enabled from lms.djangoapps.courseware.date_summary import verified_upgrade_deadline_link from common.djangoapps.edxmako.shortcuts import marketing_link, render_to_response, render_to_string @@ -79,11 +89,6 @@ def xblock_view_handler(request, xblock, check_if_enrolled=True, disable_staff_d staff_access, ) - UserActivity.record_user_activity( - request.user, usage_key.course_key, request=request, only_if_mobile_app=True - ) - - recheck_access = request.GET.get('recheck_access') == '1' try: block, _ = get_block_by_usage_id( request, @@ -91,7 +96,7 @@ def xblock_view_handler(request, xblock, check_if_enrolled=True, disable_staff_d str(usage_key), disable_staff_debug_info=disable_staff_debug_info, course=course, - will_recheck_access=recheck_access, + will_recheck_access=False, ) except: return None @@ -100,9 +105,9 @@ def xblock_view_handler(request, xblock, check_if_enrolled=True, disable_staff_d student_view_context['show_bookmark_button'] = request.GET.get('show_bookmark_button', '0') == '1' student_view_context['show_title'] = request.GET.get('show_title', '1') == '1' - is_learning_mfe = is_request_from_learning_mfe(request) - student_view_context['hide_access_error_blocks'] = is_learning_mfe and recheck_access - is_mobile_app = is_request_from_mobile_app(request) + # is_learning_mfe = is_request_from_learning_mfe(request) + student_view_context['hide_access_error_blocks'] = False + is_mobile_app = True student_view_context['is_mobile_app'] = is_mobile_app enable_completion_on_view_service = False @@ -116,7 +121,7 @@ def xblock_view_handler(request, xblock, check_if_enrolled=True, disable_staff_d missed_deadlines, missed_gated_content = dates_banner_should_display(course_key, request.user) fragment = block.render('student_view', context=student_view_context) - optimization_flags = get_optimization_flags_for_content(block, fragment) + # optimization_flags = get_optimization_flags_for_content(block, fragment) context = { 'fragment': fragment, @@ -141,7 +146,7 @@ def xblock_view_handler(request, xblock, check_if_enrolled=True, disable_staff_d 'is_mobile_app': is_mobile_app, 'render_course_wide_assets': True, - **optimization_flags, + # **optimization_flags, } return render_to_string('courseware/courseware-chromeless.html', context) @@ -152,8 +157,8 @@ def generate_offline_content(xblock, html_data): base_path = base_storage_path(xblock) remove_old_files(base_path) + html_manipulator = HtmlManipulator(xblock, html_data) + updated_html = html_manipulator.process_html() - manipulated_html = manipulate_html(html_data, lambda path, filename: save_asset_file(xblock, path, filename)) - - default_storage.save(f'{base_path}index.html', ContentFile(manipulated_html)) + default_storage.save(f'{base_path}index.html', ContentFile(updated_html)) create_zip_file(base_path, 'content_html.zip') diff --git a/openedx/features/_mobile_extensions/html_block/mobile_api_module.py b/openedx/features/_mobile_extensions/html_block/mobile_api_module.py index 4a73d7d5bca0..3adfd261ee00 100644 --- a/openedx/features/_mobile_extensions/html_block/mobile_api_module.py +++ b/openedx/features/_mobile_extensions/html_block/mobile_api_module.py @@ -114,9 +114,7 @@ def _add_js_bridge(self, soup): const originalAjax = $.ajax; $.ajax = function(options) { - sendMessageToiOS(options) sendMessageToiOS(JSON.stringify(options)) - sendMessageToAndroid(options) sendMessageToAndroid(JSON.stringify(options)) console.log(options, JSON.stringify(options))