From c0e90810b56c3c9b4d761a48f5c31214ee5430e8 Mon Sep 17 00:00:00 2001 From: Hasnain Saeed Date: Thu, 16 Sep 2021 17:28:59 +0500 Subject: [PATCH] Refactor course page Add clear course page cache functionality --- .../contentstore/courseware_index.py | 24 +- .../management/commands/reindex_catalog.py | 3 +- cms/djangoapps/contentstore/views/course.py | 6 + cms/static/js/views/pages/course_outline.js | 40 ++++ cms/static/sass/_base.scss | 4 +- cms/static/sass/bootstrap/_layouts.scss | 4 +- cms/static/sass/elements/_layout.scss | 4 +- cms/templates/course_outline.html | 9 +- cms/urls.py | 1 + lms/djangoapps/courseware/utils.py | 11 - lms/djangoapps/courseware/views/views.py | 152 ------------- lms/urls.py | 9 +- openedx/features/ucsd_features/admin.py | 6 +- openedx/features/ucsd_features/constants.py | 1 - .../management/commands/add_ucsd_courses.py | 31 +-- .../migrations/0005_courserun.py | 21 ++ openedx/features/ucsd_features/models.py | 19 +- openedx/features/ucsd_features/signals.py | 12 +- openedx/features/ucsd_features/urls.py | 22 ++ openedx/features/ucsd_features/utils.py | 35 +++ openedx/features/ucsd_features/views.py | 213 ++++++++++++++++++ 21 files changed, 404 insertions(+), 223 deletions(-) delete mode 100644 openedx/features/ucsd_features/constants.py create mode 100644 openedx/features/ucsd_features/migrations/0005_courserun.py create mode 100644 openedx/features/ucsd_features/urls.py create mode 100644 openedx/features/ucsd_features/views.py diff --git a/cms/djangoapps/contentstore/courseware_index.py b/cms/djangoapps/contentstore/courseware_index.py index 4a11324044d4..19f091e9eeb7 100644 --- a/cms/djangoapps/contentstore/courseware_index.py +++ b/cms/djangoapps/contentstore/courseware_index.py @@ -20,7 +20,8 @@ from xmodule.course_module import CATALOG_VISIBILITY_ABOUT, CATALOG_VISIBILITY_NONE from xmodule.library_tools import normalize_key_for_search from xmodule.modulestore import ModuleStoreEnum -from openedx.features.ucsd_features.models import Course +from openedx.features.ucsd_features.models import CourseRun +from openedx.features.ucsd_features.utils import get_course_id # REINDEX_AGE is the default amount of time that we look back for changes # that might have happened. If we are provided with a time at which the @@ -585,22 +586,23 @@ def index_about_information(cls, modulestore, course): if not searcher: return - course_key = f'{course.org}+{course.number}' - course_key = course_key.upper() # making catalog course id case-insensitive course_run_key = text_type(course.id) # Fetch latest run in the course try: - ucsd_course = Course.objects.get(id=course_key) + ucsd_course_run = CourseRun.objects.get(id=course_run_key) + ucsd_course = ucsd_course_run.course + course_key = ucsd_course.id latest_run = ucsd_course.latest_course_run - except Course.DoesNotExist: - ucsd_course = None - latest_run = None - # Replace current run with the latest run in the given course - if latest_run: - course_run_key = text_type(latest_run.id) - course = modulestore.get_course(latest_run.id) + # Replace current run with the latest run if exists + if latest_run: + course_run_key = text_type(latest_run.id) + course = modulestore.get_course(latest_run.id) + + except: + course_key = get_course_id(course) + ucsd_course = None course_info = { 'id': course_key, diff --git a/cms/djangoapps/contentstore/management/commands/reindex_catalog.py b/cms/djangoapps/contentstore/management/commands/reindex_catalog.py index ee43d077cd18..cae287a20c8c 100644 --- a/cms/djangoapps/contentstore/management/commands/reindex_catalog.py +++ b/cms/djangoapps/contentstore/management/commands/reindex_catalog.py @@ -11,6 +11,7 @@ from cms.djangoapps.contentstore.courseware_index import CourseAboutSearchIndexer from xmodule.modulestore.django import modulestore +from openedx.features.ucsd_features.utils import get_course_id from .prompt import query_yes_no @@ -59,7 +60,7 @@ def handle(self, *args, **options): return # remove indices new and old catalog indices - course_keys = ['{}+{}'.format(course.org, course.number).upper() for course in courses] + course_keys = [get_course_id(course) for course in courses] course_keys = list(set(course_keys)) course_keys += [text_type(course.id) for course in courses] searcher.remove(doc_type, course_keys) diff --git a/cms/djangoapps/contentstore/views/course.py b/cms/djangoapps/contentstore/views/course.py index afb4b7c9558f..1875c42c9d31 100644 --- a/cms/djangoapps/contentstore/views/course.py +++ b/cms/djangoapps/contentstore/views/course.py @@ -636,9 +636,14 @@ def course_index(request, course_key): raise Http404 lms_link = get_lms_link_for_item(course_module.location) reindex_link = None + clear_course_page_cache_link = None if settings.FEATURES.get('ENABLE_COURSEWARE_INDEX', False): if GlobalStaff().has_user(request.user): reindex_link = "/course/{course_id}/search_reindex".format(course_id=six.text_type(course_key)) + if GlobalStaff().has_user(request.user): + clear_course_page_cache_link = "/course/{course_id}/clear_course_page_cache".format(course_id=six.text_type(course_key)) + + print(clear_course_page_cache_link) sections = course_module.get_children() course_structure = _course_outline_json(request, course_module) locator_to_show = request.GET.get('show', None) @@ -682,6 +687,7 @@ def course_index(request, course_key): 'course_release_date': course_release_date, 'settings_url': settings_url, 'reindex_link': reindex_link, + 'clear_course_page_cache_link': clear_course_page_cache_link, 'deprecated_blocks_info': deprecated_blocks_info, 'notification_dismiss_url': reverse_course_url( 'course_notifications_handler', diff --git a/cms/static/js/views/pages/course_outline.js b/cms/static/js/views/pages/course_outline.js index cd86d6399795..91828944e2f8 100644 --- a/cms/static/js/views/pages/course_outline.js +++ b/cms/static/js/views/pages/course_outline.js @@ -44,6 +44,9 @@ define([ this.$('.button.button-reindex').click(function(event) { self.handleReIndexEvent(event); }); + this.$('.button.button-clear-cache').click(function(event) { + self.handleClearCacheEvent(event); + }); this.model.on('change', this.setCollapseExpandVisibility, this); $('.dismiss-button').bind('click', ViewUtils.deleteNotificationHandler(function() { $('.wrapper-alert-announcement').removeClass('is-shown').addClass('is-hidden'); @@ -167,6 +170,17 @@ define([ .always(function() { $target.css('cursor', 'pointer'); }); }, + handleClearCacheEvent: function(event) { + var self = this; + event.preventDefault(); + var $target = $(event.currentTarget); + $target.css('cursor', 'wait'); + this.clearCache($target.attr('href')) + .done(function(data) { self.onClearCacheSuccess(data); }) + .fail(function(data) { self.onClearCacheError(data); }) + .always(function() { $target.css('cursor', 'pointer'); }); + }, + startReIndex: function(reindex_url) { return $.ajax({ url: reindex_url, @@ -177,6 +191,16 @@ define([ }); }, + clearCache: function(clear_cache_url) { + return $.ajax({ + url: clear_cache_url, + method: 'GET', + global: false, + contentType: 'application/json; charset=utf-8', + dataType: 'json' + }); + }, + onIndexSuccess: function(data) { var msg = new AlertView.Announcement({ title: gettext('Course Index'), @@ -191,7 +215,23 @@ define([ message: data.user_message }); msg.show(); + }, + onClearCacheSuccess: function(data) { + var msg = new AlertView.Announcement({ + title: gettext('Clear Course Page Cache'), + message: data.user_message + }); + msg.show(); + }, + + onClearCacheError: function(data) { + var msg = new NoteView.Error({ + title: gettext('Cache Error'), + message: data.user_message + }); + msg.show(); } + }); /** diff --git a/cms/static/sass/_base.scss b/cms/static/sass/_base.scss index 464f68690688..033cd1dc99c0 100644 --- a/cms/static/sass/_base.scss +++ b/cms/static/sass/_base.scss @@ -286,7 +286,7 @@ dl { @include float(left); @include margin-right(flex-gutter()); - width: flex-grid(6, 12); + width: flex-grid(5, 12); } .nav-actions { @@ -295,7 +295,7 @@ dl { position: relative; bottom: -($baseline*0.75); - width: flex-grid(6, 12); + width: flex-grid(7, 12); .nav-item { display: inline-block; diff --git a/cms/static/sass/bootstrap/_layouts.scss b/cms/static/sass/bootstrap/_layouts.scss index 7bee47228bc9..d0a4d8e7eb80 100644 --- a/cms/static/sass/bootstrap/_layouts.scss +++ b/cms/static/sass/bootstrap/_layouts.scss @@ -96,7 +96,7 @@ .page-header { float: left; - width: flex-grid(6, 12); + width: flex-grid(5, 12); margin-right: flex-gutter(); } @@ -104,7 +104,7 @@ position: relative; bottom: -($baseline*0.75); float: right; - width: flex-grid(6, 12); + width: flex-grid(7, 12); text-align: right; .nav-item { diff --git a/cms/static/sass/elements/_layout.scss b/cms/static/sass/elements/_layout.scss index 97ec2401e9a4..fd5434149e9d 100644 --- a/cms/static/sass/elements/_layout.scss +++ b/cms/static/sass/elements/_layout.scss @@ -37,7 +37,7 @@ .page-header { @include float(left); - width: flex-grid(6, 12); + width: flex-grid(5, 12); @include margin-right(flex-gutter()); } @@ -48,7 +48,7 @@ @include float(right); - width: flex-grid(6, 12); + width: flex-grid(7, 12); @include text-align(right); diff --git a/cms/templates/course_outline.html b/cms/templates/course_outline.html index 1be2997a434f..7e07d24b2ce1 100644 --- a/cms/templates/course_outline.html +++ b/cms/templates/course_outline.html @@ -115,7 +115,7 @@

${_("This course uses features th %endif - + %if proctoring_errors:
@@ -182,6 +182,13 @@

${_("Page Actions")}

%endif + %if clear_course_page_cache_link: + + %endif