From c9827910feacb4921937b94e187443a0ad9aab51 Mon Sep 17 00:00:00 2001 From: Maria Grimaldi Date: Tue, 18 Jul 2023 11:44:45 -0400 Subject: [PATCH] feat: add filter design to modify the instructor dashboard render (#96) --- CHANGELOG.rst | 10 +++ openedx_filters/__init__.py | 2 +- openedx_filters/learning/filters.py | 73 +++++++++++++++++++ .../learning/tests/test_filters.py | 36 +++++++++ 4 files changed, 120 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 7bdd933..29d134a 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -13,6 +13,16 @@ Change Log Unreleased ---------- + +[1.4.0] - 2023-07-18 +-------------------- + +Added +~~~~~ + +* InstructorDashboardRenderStarted filter added which can be used to modify the instructor dashboard rendering process. + + [1.3.0] - 2023-05-25 -------------------- diff --git a/openedx_filters/__init__.py b/openedx_filters/__init__.py index 286d400..f6d72db 100644 --- a/openedx_filters/__init__.py +++ b/openedx_filters/__init__.py @@ -3,4 +3,4 @@ """ from openedx_filters.filters import * -__version__ = "1.3.0" +__version__ = "1.4.0" diff --git a/openedx_filters/learning/filters.py b/openedx_filters/learning/filters.py index 4751a88..2e7e876 100644 --- a/openedx_filters/learning/filters.py +++ b/openedx_filters/learning/filters.py @@ -589,3 +589,76 @@ def run_filter(cls, course_key, course_home_url): """ data = super().run_pipeline(course_key=course_key, course_home_url=course_home_url) return data.get("course_key"), data.get("course_home_url") + + +class InstructorDashboardRenderStarted(OpenEdxPublicFilter): + """ + Custom class used to create instructor dashboard filters and its custom methods. + """ + + filter_type = "org.openedx.learning.instructor.dashboard.render.started.v1" + + class RedirectToPage(OpenEdxFilterException): + """ + Custom class used to stop the instructor dashboard render by redirecting to a new page. + """ + + def __init__(self, message, redirect_to=""): + """ + Override init that defines specific arguments used in the instructor dashboard render process. + + Arguments: + message: error message for the exception. + redirect_to: URL to redirect to. + """ + super().__init__(message, redirect_to=redirect_to) + + class RenderInvalidDashboard(OpenEdxFilterException): + """ + Custom class used to render a custom template instead of the instructor dashboard. + """ + + def __init__(self, message, instructor_template="", template_context=None): + """ + Override init that defines specific arguments used in the instructor dashboard render process. + + Arguments: + message: error message for the exception. + instructor_template: template path rendered instead. + template_context: context used to the new instructor_template. + """ + super().__init__( + message, + instructor_template=instructor_template, + template_context=template_context, + ) + + class RenderCustomResponse(OpenEdxFilterException): + """ + Custom class used to stop the instructor dashboard rendering by returning a custom response. + """ + + def __init__(self, message, response=None): + """ + Override init that defines specific arguments used in the instructor dashboard render process. + + Arguments: + message: error message for the exception. + response: custom response which will be returned by the dashboard view. + """ + super().__init__( + message, + response=response, + ) + + @classmethod + def run_filter(cls, context, template_name): + """ + Execute a filter with the signature specified. + + Arguments: + context (dict): context dictionary for instructor's tab template. + template_name (str): template name to be rendered by the instructor's tab. + """ + data = super().run_pipeline(context=context, template_name=template_name) + return data.get("context"), data.get("template_name") diff --git a/openedx_filters/learning/tests/test_filters.py b/openedx_filters/learning/tests/test_filters.py index acdcd4d..cefaf76 100644 --- a/openedx_filters/learning/tests/test_filters.py +++ b/openedx_filters/learning/tests/test_filters.py @@ -18,6 +18,7 @@ CourseHomeUrlCreationStarted, CourseUnenrollmentStarted, DashboardRenderStarted, + InstructorDashboardRenderStarted, StudentLoginRequested, StudentRegistrationRequested, VerticalBlockChildRenderStarted, @@ -508,6 +509,41 @@ def test_halt_account_rendering_process(self, AccountSettingsException, attribut self.assertDictContainsSubset(attributes, exception.__dict__) + def test_instructor_dashboard_render_started(self): + """ + Test InstructorDashboardRenderStarted filter behavior under normal conditions. + + Expected behavior: + - The filter must have the signature specified. + - The filter should return context and template_name in that order. + """ + result = InstructorDashboardRenderStarted.run_filter(self.context, self.template_name) + + self.assertTupleEqual((self.context, self.template_name,), result) + + @data( + (InstructorDashboardRenderStarted.RedirectToPage, {"redirect_to": "custom-dashboard.html"}), + ( + InstructorDashboardRenderStarted.RenderInvalidDashboard, + { + "instructor_template": "custom-dasboard.html", + "template_context": {"course": Mock()}, + } + ), + (InstructorDashboardRenderStarted.RenderCustomResponse, {"response": Mock()}), + ) + @unpack + def test_halt_instructor_dashboard_render(self, dashboard_exception, attributes): + """ + Test for the instructor dashboard exceptions attributes. + + Expected behavior: + - The exception must have the attributes specified. + """ + exception = dashboard_exception(message="You can't access the dashboard", **attributes) + + self.assertDictContainsSubset(attributes, exception.__dict__) + class TestCohortFilters(TestCase): """