From 44d8950a83578011c7bb03bdf27ec24e34ea82eb Mon Sep 17 00:00:00 2001 From: Maria Grimaldi Date: Wed, 28 Jun 2023 17:29:45 -0400 Subject: [PATCH] docs: add documentation for current filters tooling --- docs/concepts/glossary.rst | 60 +++++++ docs/concepts/index.rst | 1 + docs/how-tos/create-new-filter.rst | 167 +++++++++++++++++- docs/how-tos/using-filters.rst | 27 +-- .../use-filters-to-change-enrollment.rst | 1 - docs/reference/django-plugins-and-filters.rst | 38 ++++ docs/reference/index.rst | 1 - .../pipeline-implementation-details.rst | 2 - 8 files changed, 276 insertions(+), 21 deletions(-) create mode 100644 docs/concepts/glossary.rst delete mode 100644 docs/reference/pipeline-implementation-details.rst diff --git a/docs/concepts/glossary.rst b/docs/concepts/glossary.rst new file mode 100644 index 0000000..6e35060 --- /dev/null +++ b/docs/concepts/glossary.rst @@ -0,0 +1,60 @@ +Open edX filters glossary +########################## + +This glossary provides definitions for some of the concepts needed to use the Open edX Filters library. + + +Pipelines +--------- + +A pipeline is a list of functions that are executed in order. Each function receives the output of the previous function as input. The output of the last function is the output of the pipeline. + +Pipeline steps +-------------- + +A pipeline step is a function that receives data, manipulates it and returns it. It can be used to transform data, to validate it, to filter it, to enrich it, etc. + +Open edX Filter +--------------- + +An Open edX Filter is a Python class used for executing pipelines or list of functions in specific order. It implements a `run_filter` method that receives the data to be processed and returns the output of the pipeline. + +Filter signature +---------------- + +It's the signature of the `run_filter` method. It defines the input and output of the filter. The input is a dictionary with the data to be processed. The output is a dictionary with the processed data. + +Filter type +----------- + +It's the filter identifier. It's used to identify the filter in the configuration settings. When configuring the pipeline for a filter, the type is as an index for the filter configuration. + +Filter exceptions +----------------- + +Besides acting as a filter, an Open edX Filter can also raise exceptions. These exceptions are used to control the execution of the pipeline. If an exception is raised, the pipeline execution is stopped and the exception is raised again as the output of the pipeline. + +Filter configuration +-------------------- + +The filter configuration is a dictionary with the configuration settings for the filter. It's used to configure the pipeline for a filter. The configuration settings are specific for each filter type. The dictionary looks like this: + +.. code-block:: python + + OPEN_EDX_FILTERS_CONFIG = { + "": { + "fail_silently": , + "pipeline": [ + "", + "", + ... + "", + ] + }, + } + +Where: + +- ```` is the filter type. +- ``fail_silently`` is a boolean value. If ``True``, the method ``run_pipeline`` won't raise runtime exceptions, and the pipeline execution will resume if one is raised. If ``False``, it will raise runtime exceptions and the pipeline execution will stop. By runtime exceptions we mean exceptions like ``ImportError`` or ``AttributeError``, which are not raised by the filter itself. +- ``pipeline`` is list of paths for each pipeline step. diff --git a/docs/concepts/index.rst b/docs/concepts/index.rst index cade161..e3f391e 100644 --- a/docs/concepts/index.rst +++ b/docs/concepts/index.rst @@ -6,3 +6,4 @@ Concepts :caption: Contents: hooks-extension-framework + glossary diff --git a/docs/how-tos/create-new-filter.rst b/docs/how-tos/create-new-filter.rst index cb60681..8c8ed88 100644 --- a/docs/how-tos/create-new-filter.rst +++ b/docs/how-tos/create-new-filter.rst @@ -3,8 +3,11 @@ How-to Create a new Filter .. How-tos should have a short introduction sentence that captures the user's goal and introduces the steps. -This how-to will help you add a new filter to the edx-platform codebase using -openedx-filters. +The mechanisms implemented by the Open edX Filters library are supported and maintained by the Open edX community. The +library is designed to be extensible, and we welcome contributions of new filters. + +Therefore, we've put together this guide that will walk you through the process of adding a new filter to the library, +and will provide you with a template to follow when adding new filters. Assumptions *********** @@ -13,6 +16,8 @@ Assumptions person who is following the How-to. The assumptions may link to other how-tos if possible. +* You have a development environment set up. +* You have a basic understanding of Python and Django. * You understand the concept of filters or have reviewed the relevant :doc:`/concepts/index` docs. @@ -21,14 +26,166 @@ Steps .. A task should have 3 - 7 steps. Tasks with more should be broken down into digestible chunks. -#. Step 1. +#. Propose a new filter to the Open edX community + +When creating a new filter, you must justify its implementation. For example, you could create a post in Discuss, +send a message through slack or open a new issue in the library repository listing your use cases for it. Or even, +if you have time, you could accompany your proposal with the implementation of the filter to illustrate its behavior. + +#. Place your filter in an architecture subdomain + +As specified in the Architectural Decisions Record (ADR) filter naming and versioning, the filter definition needs an Open edX Architecture +Subdomain for: + +- The type of the filter: ``{Reverse DNS}.{Architecture Subdomain}.{Subject}.{Action}.{Major Version}`` +- The package name where the definition will live, eg. ``learning/``. + +For those reasons, after studying your new filter purpose, you must place it in one of the subdomains already in use, or introduce a new subdomain: + ++-------------------+----------------------------------------------------------------------------------------------------+ +| Subdomain name | Description | ++===================+====================================================================================================+ +| Learning | Allows learners to consume content and perform actions in a learning activity on the platform. | ++-------------------+----------------------------------------------------------------------------------------------------+ + +New subdomains may require some discussion, because there does not yet exist and agreed upon set on subdomains. So we encourage you to start the conversation +as soon as possible through any of the communication channels available. + +Refer to `edX DDD Bounded Contexts `_ confluence page for more documentation on domain-driven design in the Open edX project. + +#. Define the filter's behavior + +Defining the filter's behavior includes: + +- Defining the filter type for identification +- Defining the filter's signature +- Defining the filter's behavior for stopping the process in which it is being used -#. Step 2. +The filter type is the name that will be used to identify the filter's and it'd help others identifying its purpose. For example, if you're creating a filter that will be used during the student registration process in the LMS, +according to the documentation, the filter type is defined as follows: -#. Step 3. +``{Reverse DNS}.{Architecture Subdomain}.student.registration.requested.{Major Version}`` + +Where ``student`` is the subject and ``registration.requested`` the action being performed. The major version is the version of the filter, which will be incremented +when a change is made to the filter that is not backwards compatible, as explained in the ADR. + +Now that you have the filter type, you'll need to define the filter's signature and overall behavior. The filter's signature, which is the set of parameters that the filter will manipulate, depends on where the filter is located. For example, +if you're creating a filter that will be used during the student registration process in the LMS, the filter's signature will be the set of parameters available for that time for the user. In this case, the filter's signature will be the set of parameters that the registration form sends to the LMS. + +To get to this conclusions for your specific filter, you can ask yourself the following questions: + +- What is the filter's purpose? (e.g. to validate the student's email address) +- What parameters will the filter need to to that? (e.g. the email address) +- Where in the registration process will the filter be used? (e.g. after the student submits the registration form but before anything else) + +With that information, you can define the filter's signature: + +- Arguments: ``email``. Since we want this filter to be broadly used, we'll add as much relevant information as possible for the user at that point. As we mentioned above, we can send more information stored in the registration form like ``name`` or ``username``. +- Returns: since filters take in a set of parameters and return a set of parameters, we'll return the same set of parameters that we received. + +Since filters also can act according to the result of the filter's execution, we'll need to define the filter's behavior for when the filter stops the process in which it is being used. For example, if you're using the filter in the LMS, you'll need to define +what happens when the filter stops the registration process. So, for this filter we'll define the following behavior: + +- When stopping the registration process, we'll raise a ``PreventRegistration`` exception. + +#. Implement the new filter .. Following the steps, you should add the result and any follow-up tasks needed. +Up to this point, you should have the following: + +.. code-block:: python + + class StudentRegistrationRequested(OpenEdxPublicFilter): + """ + Custom class used to create registration filters and its custom methods. + """ + + filter_type = "org.openedx.learning.student.registration.requested.v1" + + 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) + return data.get("form_data") + +.. note:: + This is not exactly what the registration filter looks like, but it's a good starting point. You can find the full implementation of the registration filter in the library's repository. + +Some things to note: + +- The filter's type is defined in the ``filter_type`` class attribute. In this case, the filter type is ``org.openedx.learning.student.registration.requested.v1``. +- The filter's signature is defined in the ``run_filter`` method. In this case, the signature is the ``form_data`` parameter. +- The ``run_filter`` is a class method that returns the same set of parameters that it receives. +- The ``run_filter`` class method calls the ``run_pipeline`` method, which is the method that executes the filter's logic. This method is defined in the ``OpenEdxPublicFilter`` class, which is the base class for all the filters in the library. This method returns a dictionary with the following structure: + + .. code-block:: python + + { + "form_data": form_data, + } + + Where ``form_data`` is the same set of parameters that the filter receives, which is the accumulated output for the filter's pipeline. This is how ``run_filter`` should always look like. +- The filter's behavior for stopping the process is defined in the ``PreventRegistration`` exception which inherits from the ``OpenEdxFilterException`` base exception. In this case, the exception is raised when the filter stops the registration process. This is done in the service where the filter is being used, which in this case is the LMS. +- The class name is the filter's type ``{Subject}.{Action}`` part in a camel case format. In this case, the filter's name is ``StudentRegistrationRequested``. + +#. Add tests for the new filter + +Each filter has its own set of tests. The tests for the filter you're creating should be located in the ``tests`` directory in the library's repository. The tests should be located in the ``test_filters.py`` file, which is where all the tests for the filters are located. Each set of tests is related to a specific type of filter, so you should add your tests to the set of tests that are related to the filter you're creating. +For example, if you're creating a filter that will be used during the student registration process in the LMS, you should add your tests to the ``TestAuthFilters`` set of tests. This is how the tests for the registration filter look like: + + +.. code-block:: python + + def test_student_registration_requested(self): + """ + Test StudentRegistrationRequested filter behavior under normal conditions. + + Expected behavior: + - The filter must have the signature specified. + - The filter should return form data. + """ + expected_form_data = { + "password": "password", + "newpassword": "password", + "username": "username", + } + + form_data = StudentRegistrationRequested.run_filter(expected_form_data) + + self.assertEqual(expected_form_data, form_data) + + @data( + ( + StudentRegistrationRequested.PreventRegistration, {"message": "Can't register in this site."} + ), + ) + @unpack + def test_halt_student_auth_process(self, auth_exception, attributes): + """ + Test for student auth exceptions attributes. + + Expected behavior: + - The exception must have the attributes specified. + """ + exception = auth_exception(**attributes) + + self.assertDictContainsSubset(attributes, exception.__dict__) + +Basically, we're testing the filter's signature and the filter's behavior for stopping the process. The first test is testing the filter's signature, which is the set of parameters that the filter receives and returns. The second test is testing the filter's behavior for stopping the process, which is the exception that is raised when the filter stops the process. + .. .. seealso:: :ref:`title to link to` diff --git a/docs/how-tos/using-filters.rst b/docs/how-tos/using-filters.rst index e17cc24..c97bcec 100644 --- a/docs/how-tos/using-filters.rst +++ b/docs/how-tos/using-filters.rst @@ -1,11 +1,10 @@ -How to use ----------- +How to use Open edX Filters +--------------------------- Using openedx-filters in your code is very straight forward. We can consider the -two possible cases: +various use cases: implementing pipeline steps, attaching/hooking pipelines to filter, +and triggering a filter. We'll also cover how to test the filters you create in your service. -Configuring a filter -^^^^^^^^^^^^^^^^^^^^ Implement pipeline steps ************************ @@ -23,6 +22,9 @@ and can be implemented in an installable Python library: from openedx_filters.learning.filters import CertificateCreationRequested class StopCertificateCreation(PipelineStep): + """ + Stop certificate creation if user is not in third party service. + """ def run_filter(self, user, course_id, mode, status): # Consult third party service and check if continue @@ -57,7 +59,7 @@ filter to execute our pipeline. } Triggering a filter -^^^^^^^^^^^^^^^^^^^ +******************* In order to execute a filter in your own plugin/library, you must install the plugin where the steps are implemented and also, ``openedx-filters``. @@ -68,14 +70,14 @@ plugin where the steps are implemented and also, ``openedx-filters``. from openedx_filters.learning.filters import CertificateCreationRequested try: - self.user, self.course_id, self.mode, self.status = CertificateCreationRequested.run_filter( - user=self.user, course_id=self.course_id, mode=self.mode, status=self.status, + user, course_id, mode, status = CertificateCreationRequested.run_filter( + user=user, course_id=course_id, mode=mode, status=status, ) except CertificateCreationRequested.PreventCertificateCreation as exc: raise CertificateGenerationNotAllowed(str(exc)) from exc Testing filters' steps -^^^^^^^^^^^^^^^^^^^^^^ +********************** It's pretty straightforward to test your pipeline steps, you'll need to include the ``openedx-filters`` library in your testing dependencies and configure them in your test case. @@ -102,14 +104,15 @@ It's pretty straightforward to test your pipeline steps, you'll need to include - The pipeline step configured for the filter raises PreventCertificateCreation when the conditions are met. """ + ... with self.assertRaises(CertificateCreationRequested.PreventCertificateCreation): CertificateCreationRequested.run_filter( - user=self.user, course_key=self.course_key, mode="audit", + user=user, course_key=course_key, mode="audit", ) # run your assertions Changes in the ``openedx-filters`` library that are not compatible with your code should break this kind of test in CI and let you know you need to upgrade your code. -The main limitation while testing filters' steps it's their arguments, as they are edxapp -memory objects, but that can be solved in CI using Python mocks. +The main limitation while testing filters' steps it's their arguments, as they are +in-memory objects, but that can be solved in CI using Python mocks. diff --git a/docs/quickstarts/use-filters-to-change-enrollment.rst b/docs/quickstarts/use-filters-to-change-enrollment.rst index a493ae7..2232069 100644 --- a/docs/quickstarts/use-filters-to-change-enrollment.rst +++ b/docs/quickstarts/use-filters-to-change-enrollment.rst @@ -9,4 +9,3 @@ you can find minimal steps exemplifying the different ways on how to use ``openedx-filters``. .. _openedx-filters-samples: https://github.com/eduNEXT/openedx-filters-samples - diff --git a/docs/reference/django-plugins-and-filters.rst b/docs/reference/django-plugins-and-filters.rst index 1548d84..7e78c42 100644 --- a/docs/reference/django-plugins-and-filters.rst +++ b/docs/reference/django-plugins-and-filters.rst @@ -1,3 +1,41 @@ Django Plugins and Filters ########################## +Django plugins is one of the most valuable extension mechanisms for the Open edX platform. In this section, we will +guide you through the process of using filters inside your own plugin. + + +Use filters inside your plugin +****************************** + +Imagine you have your own registration plugin and you want to add a filter to it. The first thing you need to do is +adding ``openedx-filters`` to your requirements file. Then, you can import the registration filter and use it inside +your registration flow as it's used in the LMS registration flow. You can even add your own filters to your registration, +after implementing their definitions in your plugin. + +Configure filters +***************** + +Filters are configured in the ``OPEN_EDX_FILTERS_CONFIG`` dictionary which can we specified in your plugin's settings +file. The dictionary has the following structure: + +.. code-block:: python + + OPEN_EDX_FILTERS_CONFIG = { + "": { + "fail_silently": , + "pipeline": [ + "", + "", + ... + "", + ] + }, + } + + +Create pipeline steps +********************* + +In your own plugin, you can create your own pipeline steps by inheriting from ``PipelineStep`` and implementing the +``run_filter`` method. You can find examples of pipeline steps in the ``openedx-filters-samples`` repository. See :doc:`/quickstarts/index` for more details. diff --git a/docs/reference/index.rst b/docs/reference/index.rst index 7a3bcf7..06f61ee 100644 --- a/docs/reference/index.rst +++ b/docs/reference/index.rst @@ -6,4 +6,3 @@ References :caption: Contents: django-plugins-and-filters - pipeline-implementation-details diff --git a/docs/reference/pipeline-implementation-details.rst b/docs/reference/pipeline-implementation-details.rst deleted file mode 100644 index 6ce96e2..0000000 --- a/docs/reference/pipeline-implementation-details.rst +++ /dev/null @@ -1,2 +0,0 @@ -Pipeline Implementation Details -###############################