From c659164d58649b71404ab27fb0d27d6c7fcb2a1b Mon Sep 17 00:00:00 2001 From: Matjaz Gregoric Date: Wed, 12 Aug 2015 09:26:29 +0200 Subject: [PATCH] Make custom themes work on microsites. --- common/djangoapps/edxmako/shortcuts.py | 19 +++++++++---------- common/djangoapps/edxmako/tests.py | 11 ++++++++++- .../microsite_configuration/microsite.py | 5 +---- .../templatetags/microsite.py | 10 ++++++++++ .../tests/test_microsites.py | 12 ++++++++++++ lms/envs/common.py | 3 +++ lms/templates/main.html | 17 +++++++---------- lms/templates/main_django.html | 18 ++++++++++++------ 8 files changed, 64 insertions(+), 31 deletions(-) diff --git a/common/djangoapps/edxmako/shortcuts.py b/common/djangoapps/edxmako/shortcuts.py index 3e584567cf8a..9db2d8d08299 100644 --- a/common/djangoapps/edxmako/shortcuts.py +++ b/common/djangoapps/edxmako/shortcuts.py @@ -81,22 +81,21 @@ def open_source_footer_context_processor(request): """ Checks the site name to determine whether to use the edX.org footer or the Open Source Footer. """ - return dict( - [ - ("IS_EDX_DOMAIN", settings.FEATURES.get('IS_EDX_DOMAIN', False)) - ] - ) + return {'IS_EDX_DOMAIN': settings.FEATURES.get('IS_EDX_DOMAIN', False)} def microsite_footer_context_processor(request): """ Checks the site name to determine whether to use the edX.org footer or the Open Source Footer. """ - return dict( - [ - ("IS_REQUEST_IN_MICROSITE", microsite.is_request_in_microsite()) - ] - ) + return {'IS_REQUEST_IN_MICROSITE': microsite.is_request_in_microsite()} + + +def custom_theme_context_processor(request): # pylint: disable=unused-argument + """ + Sets a boolean context variable to indicate whether the site uses a custom theme. + """ + return {'THEME_ENABLED': settings.FEATURES.get('USE_CUSTOM_THEME', False)} def render_to_string(template_name, dictionary, context=None, namespace='main'): diff --git a/common/djangoapps/edxmako/tests.py b/common/djangoapps/edxmako/tests.py index b77ddfd5efa7..84e2c3b175a6 100644 --- a/common/djangoapps/edxmako/tests.py +++ b/common/djangoapps/edxmako/tests.py @@ -15,7 +15,8 @@ from edxmako.shortcuts import ( marketing_link, render_to_string, - open_source_footer_context_processor + open_source_footer_context_processor, + custom_theme_context_processor ) from student.tests.factories import UserFactory from util.testing import UrlResetMixin @@ -50,6 +51,14 @@ def test_edx_footer(self, expected_result, _): result = open_source_footer_context_processor({}) self.assertEquals(expected_result, result.get('IS_EDX_DOMAIN')) + @ddt.data(True, False) + def test_custom_theme(self, expected_result): + with patch.dict('django.conf.settings.FEATURES', { + 'USE_CUSTOM_THEME': expected_result + }): + result = custom_theme_context_processor({}) + self.assertEquals(expected_result, result.get('THEME_ENABLED')) + class AddLookupTests(TestCase): """ diff --git a/common/djangoapps/microsite_configuration/microsite.py b/common/djangoapps/microsite_configuration/microsite.py index 01759ebe9b90..742c3798cc6b 100644 --- a/common/djangoapps/microsite_configuration/microsite.py +++ b/common/djangoapps/microsite_configuration/microsite.py @@ -72,10 +72,7 @@ def get_template_path(relative_path): search_path = os.path.join(microsite_template_path, relative_path) if os.path.isfile(search_path): - path = '/{0}/templates/{1}'.format( - get_value('microsite_name'), - relative_path - ) + path = os.path.join(get_value('microsite_name'), 'templates', relative_path) return path return relative_path diff --git a/common/djangoapps/microsite_configuration/templatetags/microsite.py b/common/djangoapps/microsite_configuration/templatetags/microsite.py index b349f66a874c..94a109b17f2f 100644 --- a/common/djangoapps/microsite_configuration/templatetags/microsite.py +++ b/common/djangoapps/microsite_configuration/templatetags/microsite.py @@ -62,3 +62,13 @@ def microsite_css_overrides_file(): return "".format(static(file_path)) else: return "" + + +@register.filter(name="microsite_template_path") +def microsite_template_path(relative_path): + """ + Django filter that resolves relative path to a template. The resolved path can either + be in a microsite directory (as an override) or will just return what is passed in. + {% include "some template"|microsite_template_path %} + """ + return microsite.get_template_path(relative_path) diff --git a/common/djangoapps/microsite_configuration/tests/test_microsites.py b/common/djangoapps/microsite_configuration/tests/test_microsites.py index 01cf04aa8a0b..e543851967eb 100644 --- a/common/djangoapps/microsite_configuration/tests/test_microsites.py +++ b/common/djangoapps/microsite_configuration/tests/test_microsites.py @@ -2,6 +2,7 @@ """ Tests microsite_configuration templatetags and helper functions. """ +from mock import patch from django.test import TestCase from django.conf import settings from microsite_configuration.templatetags import microsite @@ -32,3 +33,14 @@ def test_breadcrumb_tag(self): expected = u'my | less specific | Page | edX' title = microsite.page_title_breadcrumbs_tag(None, *crumbs) self.assertEqual(expected, title) + + def test_microsite_template_path(self): + relative_path = 'some_template.html' + resolved_path = 'resolved/path/to/some_template.html' + with patch( + 'microsite_configuration.microsite.get_template_path', + return_value=resolved_path + ) as mock: + result = microsite.microsite_template_path(relative_path) + mock.assert_called_once_with(relative_path) + self.assertEqual(result, resolved_path) diff --git a/lms/envs/common.py b/lms/envs/common.py index efc358446f90..74af5b23394d 100644 --- a/lms/envs/common.py +++ b/lms/envs/common.py @@ -539,6 +539,9 @@ # Allows the open edX footer to be leveraged in Django Templates. 'edxmako.shortcuts.microsite_footer_context_processor', + + # Allows custom theme overrides to be used in Django Templates. + 'edxmako.shortcuts.custom_theme_context_processor', ) # use the ratelimit backend to prevent brute force attacks diff --git a/lms/templates/main.html b/lms/templates/main.html index d06f612e8f96..ce0cfd3f65b5 100644 --- a/lms/templates/main.html +++ b/lms/templates/main.html @@ -86,13 +86,12 @@ <%block name="headextra"/> <% - if theme_enabled() and not is_microsite(): - header_extra_file = 'theme-head-extra.html' - header_file = 'theme-header.html' - google_analytics_file = 'theme-google-analytics.html' - - style_overrides_file = None + style_overrides_file = microsite.get_value('css_overrides_file') + if theme_enabled(): + header_extra_file = microsite.get_template_path('theme-head-extra.html') + header_file = microsite.get_template_path('theme-header.html') + google_analytics_file = microsite.get_template_path('theme-google-analytics.html') else: header_extra_file = None @@ -102,8 +101,6 @@ header_file = microsite.get_template_path('navigation.html') google_analytics_file = microsite.get_template_path('google_analytics.html') - - style_overrides_file = microsite.get_value('css_overrides_file') %> % if header_extra_file: @@ -142,8 +139,8 @@ % if not disable_footer: <%block name="footer"> ## Can be overridden by child templates wanting to hide the footer. - % if theme_enabled() and not is_microsite(): - <%include file="theme-footer.html" /> + % if theme_enabled(): + <%include file="${microsite.get_template_path('theme-footer.html')}" /> % elif settings.FEATURES.get('IS_EDX_DOMAIN', False) and not is_microsite(): <%include file="footer-edx-v3.html" /> % else: diff --git a/lms/templates/main_django.html b/lms/templates/main_django.html index 457dd672ed02..fb7b1f32ac94 100644 --- a/lms/templates/main_django.html +++ b/lms/templates/main_django.html @@ -21,6 +21,10 @@ {% block headextra %}{% endblock %} {% render_block "css" %} + {% if THEME_ENABLED %} + {% include "theme-head-extra.html"|microsite_template_path %} + {% endif %} + {% microsite_css_overrides_file %} @@ -30,22 +34,24 @@
{% trans "Skip to main content" %} {% with course=request.course %} - {% if IS_EDX_DOMAIN %} + {% if THEME_ENABLED %} + {% include "theme-header.html"|microsite_template_path %} + {% elif IS_EDX_DOMAIN and not IS_REQUEST_IN_MICROSITE %} {% include "navigation-edx.html" %} {% else %} - {% include "navigation.html" %} + {% include "navigation.html"|microsite_template_path %} {% endif %} {% endwith %}
{% block body %}{% endblock %} {% block bodyextra %}{% endblock %}
- {% if IS_REQUEST_IN_MICROSITE %} - {# For now we don't support overriden Django templates in microsites. Leave footer blank for now which is better than saying Edx.#} - {% elif IS_EDX_DOMAIN %} + {% if THEME_ENABLED %} + {% include "theme-footer.html"|microsite_template_path %} + {% elif IS_EDX_DOMAIN and not IS_REQUEST_IN_MICROSITE %} {% include "footer-edx-v3.html" %} {% else %} - {% include "footer.html" %} + {% include "footer.html"|microsite_template_path %} {% endif %}