-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #370 from nelc/and/add_openedx_filters
feat: implement XAPI openedx filters
- Loading branch information
Showing
27 changed files
with
288 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,7 @@ | |
.wy-table-responsive { | ||
overflow: visible !important; | ||
} | ||
|
||
.bd-page-width { | ||
max-width: 96rem; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
Various backends for receiving edX LMS events.. | ||
""" | ||
|
||
__version__ = '7.0.2' | ||
__version__ = '7.1.0' |
Empty file.
49 changes: 49 additions & 0 deletions
49
event_routing_backends/processors/openedx_filters/decorators.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
""" | ||
Decorators that helps to implement the Processor filter functionality. | ||
""" | ||
import functools | ||
|
||
from event_routing_backends.processors.openedx_filters.filters import ProcessorBaseFilter | ||
|
||
|
||
def openedx_filter(filter_type): | ||
""" | ||
This decorator allows to implement the ProcessorBaseFilter on multiple class methods | ||
and intends to modify the returned value from methods like get_actor or get_objects | ||
in cases where the standard output doesn't satisfy the implementation requirements. | ||
Arguments: | ||
filter_type: String that defines the filter_type attribute of ProcessorBaseFilter, | ||
this allows to identify the configuration setting. | ||
Example: | ||
1. Decorate your method: | ||
@openedx_filter(filter_type="this.will.be.the.filter.key") | ||
def get_object(self): | ||
... | ||
2. Set the openedx filter config in your environment variables. | ||
OPEN_EDX_FILTERS_CONFIG = { | ||
"this.will.be.the.filter.key": { | ||
"pipeline": ["path.to.an.external.pipeline.step"], | ||
"fail_silently": False, | ||
} | ||
} | ||
3. More details about filters https://github.com/openedx/openedx-filters/ | ||
""" | ||
def wrapper(func): | ||
@functools.wraps(func) | ||
def inner_wrapper(*args, **kwargs): | ||
dynamic_filter = ProcessorBaseFilter.generate_dynamic_filter(filter_type=filter_type) | ||
|
||
return dynamic_filter.run_filter( | ||
result=func(*args, **kwargs), | ||
) | ||
|
||
return inner_wrapper | ||
|
||
return wrapper |
10 changes: 10 additions & 0 deletions
10
event_routing_backends/processors/openedx_filters/exceptions.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
""" | ||
Custom processors exceptions thrown by filters. | ||
""" | ||
from openedx_filters.exceptions import OpenEdxFilterException | ||
|
||
|
||
class InvalidFilterType(OpenEdxFilterException): | ||
""" | ||
Exception that indicates that the attribute `filter_type` has not been set property. | ||
""" |
50 changes: 50 additions & 0 deletions
50
event_routing_backends/processors/openedx_filters/filters.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
""" | ||
Processors filters, this file aims to contain all the filters that could modify the | ||
standard transformer results by implementing external pipeline steps. | ||
""" | ||
from openedx_filters.tooling import OpenEdxPublicFilter | ||
|
||
from event_routing_backends.processors.openedx_filters.exceptions import InvalidFilterType | ||
|
||
|
||
class ProcessorBaseFilter(OpenEdxPublicFilter): | ||
""" | ||
This is a general filter class that applies the open edx filter in multiple | ||
scenarios, its functionality is limited to one input one output therefore this | ||
only can be applied in method that returns a unique value. | ||
""" | ||
|
||
@classmethod | ||
def generate_dynamic_filter(cls, filter_type): | ||
"""This generates a sub class of ProcessorBaseFilter with the filter_type attribute. | ||
Arguments: | ||
filter_type: String the defines the filter key on the OPEN_EDX_FILTERS_CONFIG | ||
section | ||
Returns: | ||
ProcessorBaseFilter sub-class: This new class includes the filter_type attribute. | ||
""" | ||
return type("DynamicFilter", (cls,), {"filter_type": filter_type}) | ||
|
||
@classmethod | ||
def run_filter(cls, result): | ||
""" | ||
Executes a filter after validating the right class configuration. | ||
Arguments: | ||
result: Result to be modified or extended. | ||
Returns: | ||
result: This value comes from the dictionary returned by run_pipeline and will vary | ||
depends on the implemented pipelines. | ||
Raises: | ||
InvalidFilterType: if the ProcessorBaseFilter is used instead of a dynamic filter. | ||
""" | ||
if not cls.filter_type: | ||
raise InvalidFilterType("Parameter filter_type has not been set.") | ||
|
||
data = super().run_pipeline(result=result) | ||
|
||
return data.get("result", result) |
Empty file.
40 changes: 40 additions & 0 deletions
40
event_routing_backends/processors/tests/openedx_filters/test_filters.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"""Test cases for the filters file.""" | ||
from django.test import TestCase | ||
from mock import patch | ||
from openedx_filters.tooling import OpenEdxPublicFilter | ||
|
||
from event_routing_backends.processors.openedx_filters.exceptions import InvalidFilterType | ||
from event_routing_backends.processors.openedx_filters.filters import ProcessorBaseFilter | ||
|
||
|
||
class TestProcessorBaseFilter(TestCase): | ||
"""General test cases for the ProcessorBaseFilter class.""" | ||
|
||
def test_invalid_configuration(self): | ||
"""This test that the exception XApiInvalidFilterType is raised when | ||
the filter_type attribute has not been set. | ||
Expected behavior: | ||
- InvalidFilterType exception is raised | ||
""" | ||
self.assertRaises(InvalidFilterType, ProcessorBaseFilter.run_filter, "dummy_value") | ||
|
||
@patch.object(OpenEdxPublicFilter, "run_pipeline") | ||
def test_expected_value(self, run_pipeline_mock): | ||
"""This checks that the method run_filter returns the value generated by | ||
the parent method `run_pipeline` | ||
Expected behavior: | ||
- run_pipeline is called with the right key and value | ||
- run_filter returns the value of the result key | ||
""" | ||
run_pipeline_mock.return_value = { | ||
"result": "expected_value" | ||
} | ||
input_value = "dummy_value" | ||
openedx_filter = ProcessorBaseFilter.generate_dynamic_filter(filter_type="test_filter") | ||
|
||
result = openedx_filter.run_filter(result=input_value) | ||
|
||
run_pipeline_mock.assert_called_once_with(result=input_value) | ||
self.assertEqual(run_pipeline_mock()["result"], result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.