From 5d1a778a1d97b99f551b2375e727fbb08d57e187 Mon Sep 17 00:00:00 2001 From: Kristin Aoki <42981026+KristinAoki@users.noreply.github.com> Date: Mon, 25 Sep 2023 13:08:19 -0400 Subject: [PATCH] feat: add new library util for split studio view (#33283) --- .../contentstore/asset_storage_handlers.py | 14 +--- cms/djangoapps/contentstore/utils.py | 66 ++++++++++++++++++- cms/djangoapps/contentstore/views/course.py | 24 +------ 3 files changed, 68 insertions(+), 36 deletions(-) diff --git a/cms/djangoapps/contentstore/asset_storage_handlers.py b/cms/djangoapps/contentstore/asset_storage_handlers.py index 29c8800fa0..8808dc4f0b 100644 --- a/cms/djangoapps/contentstore/asset_storage_handlers.py +++ b/cms/djangoapps/contentstore/asset_storage_handlers.py @@ -32,7 +32,7 @@ from xmodule.modulestore.django import modulestore # lint-amnesty, pylint: disa from xmodule.modulestore.exceptions import ItemNotFoundError # lint-amnesty, pylint: disable=wrong-import-order from .exceptions import AssetNotFoundException, AssetSizeTooLargeException -from .utils import reverse_course_url, get_files_uploads_url +from .utils import reverse_course_url, get_files_uploads_url, get_response_format, request_response_format_is_json from .toggles import use_new_files_uploads_page @@ -73,8 +73,8 @@ def handle_assets(request, course_key_string=None, asset_key_string=None): if not has_course_author_access(request.user, course_key): raise PermissionDenied() - response_format = _get_response_format(request) - if _request_response_format_is_json(request, response_format): + response_format = get_response_format(request) + if request_response_format_is_json(request, response_format): if request.method == 'GET': return _assets_json(request, course_key) @@ -133,14 +133,6 @@ def get_asset_usage_path(request, course_key, asset_key_string): return JsonResponse({'usage_locations': usage_locations}) -def _get_response_format(request): - return request.GET.get('format') or request.POST.get('format') or 'html' - - -def _request_response_format_is_json(request, response_format): - return response_format == 'json' or 'application/json' in request.META.get('HTTP_ACCEPT', 'application/json') - - def _asset_index(request, course_key): ''' Display an editable asset library. diff --git a/cms/djangoapps/contentstore/utils.py b/cms/djangoapps/contentstore/utils.py index 97b70e828c..1a4b709622 100644 --- a/cms/djangoapps/contentstore/utils.py +++ b/cms/djangoapps/contentstore/utils.py @@ -1390,9 +1390,63 @@ def get_help_urls(): return help_tokens +def get_response_format(request): + return request.GET.get('format') or request.POST.get('format') or 'html' + + +def request_response_format_is_json(request, response_format): + return response_format == 'json' or 'application/json' in request.META.get('HTTP_ACCEPT', 'application/json') + + +def get_library_context(request, request_is_json=False): + """ + Utils is used to get context of course home library tab. + It is used for both DRF and django views. + """ + from cms.djangoapps.contentstore.views.course import ( + get_allowed_organizations, + get_allowed_organizations_for_libraries, + user_can_create_organizations, + _accessible_libraries_iter, + _get_course_creator_status, + _format_library_for_view, + ) + from cms.djangoapps.contentstore.views.library import ( + LIBRARIES_ENABLED, + ) + + libraries = _accessible_libraries_iter(request.user) if LIBRARIES_ENABLED else [] + data = { + 'libraries': [_format_library_for_view(lib, request) for lib in libraries], + } + + if not request_is_json: + return { + **data, + 'in_process_course_actions': [], + 'courses': [], + 'libraries_enabled': LIBRARIES_ENABLED, + 'show_new_library_button': LIBRARIES_ENABLED and request.user.is_active, + 'user': request.user, + 'request_course_creator_url': reverse('request_course_creator'), + 'course_creator_status': _get_course_creator_status(request.user), + 'allow_unicode_course_id': settings.FEATURES.get('ALLOW_UNICODE_COURSE_ID', False), + 'archived_courses': True, + 'allow_course_reruns': settings.FEATURES.get('ALLOW_COURSE_RERUNS', True), + 'rerun_creator_status': GlobalStaff().has_user(request.user), + 'split_studio_home': split_library_view_on_dashboard(), + 'active_tab': 'libraries', + 'allowed_organizations_for_libraries': get_allowed_organizations_for_libraries(request.user), + 'allowed_organizations': get_allowed_organizations(request.user), + 'can_create_organizations': user_can_create_organizations(request.user), + } + + return data + + def get_home_context(request): """ - Utils is used to get context of course grading. + Utils is used to get context of course home. It is used for both DRF and django views. """ @@ -1420,8 +1474,14 @@ def get_home_context(request): courses_iter, in_process_course_actions = get_courses_accessible_to_user(request, org) user = request.user libraries = [] + response_format = get_response_format(request) + if not split_library_view_on_dashboard() and LIBRARIES_ENABLED: - libraries = _accessible_libraries_iter(request.user) + accessible_libraries = _accessible_libraries_iter(user) + libraries = [_format_library_for_view(lib, request) for lib in accessible_libraries] + + if split_library_view_on_dashboard() and request_response_format_is_json(request, response_format): + libraries = get_library_context(request, True)['libraries'] def format_in_process_course_view(uca): """ @@ -1456,7 +1516,7 @@ def get_home_context(request): 'libraries_enabled': LIBRARIES_ENABLED, 'redirect_to_library_authoring_mfe': should_redirect_to_library_authoring_mfe(), 'library_authoring_mfe_url': LIBRARY_AUTHORING_MICROFRONTEND_URL, - 'libraries': [_format_library_for_view(lib, request) for lib in libraries], + 'libraries': libraries, 'show_new_library_button': user_can_create_library(user) and not should_redirect_to_library_authoring_mfe(), 'user': user, 'request_course_creator_url': reverse('request_course_creator'), diff --git a/cms/djangoapps/contentstore/views/course.py b/cms/djangoapps/contentstore/views/course.py index d74a99a8e3..4a70e5028c 100644 --- a/cms/djangoapps/contentstore/views/course.py +++ b/cms/djangoapps/contentstore/views/course.py @@ -89,7 +89,6 @@ from ..courseware_index import CoursewareSearchIndexer, SearchIndexingError from ..tasks import rerun_course as rerun_course_task from ..toggles import ( default_enable_flexible_peer_openassessments, - split_library_view_on_dashboard, use_new_course_outline_page, use_new_home_page, use_new_updates_page, @@ -102,6 +101,7 @@ from ..utils import ( get_course_settings, get_course_grading, get_home_context, + get_library_context, get_lms_link_for_item, get_proctored_exam_settings_url, get_course_outline_url, @@ -121,7 +121,6 @@ from ..utils import ( update_course_discussions_settings, ) from .component import ADVANCED_COMPONENT_TYPES -from .library import LIBRARIES_ENABLED log = logging.getLogger(__name__) User = get_user_model() @@ -551,26 +550,7 @@ def library_listing(request): """ List all Libraries available to the logged in user """ - libraries = _accessible_libraries_iter(request.user) if LIBRARIES_ENABLED else [] - data = { - 'in_process_course_actions': [], - 'courses': [], - 'libraries_enabled': LIBRARIES_ENABLED, - 'libraries': [_format_library_for_view(lib, request) for lib in libraries], - 'show_new_library_button': LIBRARIES_ENABLED and request.user.is_active, - 'user': request.user, - 'request_course_creator_url': reverse('request_course_creator'), - 'course_creator_status': _get_course_creator_status(request.user), - 'allow_unicode_course_id': settings.FEATURES.get('ALLOW_UNICODE_COURSE_ID', False), - 'archived_courses': True, - 'allow_course_reruns': settings.FEATURES.get('ALLOW_COURSE_RERUNS', True), - 'rerun_creator_status': GlobalStaff().has_user(request.user), - 'split_studio_home': split_library_view_on_dashboard(), - 'active_tab': 'libraries', - 'allowed_organizations': get_allowed_organizations(request.user), - 'allowed_organizations_for_libraries': get_allowed_organizations_for_libraries(request.user), - 'can_create_organizations': user_can_create_organizations(request.user), - } + data = get_library_context(request) return render_to_response('index.html', data)