Skip to content

Commit

Permalink
feat: add filter before user setttings context is rendered (#46)
Browse files Browse the repository at this point in the history
AccountSettingsRenderStarted filter which passes the account settings context before is rendered.
  • Loading branch information
Henrrypg authored Mar 1, 2023
1 parent cfd5969 commit 05f31c8
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 2 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ Change Log
Unreleased
----------
[1.2.0] - 2023-03-01
--------------------

Added
~~~~~

* AccountSettingsRenderStarted filter added which can be used to modify the rendered output of the account settings page.

[1.1.0] - 2023-02-16
--------------------

Expand Down
2 changes: 1 addition & 1 deletion openedx_filters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
"""
from openedx_filters.filters import *

__version__ = "1.1.0"
__version__ = "1.2.0"
71 changes: 71 additions & 0 deletions openedx_filters/learning/filters.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,82 @@
"""
Package where filters related to the learning architectural subdomain are implemented.
"""

from openedx_filters.exceptions import OpenEdxFilterException
from openedx_filters.tooling import OpenEdxPublicFilter
from openedx_filters.utils import SensitiveDataManagementMixin


class AccountSettingsRenderStarted(OpenEdxPublicFilter):
"""
Custom class used to create Account settings filters.
"""

filter_type = "org.openedx.learning.student.settings.render.started.v1"

class RedirectToPage(OpenEdxFilterException):
"""
Custom class used to redirect before the account settings rendering process.
"""

def __init__(self, message, redirect_to=""):
"""
Override init that defines specific arguments used in the account settings render process.
Arguments:
message: error message for the exception.
redirect_to: URL to redirect to.
"""
super().__init__(message, redirect_to=redirect_to)

class RenderInvalidAccountSettings(OpenEdxFilterException):
"""
Custom class used to stop the account settings rendering process.
"""

def __init__(self, message, account_settings_template="", template_context=None):
"""
Override init that defines specific arguments used in the account settings render process.
Arguments:
message: error message for the exception.
account_settings_template: template path rendered instead.
template_context: context used to the new account settings template.
"""
super().__init__(
message,
account_settings_template=account_settings_template,
template_context=template_context,
)

class RenderCustomResponse(OpenEdxFilterException):
"""
Custom class used to stop the account settings rendering process and return a custom response.
"""

def __init__(self, message, response=None):
"""
Override init that defines specific arguments used in the account settings render process.
Arguments:
message: error message for the exception.
response: custom response which will be returned by the account settings view.
"""
super().__init__(message, response=response)

@classmethod
def run_filter(cls, context, template_name):
"""
Execute a filter with the signature specified.
Arguments:
context (dict): template context for the account settings page.
template_name (str): template path used to render the account settings page.
"""
data = super().run_pipeline(context=context, template_name=template_name)
return data.get("context"), data.get("template_name")


class StudentRegistrationRequested(OpenEdxPublicFilter, SensitiveDataManagementMixin):
"""
Custom class used to create registration filters and its custom methods.
Expand Down
35 changes: 35 additions & 0 deletions openedx_filters/learning/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.test import TestCase

from openedx_filters.learning.filters import (
AccountSettingsRenderStarted,
CertificateCreationRequested,
CertificateRenderStarted,
CohortAssignmentRequested,
Expand Down Expand Up @@ -309,6 +310,7 @@ class TestRenderingFilters(TestCase):
- DashboardRenderStarted
- VerticalBlockChildRenderStarted
- VerticalBlockRenderCompleted
- AccountSettingsRenderStarted
"""

def setUp(self):
Expand Down Expand Up @@ -472,6 +474,39 @@ def test_halt_vertical_block_render(self, render_exception, attributes):

self.assertDictContainsSubset(attributes, exception.__dict__)

def test_account_settings_render_started(self):
"""
Test AccountSettingsRenderStarted filter behavior under normal conditions.
Expected behavior:
- The filter should return context.
"""
context = {
'duplicate_provider': None,
'disable_courseware_js': True,
'show_dashboard_tabs': True
}

result, _ = AccountSettingsRenderStarted.run_filter(context=context, template_name=None)

self.assertEqual(result, context)

@data(
(AccountSettingsRenderStarted.RedirectToPage, {"redirect_to": "custom_account_settings.html"}),
(AccountSettingsRenderStarted.RenderInvalidAccountSettings, {}),
(AccountSettingsRenderStarted.RenderCustomResponse, {"response": Mock()})
)
@unpack
def test_halt_account_rendering_process(self, AccountSettingsException, attributes):
"""
Test for account settings exceptions attributes.
Expected behavior:
- The exception must have the attributes specified.
"""
exception = AccountSettingsException(message="You can't access this page", **attributes)

self.assertDictContainsSubset(attributes, exception.__dict__)


class TestCohortFilters(TestCase):
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 1.1.0
current_version = 1.2.0
commit = True
tag = True

Expand Down

0 comments on commit 05f31c8

Please sign in to comment.