From 3e92d8ba3eac5dc5607b87ca3496288cedac5629 Mon Sep 17 00:00:00 2001 From: Kaustav Banerjee Date: Thu, 15 Aug 2024 20:04:47 +0530 Subject: [PATCH 1/4] fix: do not set show_new_library_button as false for DRF if library MFE is enabled --- cms/djangoapps/contentstore/rest_api/v1/views/home.py | 2 +- cms/djangoapps/contentstore/utils.py | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/cms/djangoapps/contentstore/rest_api/v1/views/home.py b/cms/djangoapps/contentstore/rest_api/v1/views/home.py index d41ceb2647c5..14f1423ea367 100644 --- a/cms/djangoapps/contentstore/rest_api/v1/views/home.py +++ b/cms/djangoapps/contentstore/rest_api/v1/views/home.py @@ -75,7 +75,7 @@ def get(self, request: Request): ``` """ - home_context = get_home_context(request, True) + home_context = get_home_context(request, no_course=True, hide_library_button_for_mfe=False) home_context.update({ 'allow_to_create_new_org': settings.FEATURES.get('ENABLE_CREATOR_GROUP', True) and request.user.is_staff, 'studio_name': settings.STUDIO_NAME, diff --git a/cms/djangoapps/contentstore/utils.py b/cms/djangoapps/contentstore/utils.py index 631ceeb270b6..bf3f16dedcbc 100644 --- a/cms/djangoapps/contentstore/utils.py +++ b/cms/djangoapps/contentstore/utils.py @@ -1651,7 +1651,7 @@ def format_in_process_course_view(uca): return courses_iter, in_process_course_actions -def get_home_context(request, no_course=False): +def get_home_context(request, no_course=False, hide_library_button_for_mfe=True): """ Utils is used to get context of course home. It is used for both DRF and django views. @@ -1688,6 +1688,11 @@ def get_home_context(request, no_course=False): if not split_library_view_on_dashboard() and LIBRARIES_ENABLED and not no_course: libraries = get_library_context(request, True)['libraries'] + show_new_library_button = user_can_view_create_library_button(user) + + if hide_library_button_for_mfe: + show_new_library_button = show_new_library_button and not should_redirect_to_library_authoring_mfe() + home_context = { 'courses': active_courses, 'split_studio_home': split_library_view_on_dashboard(), @@ -1699,8 +1704,7 @@ def get_home_context(request, no_course=False): 'library_authoring_mfe_url': LIBRARY_AUTHORING_MICROFRONTEND_URL, 'taxonomy_list_mfe_url': get_taxonomy_list_url(), 'libraries': libraries, - 'show_new_library_button': user_can_view_create_library_button(user) - and not should_redirect_to_library_authoring_mfe(), + 'show_new_library_button': show_new_library_button, 'user': user, 'request_course_creator_url': reverse('request_course_creator'), 'course_creator_status': _get_course_creator_status(user), From 661ee89adfdf2077809c5b340913eaab7f15e670 Mon Sep 17 00:00:00 2001 From: Kaustav Banerjee Date: Thu, 15 Aug 2024 20:05:44 +0530 Subject: [PATCH 2/4] fix: hide New Library button for ineligible users in split studio view --- cms/djangoapps/contentstore/utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cms/djangoapps/contentstore/utils.py b/cms/djangoapps/contentstore/utils.py index bf3f16dedcbc..fc0e836e54d9 100644 --- a/cms/djangoapps/contentstore/utils.py +++ b/cms/djangoapps/contentstore/utils.py @@ -1534,6 +1534,7 @@ def get_library_context(request, request_is_json=False): ) from cms.djangoapps.contentstore.views.library import ( LIBRARIES_ENABLED, + user_can_view_create_library_button, ) libraries = _accessible_libraries_iter(request.user) if LIBRARIES_ENABLED else [] @@ -1547,7 +1548,7 @@ def get_library_context(request, request_is_json=False): 'in_process_course_actions': [], 'courses': [], 'libraries_enabled': LIBRARIES_ENABLED, - 'show_new_library_button': LIBRARIES_ENABLED and request.user.is_active, + 'show_new_library_button': user_can_view_create_library_button(request.user) 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), From ba1848c15b568816c3ba38715eb49921c5b88ef9 Mon Sep 17 00:00:00 2001 From: Kaustav Banerjee Date: Thu, 15 Aug 2024 20:08:11 +0530 Subject: [PATCH 3/4] refactor: extract common code related to view/create library --- cms/djangoapps/contentstore/views/library.py | 56 +++++++++----------- 1 file changed, 24 insertions(+), 32 deletions(-) diff --git a/cms/djangoapps/contentstore/views/library.py b/cms/djangoapps/contentstore/views/library.py index 870c192653d2..8c314caa6697 100644 --- a/cms/djangoapps/contentstore/views/library.py +++ b/cms/djangoapps/contentstore/views/library.py @@ -69,31 +69,7 @@ def should_redirect_to_library_authoring_mfe(): ) -def user_can_view_create_library_button(user): - """ - Helper method for displaying the visibilty of the create_library_button. - """ - if not LIBRARIES_ENABLED: - return False - elif user.is_staff: - return True - elif settings.FEATURES.get('ENABLE_CREATOR_GROUP', False): - is_course_creator = get_course_creator_status(user) == 'granted' - has_org_staff_role = OrgStaffRole().get_orgs_for_user(user).exists() - has_course_staff_role = UserBasedRole(user=user, role=CourseStaffRole.ROLE).courses_with_role().exists() - has_course_admin_role = UserBasedRole(user=user, role=CourseInstructorRole.ROLE).courses_with_role().exists() - return is_course_creator or has_org_staff_role or has_course_staff_role or has_course_admin_role - else: - # EDUCATOR-1924: DISABLE_LIBRARY_CREATION overrides DISABLE_COURSE_CREATION, if present. - disable_library_creation = settings.FEATURES.get('DISABLE_LIBRARY_CREATION', None) - disable_course_creation = settings.FEATURES.get('DISABLE_COURSE_CREATION', False) - if disable_library_creation is not None: - return not disable_library_creation - else: - return not disable_course_creation - - -def user_can_create_library(user, org): +def _user_can_create_library_for_org(user, org=None): """ Helper method for returning the library creation status for a particular user, taking into account the value LIBRARIES_ENABLED. @@ -109,29 +85,29 @@ def user_can_create_library(user, org): Course Staff: Can make libraries in the organization which has courses of which they are staff. Course Admin: Can make libraries in the organization which has courses of which they are Admin. """ - if org is None: - return False if not LIBRARIES_ENABLED: return False elif user.is_staff: return True - if settings.FEATURES.get('ENABLE_CREATOR_GROUP', False): + elif settings.FEATURES.get('ENABLE_CREATOR_GROUP', False): + org_filter_params = {} + if org: + org_filter_params['org'] = org is_course_creator = get_course_creator_status(user) == 'granted' - has_org_staff_role = org in OrgStaffRole().get_orgs_for_user(user) + has_org_staff_role = OrgStaffRole().get_orgs_for_user(user).filter(**org_filter_params).exists() has_course_staff_role = ( UserBasedRole(user=user, role=CourseStaffRole.ROLE) .courses_with_role() - .filter(org=org) + .filter(**org_filter_params) .exists() ) has_course_admin_role = ( UserBasedRole(user=user, role=CourseInstructorRole.ROLE) .courses_with_role() - .filter(org=org) + .filter(**org_filter_params) .exists() ) return is_course_creator or has_org_staff_role or has_course_staff_role or has_course_admin_role - else: # EDUCATOR-1924: DISABLE_LIBRARY_CREATION overrides DISABLE_COURSE_CREATION, if present. disable_library_creation = settings.FEATURES.get('DISABLE_LIBRARY_CREATION', None) @@ -142,6 +118,22 @@ def user_can_create_library(user, org): return not disable_course_creation +def user_can_view_create_library_button(user): + """ + Helper method for displaying the visibilty of the create_library_button. + """ + return _user_can_create_library_for_org(user) + + +def user_can_create_library(user, org): + """ + Helper method for to check if user can create library for given org. + """ + if org is None: + return False + return _user_can_create_library_for_org(user, org) + + @login_required @ensure_csrf_cookie @require_http_methods(('GET', 'POST')) From c9e02b7ebd9cd0e3cccd8ec49b48ef736f31c979 Mon Sep 17 00:00:00 2001 From: Kaustav Banerjee Date: Thu, 29 Aug 2024 15:26:14 +0530 Subject: [PATCH 4/4] revert: fix: do not set show_new_library_button as false for DRF if library MFE is enabled --- cms/djangoapps/contentstore/rest_api/v1/views/home.py | 2 +- cms/djangoapps/contentstore/utils.py | 10 +++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/cms/djangoapps/contentstore/rest_api/v1/views/home.py b/cms/djangoapps/contentstore/rest_api/v1/views/home.py index 14f1423ea367..d41ceb2647c5 100644 --- a/cms/djangoapps/contentstore/rest_api/v1/views/home.py +++ b/cms/djangoapps/contentstore/rest_api/v1/views/home.py @@ -75,7 +75,7 @@ def get(self, request: Request): ``` """ - home_context = get_home_context(request, no_course=True, hide_library_button_for_mfe=False) + home_context = get_home_context(request, True) home_context.update({ 'allow_to_create_new_org': settings.FEATURES.get('ENABLE_CREATOR_GROUP', True) and request.user.is_staff, 'studio_name': settings.STUDIO_NAME, diff --git a/cms/djangoapps/contentstore/utils.py b/cms/djangoapps/contentstore/utils.py index fc0e836e54d9..d98ae7b93db3 100644 --- a/cms/djangoapps/contentstore/utils.py +++ b/cms/djangoapps/contentstore/utils.py @@ -1652,7 +1652,7 @@ def format_in_process_course_view(uca): return courses_iter, in_process_course_actions -def get_home_context(request, no_course=False, hide_library_button_for_mfe=True): +def get_home_context(request, no_course=False): """ Utils is used to get context of course home. It is used for both DRF and django views. @@ -1689,11 +1689,6 @@ def get_home_context(request, no_course=False, hide_library_button_for_mfe=True) if not split_library_view_on_dashboard() and LIBRARIES_ENABLED and not no_course: libraries = get_library_context(request, True)['libraries'] - show_new_library_button = user_can_view_create_library_button(user) - - if hide_library_button_for_mfe: - show_new_library_button = show_new_library_button and not should_redirect_to_library_authoring_mfe() - home_context = { 'courses': active_courses, 'split_studio_home': split_library_view_on_dashboard(), @@ -1705,7 +1700,8 @@ def get_home_context(request, no_course=False, hide_library_button_for_mfe=True) 'library_authoring_mfe_url': LIBRARY_AUTHORING_MICROFRONTEND_URL, 'taxonomy_list_mfe_url': get_taxonomy_list_url(), 'libraries': libraries, - 'show_new_library_button': show_new_library_button, + 'show_new_library_button': user_can_view_create_library_button(user) + and not should_redirect_to_library_authoring_mfe(), 'user': user, 'request_course_creator_url': reverse('request_course_creator'), 'course_creator_status': _get_course_creator_status(user),