Skip to content

Commit

Permalink
feat: adds VerticalBlockRenderCompleted filter (#52)
Browse files Browse the repository at this point in the history
* feat: adds VerticalBlockChildrenLoaded filter

This adds VerticalBlockChildrenLoaded filter that will be passed the
list of the child blocks in a vertical block with the context and the
view in which it would be rendered.

This would allow for adding/removing blocks in a Unit conditionally just
before they are rendered into the view.
  • Loading branch information
tecoholic authored Feb 17, 2023
1 parent 22ffa9b commit 03f4aa7
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 2 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ Change Log
Unreleased
----------
[1.1.0] - 2023-02-16
--------------------

Added
~~~~~

* VerticalBlockRenderCompleted filter added which can be used to modify the rendered output of a VerticalBlock.

Changed
~~~~~~~

* Introduced PreventChildBlockRender exception to the VerticalBlockChildRenderStarted filter.

[1.0.0] - 2023-01-18
--------------------
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.0.0"
__version__ = "1.1.0"
32 changes: 32 additions & 0 deletions openedx_filters/learning/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,11 @@ class VerticalBlockChildRenderStarted(OpenEdxPublicFilter):

filter_type = "org.openedx.learning.vertical_block_child.render.started.v1"

class PreventChildBlockRender(OpenEdxFilterException):
"""
Custom class used to stop a particular child block from being rendered.
"""

@classmethod
def run_filter(cls, block, context):
"""
Expand Down Expand Up @@ -466,3 +471,30 @@ def run_filter(cls, enrollments):
"""
data = super().run_pipeline(enrollments=enrollments)
return data.get("enrollments")


class VerticalBlockRenderCompleted(OpenEdxPublicFilter):
"""
Custom class used to create filters to act on vertical block rendering completed.
"""

filter_type = "org.openedx.learning.vertical_block.render.completed.v1"

class PreventVerticalBlockRender(OpenEdxFilterException):
"""
Custom class used to prevent the vertical block from rendering for the user.
"""

@classmethod
def run_filter(cls, block, fragment, context, view):
"""
Execute a filter with the specified signature.
Arguments:
block (VerticalBlock): The VeriticalBlock instance which is being rendered
fragment (web_fragments.Fragment): The web-fragment containing the rendered content of VerticalBlock
context (dict): rendering context values like is_mobile_app, show_title..etc.,
view (str): the rendering view. Can be either 'student_view', or 'public_view'
"""
data = super().run_pipeline(block=block, fragment=fragment, context=context, view=view)
return data.get("block"), data.get("fragment"), data.get("context"), data.get("view")
63 changes: 63 additions & 0 deletions openedx_filters/learning/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
StudentLoginRequested,
StudentRegistrationRequested,
VerticalBlockChildRenderStarted,
VerticalBlockRenderCompleted,
)


Expand Down Expand Up @@ -307,6 +308,7 @@ class TestRenderingFilters(TestCase):
- CourseAboutRenderStarted
- DashboardRenderStarted
- VerticalBlockChildRenderStarted
- VerticalBlockRenderCompleted
"""

def setUp(self):
Expand Down Expand Up @@ -409,6 +411,67 @@ def test_verticalblock_child_render_started(self):

self.assertTupleEqual((block, context,), result)

@data(
(
VerticalBlockChildRenderStarted.PreventChildBlockRender,
{
"message": "Assessement question not available for Audit students"
}
)
)
@unpack
def test_halt_vertical_child_block_render(self, block_render_exception, attributes):
"""
Test for vertical child block render exception attributes.
Expected behavior:
- The exception must have the attributes specified.
"""
exception = block_render_exception(**attributes)

self.assertDictContainsSubset(attributes, exception.__dict__)

def test_vertical_block_render_completed(self):
"""
Test VerticalBlockRenderCompleted filter behavior under normal conditions.
Expected behavior:
- The filter must have the signature specified.
- The filter must return a webfragment, the context and view in order.
"""
block = Mock("VerticalBlock")
fragment = Mock("webfragment")
context = {
"is_mobile_view": False,
"username": "edx",
"bookmarked": False
}
view = "student_view"

result = VerticalBlockRenderCompleted.run_filter(block, fragment, context, view)

self.assertTupleEqual((block, fragment, context, view), result)

@data(
(
VerticalBlockRenderCompleted.PreventVerticalBlockRender,
{
"message": "Assignment units are not available for Audit students"
}
)
)
@unpack
def test_halt_vertical_block_render(self, render_exception, attributes):
"""
Test for vertical child block render exception attributes.
Expected behavior:
- The exception must have the attributes specified.
"""
exception = render_exception(**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.0.0
current_version = 1.1.0
commit = True
tag = True

Expand Down

0 comments on commit 03f4aa7

Please sign in to comment.