diff --git a/cms/djangoapps/contentstore/views/preview.py b/cms/djangoapps/contentstore/views/preview.py index b64a77ddb2..0d7e8786b4 100644 --- a/cms/djangoapps/contentstore/views/preview.py +++ b/cms/djangoapps/contentstore/views/preview.py @@ -212,7 +212,6 @@ def _preview_module_system(request, descriptor, field_data): # TODO (cpennington): Do we want to track how instructors are using the preview problems? track_function=lambda event_type, event: None, get_module=partial(_load_preview_module, request), - debug=True, mixins=settings.XBLOCK_MIXINS, course_id=course_id, diff --git a/common/lib/xmodule/xmodule/capa_module.py b/common/lib/xmodule/xmodule/capa_module.py index 250fd0974f..225d9aa46b 100644 --- a/common/lib/xmodule/xmodule/capa_module.py +++ b/common/lib/xmodule/xmodule/capa_module.py @@ -490,6 +490,15 @@ class ProblemBlock( return self.display_name + @property + def debug(self): + """ + If CAPA block fails to render, we want course authors to be able to see + the error in Studio. At the same time, in production, we don't want + to show errors to students. + """ + return getattr(self.runtime, 'is_author_mode', False) or settings.DEBUG + @classmethod def filter_templates(cls, template, course): """ @@ -825,7 +834,7 @@ class ProblemBlock( cache=cache_service, can_execute_unsafe_code=sandbox_service.can_execute_unsafe_code, get_python_lib_zip=sandbox_service.get_python_lib_zip, - DEBUG=self.runtime.DEBUG, + DEBUG=self.debug, i18n=self.runtime.service(self, "i18n"), render_template=self.runtime.service(self, 'mako').render_template, resources_fs=self.runtime.resources_fs, @@ -1060,8 +1069,7 @@ class ProblemBlock( str(err) ) - # TODO (vshnayder): another switch on DEBUG. - if self.runtime.DEBUG: + if self.debug: msg = HTML( '[courseware.capa.capa_module] ' 'Failed to generate HTML for problem {url}' @@ -1777,7 +1785,7 @@ class ProblemBlock( self.set_last_submission_time() except (StudentInputError, ResponseError, LoncapaProblemError) as inst: - if self.runtime.DEBUG: + if self.debug: log.warning( "StudentInputError in capa_module:problem_check", exc_info=True @@ -1811,7 +1819,7 @@ class ProblemBlock( self.set_state_from_lcp() self.set_score(self.score_from_lcp(self.lcp)) - if self.runtime.DEBUG: + if self.debug: msg = f"Error checking problem: {str(err)}" msg += f'\nTraceback:\n{traceback.format_exc()}' return {'success': msg} diff --git a/common/lib/xmodule/xmodule/lti_2_util.py b/common/lib/xmodule/xmodule/lti_2_util.py index 17dfe680c0..3829139b7b 100644 --- a/common/lib/xmodule/xmodule/lti_2_util.py +++ b/common/lib/xmodule/xmodule/lti_2_util.py @@ -12,6 +12,7 @@ import re from unittest import mock from urllib import parse +from django.conf import settings from oauthlib.oauth1 import Client from webob import Response from xblock.core import XBlock @@ -63,7 +64,7 @@ class LTI20BlockMixin: Returns: webob.response: response to this request. See above for details. """ - if self.system.debug: + if settings.DEBUG: self._log_correct_authorization_header(request) if not self.accept_grades_past_due and self.is_past_due(): diff --git a/common/lib/xmodule/xmodule/tests/__init__.py b/common/lib/xmodule/xmodule/tests/__init__.py index 206784dc24..0925073099 100644 --- a/common/lib/xmodule/xmodule/tests/__init__.py +++ b/common/lib/xmodule/xmodule/tests/__init__.py @@ -153,7 +153,6 @@ def get_test_system( static_url='/static', track_function=Mock(name='get_test_system.track_function'), get_module=get_module, - debug=True, hostname="edx.org", services={ 'user': user_service, diff --git a/common/lib/xmodule/xmodule/tests/test_capa_module.py b/common/lib/xmodule/xmodule/tests/test_capa_module.py index 26ff575f81..41acc05a93 100644 --- a/common/lib/xmodule/xmodule/tests/test_capa_module.py +++ b/common/lib/xmodule/xmodule/tests/test_capa_module.py @@ -17,6 +17,7 @@ import ddt import requests import webob from codejail.safe_exec import SafeExecException +from django.test import override_settings from django.utils.encoding import smart_str from edx_user_state_client.interface import XBlockUserState from lxml import etree @@ -961,6 +962,7 @@ class ProblemBlockTest(unittest.TestCase): # lint-amnesty, pylint: disable=miss # but that this was considered the second attempt for grading purposes assert module.lcp.context['attempt'] == 2 + @override_settings(DEBUG=True) def test_submit_problem_other_errors(self): """ Test that errors other than the expected kinds give an appropriate message. @@ -970,9 +972,6 @@ class ProblemBlockTest(unittest.TestCase): # lint-amnesty, pylint: disable=miss # Create the module module = CapaFactory.create(attempts=1, user_is_staff=False) - # Ensure that DEBUG is on - module.system.DEBUG = True - # Simulate answering a problem that raises the exception with patch('capa.capa_problem.LoncapaProblem.grade_answers') as mock_grade: error_msg = "Superterrible error happened: ☠" @@ -1711,9 +1710,6 @@ class ProblemBlockTest(unittest.TestCase): # lint-amnesty, pylint: disable=miss # is asked to render itself as HTML module.lcp.get_html = Mock(side_effect=Exception("Test")) - # Turn off DEBUG - module.system.DEBUG = False - # Try to render the module with DEBUG turned off html = module.get_problem_html() @@ -1727,6 +1723,31 @@ class ProblemBlockTest(unittest.TestCase): # lint-amnesty, pylint: disable=miss # Expect that the module has created a new dummy problem with the error assert original_problem != module.lcp + def test_get_problem_html_error_preview(self): + """ + Test the html response when an error occurs with DEBUG off in Studio. + """ + render_template = Mock(return_value="