Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ Change Log

Unreleased
~~~~~~~~~~
Added
_____

* Filter definitions for registration and login.
* Sensitive data mixin for filters.

Changed
_______

* Pipeline runner from `run` to `run_filter`.
* Moved filters definitions to filters file inside their domain.

[0.3.0] - 2021-11-24
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
6 changes: 3 additions & 3 deletions openedx_filters/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class PipelineStep:

class MyFilterStep(PipelineStep):

def run(self, user, course_key, mode):
def run_filter(self, user, course_key, mode):
if mode != "honor":
return

Expand All @@ -35,7 +35,7 @@ def run(self, user, course_key, mode):

class MyFilterStep(PipelineStep):

def run(self, user, course_key, mode):
def run_filter(self, user, course_key, mode):
if mode != "honor":
return

Expand All @@ -55,7 +55,7 @@ def __init__(self, filter_type, running_pipeline, **extra_config):
self.running_pipeline = running_pipeline
self.extra_config = extra_config

def run(self, **kwargs): # pylint: disable=unused-argument
def run_filter(self, **kwargs): # pylint: disable=unused-argument
"""
Abstract pipeline step runner.

Expand Down
33 changes: 0 additions & 33 deletions openedx_filters/learning/enrollment.py

This file was deleted.

95 changes: 95 additions & 0 deletions openedx_filters/learning/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"""
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 StudentRegistrationRequested(OpenEdxPublicFilter, SensitiveDataManagementMixin):
"""
Custom class used to create PreRegister filters.
"""

filter_type = "org.openedx.learning.student.registration.requested.v1"
sensitive_form_data = [
"password", "newpassword", "new_password", "oldpassword", "old_password", "new_password1", "new_password2",
]

class PreventRegistration(OpenEdxFilterException):
"""
Custom class used to stop the registration process.
"""

@classmethod
def run_filter(cls, form_data):
"""
Execute a filter with the signature specified.

Arguments:
form_data (QueryDict): contains the request.data submitted by the registration
form.
"""
sensitive_data = cls.extract_sensitive_data(form_data)
data = super().run_pipeline(form_data=form_data)
form_data = data.get("form_data")
form_data.update(sensitive_data)
return form_data


class StudentLoginRequested(OpenEdxPublicFilter):
"""
Custom class used to create PreLogin filters.
"""

filter_type = "org.openedx.learning.student.login.requested.v1"

class PreventLogin(OpenEdxFilterException):
"""
Custom class used to stop the login process.
"""

def __init__(self, message, redirect_to=None, error_code="", context=None):
"""
Override init that defines specific arguments used in the login process.
"""
super().__init__(message, redirect_to=redirect_to, error_code=error_code, context=context)

@classmethod
def run_filter(cls, user):
"""
Execute a filter with the signature specified.

Arguments:
user (User): is a Django User object.
"""
data = super().run_pipeline(user=user)
return data.get("user")


class CourseEnrollmentStarted(OpenEdxPublicFilter):
"""
Custom class used to create PreEnrollment filters.
"""

filter_type = "org.openedx.learning.course.enrollment.started.v1"

class PreventEnrollment(OpenEdxFilterException):
"""
Custom class used to stop the enrollment process.
"""

@classmethod
def run_filter(cls, user, course_key, mode):
"""
Execute a filter with the signature specified.

Arguments:
user (User): is a Django User object.
course_key (CourseKey): name of the filter.
mode (str): is a string specifying what kind of enrollment.
"""
data = super().run_pipeline(
user=user, course_key=course_key, mode=mode,
)
return data.get("user"), data.get("course_key"), data.get("mode")
4 changes: 2 additions & 2 deletions openedx_filters/tests/test_tooling.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class FirstPipelineStep(PipelineStep):
Utility function used when getting steps for pipeline.
"""

def run(self, **kwargs):
def run_filter(self, **kwargs):
pass


Expand All @@ -30,7 +30,7 @@ class SecondPipelineStep(PipelineStep):
Utility class used when getting steps for pipeline.
"""

def run(self, **kwargs):
def run_filter(self, **kwargs):
pass


Expand Down
35 changes: 35 additions & 0 deletions openedx_filters/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
Utilities for Open edX filters usage.
"""


class SensitiveDataManagementMixin:
"""
Custom class used manage sensitive data within filter arguments.
"""

sensitive_form_data = []

@classmethod
def extract_sensitive_data(cls, form_data):
"""
Extract sensitive data from its child class input arguments.

Example usage:

>> sensitive_form_data = ["password"] # Specified in FilterExample
>> form_data = {"username": "example", "password": "password"}
>> sensitive_data = FilterExample.extract_sensitive_data(form_data)
>> sensitive_data
{"password": "password"}
>> form_data
{"username": "example"}
"""
sensitive_data = {}
base_form_data = form_data.copy()
for key, value in base_form_data.items():
if key in cls.sensitive_form_data:
form_data.pop(key)
sensitive_data[key] = value

return sensitive_data