From 8a00b76f01519e0f2a2a3bf855ca020d279378a8 Mon Sep 17 00:00:00 2001 From: christopher lee Date: Thu, 18 Jan 2018 13:31:47 -0500 Subject: [PATCH] Disable optimizely for xblocks for the mobile app --- common/djangoapps/student/tests/test_email.py | 2 +- .../mobile_api/context_processor.py | 14 ++++++++ .../tests/test_context_processor.py | 32 +++++++++++++++++++ lms/envs/common.py | 5 ++- lms/templates/widgets/optimizely.html | 2 +- 5 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 lms/djangoapps/mobile_api/context_processor.py create mode 100644 lms/djangoapps/mobile_api/tests/test_context_processor.py diff --git a/common/djangoapps/student/tests/test_email.py b/common/djangoapps/student/tests/test_email.py index ba039ffc77..7f1752777b 100644 --- a/common/djangoapps/student/tests/test_email.py +++ b/common/djangoapps/student/tests/test_email.py @@ -137,7 +137,7 @@ class ActivationEmailTests(TestCase): Registration().register(inactive_user) request = RequestFactory().get(settings.SOCIAL_AUTH_INACTIVE_USER_URL) request.user = inactive_user - with patch('edxmako.request_context.get_current_request'): + with patch('edxmako.request_context.get_current_request', return_value=request): inactive_user_view(request) mock_log.info.assert_called_with( "Activation Email has been sent to User {user_email}".format( diff --git a/lms/djangoapps/mobile_api/context_processor.py b/lms/djangoapps/mobile_api/context_processor.py new file mode 100644 index 0000000000..51de77f10c --- /dev/null +++ b/lms/djangoapps/mobile_api/context_processor.py @@ -0,0 +1,14 @@ +""" +Django template context processors. +""" + +from openedx.core.lib.mobile_utils import is_request_from_mobile_app + + +def is_from_mobile_app(request): # pylint: disable=unused-argument + """ + Configuration context for django templates. + """ + return { + 'is_from_mobile_app': is_request_from_mobile_app(request) + } diff --git a/lms/djangoapps/mobile_api/tests/test_context_processor.py b/lms/djangoapps/mobile_api/tests/test_context_processor.py new file mode 100644 index 0000000000..0426731823 --- /dev/null +++ b/lms/djangoapps/mobile_api/tests/test_context_processor.py @@ -0,0 +1,32 @@ +""" +Tests for Django template context processors. +""" +from django.conf import settings +from django.test import TestCase +from django.test.client import RequestFactory + +from lms.djangoapps.mobile_api.context_processor import is_from_mobile_app + + +class MobileContextProcessorTests(TestCase): + """ + Tests for the configuration context processor. + """ + + def test_is_from_mobile_app(self): + """ + Verify the context is from mobile app. + """ + request = RequestFactory().get('/') + request.META['HTTP_USER_AGENT'] = settings.MOBILE_APP_USER_AGENT_REGEXES[0] + context = is_from_mobile_app(request) + self.assertEqual(context['is_from_mobile_app'], True) + + def test_not_is_from_mobile_app(self): + """ + Verify the context is not from the mobile app. + """ + request = RequestFactory().get('/') + request.META['HTTP_USER_AGENT'] = "Not from the mobile app" + context = is_from_mobile_app(request) + self.assertEqual(context['is_from_mobile_app'], False) diff --git a/lms/envs/common.py b/lms/envs/common.py index f3451bdc50..a454e40247 100644 --- a/lms/envs/common.py +++ b/lms/envs/common.py @@ -595,7 +595,10 @@ CONTEXT_PROCESSORS = [ # Online contextual help 'help_tokens.context_processor', - 'openedx.core.djangoapps.site_configuration.context_processors.configuration_context' + 'openedx.core.djangoapps.site_configuration.context_processors.configuration_context', + + # Mobile App processor (Detects if request is from the mobile app) + 'mobile_api.context_processor.is_from_mobile_app' ] # Django templating diff --git a/lms/templates/widgets/optimizely.html b/lms/templates/widgets/optimizely.html index 2ebe77200a..28986cf32a 100644 --- a/lms/templates/widgets/optimizely.html +++ b/lms/templates/widgets/optimizely.html @@ -1,5 +1,5 @@ <%page expression_filter="h"/> -% if settings.OPTIMIZELY_PROJECT_ID and not disable_optimizely: +% if settings.OPTIMIZELY_PROJECT_ID and not disable_optimizely and not is_from_mobile_app: % endif