From d3bc4601aeecb3de77919ae198a0f56a2aabddcf Mon Sep 17 00:00:00 2001 From: ha-D Date: Thu, 5 Aug 2021 12:26:51 +0000 Subject: [PATCH] feat: Add course-wide custom scripts Imlements OEP-15 by adding two fields to the course settings: - Course-wide Custom JS - Course-wide Custom CSS The resources defined in these fields will be rendered in all course pages. Rebase b6cb629849..0578e1c4c6 onto b6cb629849: - Add course-wide resources to API for MFE use - Revert "Add course-wide resources to API for MFE use" reverts commit 53648dcf0afe3cd171c9dc2eb5e56b871b2bcfb2 Signed-off-by: Gabor Boros --- common/lib/xmodule/xmodule/course_module.py | 13 ++++ lms/djangoapps/courseware/tests/test_views.py | 63 +++++++++++++++++++ lms/djangoapps/courseware/views/index.py | 3 +- lms/djangoapps/courseware/views/views.py | 1 + lms/templates/main.html | 11 ++++ lms/templates/main_django.html | 12 ++++ 6 files changed, 102 insertions(+), 1 deletion(-) diff --git a/common/lib/xmodule/xmodule/course_module.py b/common/lib/xmodule/xmodule/course_module.py index 7d0186b5da..8fb6899a6a 100644 --- a/common/lib/xmodule/xmodule/course_module.py +++ b/common/lib/xmodule/xmodule/course_module.py @@ -998,6 +998,19 @@ class CourseFields: # lint-amnesty, pylint: disable=missing-class-docstring ), scope=Scope.settings, default=False ) + + course_wide_js = List( + display_name=_("Course-wide Custom JS"), + help=_('Enter Javascript resource URLs you want to be loaded globally throughout the course pages.'), + scope=Scope.settings, + ) + + course_wide_css = List( + display_name=_("Course-wide Custom CSS"), + help=_('Enter CSS resource URLs you want to be loaded globally throughout the course pages.'), + scope=Scope.settings, + ) + other_course_settings = Dict( display_name=_("Other Course Settings"), help=_( diff --git a/lms/djangoapps/courseware/tests/test_views.py b/lms/djangoapps/courseware/tests/test_views.py index f3e9fc1017..2b510cc9d1 100644 --- a/lms/djangoapps/courseware/tests/test_views.py +++ b/lms/djangoapps/courseware/tests/test_views.py @@ -6,6 +6,7 @@ Tests courseware views.py import html import itertools import json +import re from datetime import datetime, timedelta from uuid import uuid4 @@ -65,6 +66,7 @@ from lms.djangoapps.courseware.toggles import ( from lms.djangoapps.courseware.user_state_client import DjangoXBlockUserStateClient from lms.djangoapps.grades.config.waffle import ASSUME_ZERO_GRADE_IF_ABSENT from lms.djangoapps.grades.config.waffle import waffle_switch as grades_waffle_switch +from lms.djangoapps.instructor.access import allow_access from lms.djangoapps.verify_student.models import VerificationDeadline from lms.djangoapps.verify_student.services import IDVerificationService from openedx.core.djangoapps.catalog.tests.factories import CourseFactory as CatalogCourseFactory @@ -3743,3 +3745,64 @@ class ContentOptimizationTestCase(ModuleStoreTestCase): response = self.client.get(url) assert response.status_code == 200 assert b"MathJax.Hub.Config" in response.content + + +@ddt.ddt +class TestCourseWideResources(ModuleStoreTestCase): + """ + Tests that custom course-wide resources are rendered in course pages + """ + + @ddt.data( + ('courseware', 'course_id', False, True), + ('dates', 'course_id', False, False), + ('progress', 'course_id', False, False), + ('instructor_dashboard', 'course_id', True, False), + ('forum_form_discussion', 'course_id', False, False), + ('render_xblock', 'usage_key_string', False, True), + ) + @ddt.unpack + def test_course_wide_resources(self, url_name, param, is_instructor, is_rendered): + """ + Tests that the % endif + % if course and course is not UNDEFINED and render_course_wide_assets: + % for css in course.course_wide_css: + + % endfor + % endif @@ -215,6 +220,12 @@ from common.djangoapps.pipeline_mako import render_require_js_path_overrides <%static:optional_include_mako file="body-extra.html" is_theming_enabled="True" />
+ + % if course and course is not UNDEFINED and render_course_wide_assets: + % for js in course.course_wide_js: + + % endfor + % endif diff --git a/lms/templates/main_django.html b/lms/templates/main_django.html index 5a858546fd..cc6ebaf24a 100644 --- a/lms/templates/main_django.html +++ b/lms/templates/main_django.html @@ -20,6 +20,12 @@ {% block headextra %}{% endblock %} {% render_block "css" %} + {% if request.course and request.render_course_wide_assets %} + {% for css in request.course.course_wide_css %} + + {% endfor %} + {% endif %} + {% optional_include "head-extra.html"|microsite_template_path %} @@ -46,6 +52,12 @@ {% javascript 'base_application' %} {% render_block "js" %} + + {% if request.course and request.render_course_wide_assets %} + {% for js in request.course.course_wide_js %} + + {% endfor %} + {% endif %}