Skip to content

Commit

Permalink
feat: update logic for courseware search enabled endpoint (#35922)
Browse files Browse the repository at this point in the history
  • Loading branch information
alangsto authored Nov 25, 2024
1 parent 8ea2d10 commit 139b416
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lms/djangoapps/courseware/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3777,7 +3777,7 @@ def test_courseware_mfe_search_staff_access(self):
@override_waffle_flag(COURSEWARE_MICROFRONTEND_SEARCH_ENABLED, active=False)
def test_is_mfe_search_waffle_disabled(self):
"""
Courseware search is only available when the waffle flag is enabled.
Courseware search is only available when the waffle flag is enabled, if no inclusion date is provided.
"""
user_admin = UserFactory(is_staff=True, is_superuser=True)
CourseEnrollmentFactory.create(user=user_admin, course_id=self.course.id, mode=CourseMode.VERIFIED)
Expand All @@ -3789,6 +3789,7 @@ def test_is_mfe_search_waffle_disabled(self):
self.assertEqual(body, {'enabled': False})

@patch.dict('django.conf.settings.FEATURES', {'COURSEWARE_SEARCH_INCLUSION_DATE': '2020'})
@override_waffle_flag(COURSEWARE_MICROFRONTEND_SEARCH_ENABLED, active=False)
@ddt.data(
(datetime(2013, 9, 18, 11, 30, 00), False),
(None, False),
Expand Down
19 changes: 13 additions & 6 deletions lms/djangoapps/courseware/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2316,28 +2316,35 @@ def courseware_mfe_search_enabled(request, course_id=None):
Simple GET endpoint to expose whether the user may use Courseware Search
for a given course.
"""
enabled = False
course_key = CourseKey.from_string(course_id) if course_id else None
user = request.user

has_required_enrollment = False
if settings.FEATURES.get('ENABLE_COURSEWARE_SEARCH_VERIFIED_ENROLLMENT_REQUIRED'):
enrollment_mode, _ = CourseEnrollment.enrollment_mode_for_user(user, course_key)
if (
auth.user_has_role(user, CourseStaffRole(CourseKey.from_string(course_id)))
or (enrollment_mode in CourseMode.VERIFIED_MODES)
):
enabled = True
has_required_enrollment = True
else:
enabled = True
has_required_enrollment = True

inclusion_date = settings.FEATURES.get('COURSEWARE_SEARCH_INCLUSION_DATE')
start_date = CourseOverview.get_from_id(course_key).start
has_valid_inclusion_date = False

# only include courses that have a start date later than the setting-defined inclusion date
# only include courses that have a start date later than the setting-defined inclusion date, if setting exists
if inclusion_date:
enabled = enabled and (start_date and start_date.strftime('%Y-%m-%d') > inclusion_date)
has_valid_inclusion_date = start_date and start_date.strftime('%Y-%m-%d') > inclusion_date

payload = {"enabled": courseware_mfe_search_is_enabled(course_key) if enabled else False}
# if the user has the appropriate enrollment, the feature is enabled if the course has a valid start date
# or if the feature is explicitly enabled via waffle flag.
enabled = (has_valid_inclusion_date or courseware_mfe_search_is_enabled(course_key)) \
if has_required_enrollment \
else False

payload = {"enabled": enabled}
return JsonResponse(payload)


Expand Down

0 comments on commit 139b416

Please sign in to comment.