Skip to content

Commit

Permalink
feat: add ora filter for submission step rendering (#2204)
Browse files Browse the repository at this point in the history
* feat: add ora filter for submission step rendering
* chore: add `openedx-filters` in base requirements
* test: add filter unit tests
  • Loading branch information
BryanttV authored Apr 18, 2024
1 parent 00934e2 commit d8eeddf
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 95 deletions.
82 changes: 81 additions & 1 deletion openassessment/xblock/test/test_submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
from django.core.exceptions import ObjectDoesNotExist
from django.test.utils import override_settings
from freezegun import freeze_time
from openedx_filters import PipelineStep
from openedx_filters.learning.filters import ORASubmissionViewRenderStarted

from xblock.exceptions import NoSuchServiceError
from submissions import api as sub_api
Expand All @@ -29,7 +31,7 @@
from openassessment.xblock.apis.submissions import submissions_actions
from openassessment.xblock.utils.data_conversion import create_submission_dict, prepare_submission_for_serialization
from openassessment.xblock.openassessmentblock import OpenAssessmentBlock
from openassessment.xblock.ui_mixins.legacy.views.submission import get_team_submission_context
from openassessment.xblock.ui_mixins.legacy.views.submission import get_team_submission_context, render_submission
from openassessment.xblock.workflow_mixin import WorkflowMixin
from openassessment.xblock.test.test_team import MockTeamsService, MOCK_TEAM_ID

Expand Down Expand Up @@ -79,6 +81,22 @@ def setup_mock_team(xblock):
return setup_mock_team(xblock)


class TestRenderInvalidTemplate(PipelineStep):
"""
Utility class used when getting steps for pipeline.
"""

def run_filter(self, context, template_name): # pylint: disable=arguments-differ
"""
Pipeline step that stops the course about render process.
"""
raise ORASubmissionViewRenderStarted.RenderInvalidTemplate(
"Invalid template.",
context={"context": "current_context"},
template_name="current/path/template.html",
)


@ddt.ddt
class SubmissionTest(SubmissionXBlockHandlerTestCase, SubmissionTestMixin):
""" Test Submissions Api for Open Assessments. """
Expand Down Expand Up @@ -822,6 +840,68 @@ def test_team_open_unanswered(self, xblock):
}
)

@patch('openassessment.xblock.ui_mixins.legacy.views.submission.get_submission_path')
@patch.object(OpenAssessmentBlock, "render_assessment")
@patch.object(ORASubmissionViewRenderStarted, "run_filter")
@scenario('data/submission_open.xml', user_id="Red Five")
def test_render_submission_no_run_filter(
self,
xblock,
mock_run_filter: Mock,
mock_render_assessment: Mock,
mock_get_submission_path: Mock
):
"""
Test for `render_submission` when the `run_filter` method is not called.
"""
mock_get_submission_path.return_value = "another/path/template.html"

render_submission(xblock.config_data, xblock.submission_data)

mock_run_filter.assert_not_called()
mock_render_assessment.assert_called_once()

@patch.object(OpenAssessmentBlock, "render_assessment")
@patch.object(ORASubmissionViewRenderStarted, "run_filter")
@scenario('data/submission_open.xml', user_id="Red Five")
def test_render_submission_run_filter(
self, xblock, mock_run_filter: Mock, mock_render_assessment: Mock
):
"""
Test for `render_submission` when the `run_filter` method is called.
"""
expected_context = {"context": "new_context"}
expected_path = "new/path/template.html"
mock_run_filter.return_value = (expected_context, expected_path)

render_submission(xblock.config_data, xblock.submission_data)

mock_run_filter.assert_called_once()
mock_render_assessment.assert_called_once_with(expected_path, expected_context)

@override_settings(
OPEN_EDX_FILTERS_CONFIG={
"org.openedx.learning.ora.submission_view.render.started.v1": {
"fail_silently": False,
"pipeline": [
"openassessment.xblock.test.test_submission.TestRenderInvalidTemplate"
]
}
}
)
@patch.object(OpenAssessmentBlock, "render_assessment")
@scenario('data/submission_open.xml', user_id="Red Five")
def test_render_submission_run_filter_exception(self, xblock, mock_render_assessment: Mock):
"""
Test for `render_submission` when the `run_filter` method raises an exception.
"""
expected_context = {"context": "current_context"}
expected_path = "current/path/template.html"

render_submission(xblock.config_data, xblock.submission_data)

mock_render_assessment.assert_called_once_with(expected_path, expected_context)

@patch('submissions.team_api.get_teammates_with_submissions_from_other_teams')
@scenario('data/submission_open.xml', user_id="Red Five")
def test_get_team_submission_context(
Expand Down
9 changes: 9 additions & 0 deletions openassessment/xblock/ui_mixins/legacy/views/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging

from django.core.exceptions import ObjectDoesNotExist
from openedx_filters.learning.filters import ORASubmissionViewRenderStarted
from xblock.exceptions import NoSuchServiceError

from openassessment.xblock.utils.data_conversion import (
Expand Down Expand Up @@ -37,6 +38,14 @@ def render_submission(config, submission_info):
context = get_submission_context(config, submission_info)
path = get_submission_path(submission_info)

if path == "legacy/response/oa_response.html":
try:
# .. filter_implemented_name: ORASubmissionViewRenderStarted
# .. filter_type: org.openedx.learning.ora.submission_view.render.started.v1
context, path = ORASubmissionViewRenderStarted.run_filter(context, path)
except ORASubmissionViewRenderStarted.RenderInvalidTemplate as exc:
context, path = exc.context, exc.template_name # pylint: disable=no-member

return config.render_assessment(path, context_dict=context)


Expand Down
1 change: 1 addition & 0 deletions requirements/base.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edx-toggles
djangorestframework
Xblock
edx-opaque-keys
openedx-filters

django
django-simple-history
Expand Down
34 changes: 19 additions & 15 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ backports-zoneinfo==0.2.1 ; python_version < "3.9"
# via
# -c requirements/constraints.txt
# django
# djangorestframework
bleach==6.1.0
# via -r requirements/base.in
boto3==1.34.72
boto3==1.34.83
# via -r requirements/base.in
botocore==1.34.72
botocore==1.34.83
# via
# boto3
# s3transfer
Expand All @@ -30,7 +31,7 @@ click==8.1.7
# via
# code-annotations
# edx-django-utils
code-annotations==1.7.0
code-annotations==1.8.0
# via edx-toggles
defusedxml==0.7.1
# via -r requirements/base.in
Expand All @@ -47,11 +48,12 @@ django==4.2.11
# edx-submissions
# edx-toggles
# jsonfield
# openedx-filters
django-crum==0.7.9
# via
# edx-django-utils
# edx-toggles
django-model-utils==4.4.0
django-model-utils==4.5.0
# via
# -r requirements/base.in
# edx-submissions
Expand All @@ -63,21 +65,21 @@ django-waffle==4.1.0
# via
# edx-django-utils
# edx-toggles
djangorestframework==3.14.0
djangorestframework==3.15.1
# via
# -r requirements/base.in
# edx-submissions
edx-django-utils==5.11.0
edx-django-utils==5.12.0
# via
# -r requirements/base.in
# edx-toggles
edx-i18n-tools==1.3.0
edx-i18n-tools==1.5.0
# via -r requirements/base.in
edx-opaque-keys==2.5.1
# via -r requirements/base.in
edx-submissions==3.6.0
edx-submissions==3.7.0
# via -r requirements/base.in
edx-toggles==5.1.1
edx-toggles==5.2.0
# via -r requirements/base.in
fs==2.0.18
# via
Expand Down Expand Up @@ -107,10 +109,11 @@ loremipsum==1.0.5
# -r requirements/base.in
lxml==4.9.4
# via
# -c requirements/constraints.txt
# -r requirements/base.in
# edx-i18n-tools
# xblock
mako==1.3.2
mako==1.3.3
# via xblock
markupsafe==2.1.5
# via
Expand All @@ -119,6 +122,8 @@ markupsafe==2.1.5
# xblock
newrelic==9.8.0
# via edx-django-utils
openedx-filters==1.8.0
# via -r requirements/base.in
path==13.1.0
# via
# -c requirements/constraints.txt
Expand All @@ -132,7 +137,7 @@ polib==1.2.0
# via edx-i18n-tools
psutil==5.9.8
# via edx-django-utils
pycparser==2.21
pycparser==2.22
# via cffi
pymongo==3.13.0
# via edx-opaque-keys
Expand All @@ -152,7 +157,6 @@ python-swiftclient==3.13.1
pytz==2024.1
# via
# -r requirements/base.in
# djangorestframework
# edx-submissions
# fs
# xblock
Expand Down Expand Up @@ -183,7 +187,7 @@ stevedore==5.2.0
# edx-opaque-keys
text-unidecode==1.3
# via python-slugify
typing-extensions==4.10.0
typing-extensions==4.11.0
# via
# asgiref
# edx-opaque-keys
Expand All @@ -195,15 +199,15 @@ voluptuous==0.14.2
# via
# -c requirements/constraints.txt
# -r requirements/base.in
web-fragments==2.1.0
web-fragments==2.2.0
# via xblock
webencodings==0.5.1
# via
# bleach
# html5lib
webob==1.8.7
# via xblock
xblock==2.0.0
xblock==3.1.0
# via -r requirements/base.in

# The following packages are considered to be unsafe in a requirements file:
Expand Down
2 changes: 1 addition & 1 deletion requirements/ci.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ distlib==0.3.8
# virtualenv
docopt==0.6.2
# via coveralls
filelock==3.13.3
filelock==3.13.4
# via
# -r requirements/tox.txt
# tox
Expand Down
2 changes: 1 addition & 1 deletion requirements/docs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ sphinxcontrib-serializinghtml==1.1.5
# via sphinx
tornado==6.4
# via livereload
typing-extensions==4.10.0
typing-extensions==4.11.0
# via pydata-sphinx-theme
urllib3==2.2.1
# via requests
Expand Down
Loading

0 comments on commit d8eeddf

Please sign in to comment.