From c068bc95cf02f1309fc8b2bf2a63891c5bacfb58 Mon Sep 17 00:00:00 2001 From: Saleem Latif Date: Wed, 11 Nov 2015 18:03:45 +0500 Subject: [PATCH] Custom 500 error message should only be seen for Preview --- common/djangoapps/util/views.py | 19 +++++++++++++++---- .../certificates/tests/test_webview_views.py | 6 +++++- lms/djangoapps/certificates/views/webview.py | 5 ++++- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/common/djangoapps/util/views.py b/common/djangoapps/util/views.py index cfa37c62c9..99083db7fc 100644 --- a/common/djangoapps/util/views.py +++ b/common/djangoapps/util/views.py @@ -56,13 +56,18 @@ def jsonable_server_error(request, template_name='500.html'): return server_error(request, template_name=template_name) -def handle_500(template_path, context=None): +def handle_500(template_path, context=None, test_func=None): """ Decorator for view specific 500 error handling. + Custom handling will be skipped only if test_func is passed and it returns False - Usage:: + Usage: - @handle_500(template_path='certificates/server-error.html', context={'error-info': 'Internal Server Error'}) + @handle_500( + template_path='certificates/server-error.html', + context={'error-info': 'Internal Server Error'}, + test_func=lambda request: request.GET.get('preview', None) + ) def my_view(request): # Any unhandled exception in this view would be handled by the handle_500 decorator # ... @@ -83,9 +88,15 @@ def handle_500(template_path, context=None): if settings.DEBUG: # In debug mode let django process the 500 errors and display debug info for the developer raise - else: + elif test_func is None or test_func(request): + # Display custom 500 page if either + # 1. test_func is None (meaning nothing to test) + # 2. or test_func(request) returns True log.exception("Error in django view.") return render_to_response(template_path, context) + else: + # Do not show custom 500 error when test fails + raise return inner return decorator diff --git a/lms/djangoapps/certificates/tests/test_webview_views.py b/lms/djangoapps/certificates/tests/test_webview_views.py index 18727eb04e..362122e276 100644 --- a/lms/djangoapps/certificates/tests/test_webview_views.py +++ b/lms/djangoapps/certificates/tests/test_webview_views.py @@ -454,9 +454,13 @@ class CertificatesViewsTests(ModuleStoreTestCase, EventTrackingTestCase): user_id=self.user.id, course_id=unicode(self.course.id) ) - response = self.client.get(test_url) + response = self.client.get(test_url + "?preview=honor") self.assertIn("Invalid Certificate Configuration", response.content) + # Verify that Exception is raised when certificate is not in the preview mode + with self.assertRaises(Exception): + self.client.get(test_url) + @override_settings(FEATURES=FEATURES_WITH_CERTS_ENABLED) def test_certificate_evidence_event_emitted(self): self.client.logout() diff --git a/lms/djangoapps/certificates/views/webview.py b/lms/djangoapps/certificates/views/webview.py index 11465db16b..3d4fbb4118 100644 --- a/lms/djangoapps/certificates/views/webview.py +++ b/lms/djangoapps/certificates/views/webview.py @@ -281,7 +281,10 @@ def _update_certificate_context(context, course, user, user_certificate): ) -@handle_500(template_path="certificates/server-error.html") +@handle_500( + template_path="certificates/server-error.html", + test_func=lambda request: request.GET.get('preview', None) +) def render_html_view(request, user_id, course_id): """ This public view generates an HTML representation of the specified student's certificate