Skip to content

Commit

Permalink
Adding support for microsite template_paths on django templates
Browse files Browse the repository at this point in the history
Stripping the leading / for the django_templates finder

Enabling the microsite configurations before running django.setup()

Adding only the templates directory before startup

Adding the missing overrides file at the django templates main

Using the comp_theming way of overriding css

Adding test for the microsite_template_path filter
  • Loading branch information
felipemontoya committed Jan 12, 2016
1 parent 4119576 commit f2a6a27
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,14 @@ def microsite_css_overrides_file():
return "<link href='{}' rel='stylesheet' type='text/css'>".format(static(file_path))
else:
return ""


@register.filter
def microsite_template_path(template_name):
"""
Django template filter to apply template overriding to microsites.
The django_templates loader does not support the leading slash, therefore
it is stripped before returning.
"""
template_name = microsite.get_template_path(template_name)
return template_name[1:] if template_name[0] == '/' else template_name
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,10 @@ 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):
"""
When an unexistent path is passed to the filter, it should return the same path
"""
path = microsite.microsite_template_path('footer.html')
self.assertEqual("footer.html", path)
16 changes: 15 additions & 1 deletion lms/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def run():
if settings.FEATURES.get('ENABLE_THIRD_PARTY_AUTH', False):
enable_third_party_auth()

if settings.FEATURES.get('USE_MICROSITES', False):
enable_microsites_pre_startup()

django.setup()

autostartup()
Expand Down Expand Up @@ -116,6 +119,18 @@ def enable_stanford_theme():
settings.LOCALE_PATHS = (theme_root / 'conf/locale',) + settings.LOCALE_PATHS


def enable_microsites_pre_startup():
"""
The TEMPLATE_ENGINE directory to search for microsite templates
in non-mako templates must be loaded before the django startup
"""
microsites_root = settings.MICROSITE_ROOT_DIR
microsite_config_dict = settings.MICROSITE_CONFIGURATION

if microsite_config_dict:
settings.DEFAULT_TEMPLATE_ENGINE['DIRS'].append(microsites_root)


def enable_microsites():
"""
Enable the use of microsites, which are websites that allow
Expand Down Expand Up @@ -151,7 +166,6 @@ def enable_microsites():

# if we have any valid microsites defined, let's wire in the Mako and STATIC_FILES search paths
if microsite_config_dict:
settings.DEFAULT_TEMPLATE_ENGINE['DIRS'].append(microsites_root)
edxmako.paths.add_lookup('main', microsites_root)

settings.STATICFILES_DIRS.insert(0, microsites_root)
Expand Down
6 changes: 3 additions & 3 deletions lms/templates/main_django.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
{% block headextra %}{% endblock %}
{% render_block "css" %}

{% optional_include "head-extra.html" %}
{% optional_include "head-extra.html"|microsite_template_path %}

<meta name="path_prefix" content="{{EDX_ROOT_URL}}">
</head>
Expand All @@ -28,14 +28,14 @@
<div class="window-wrap" dir="${static.dir_rtl()}">
<a class="nav-skip" href="{% block nav_skip %}#content{% endblock %}">{% trans "Skip to main content" %}</a>
{% with course=request.course %}
{% include "header.html" %}
{% include "header.html"|microsite_template_path %}
{% endwith %}
<div class="content-wrapper" id="content">
{% block body %}{% endblock %}
{% block bodyextra %}{% endblock %}
</div>
{% with course=request.course %}
{% include "footer.html" %}
{% include "footer.html"|microsite_template_path %}
{% endwith %}

</div>
Expand Down

0 comments on commit f2a6a27

Please sign in to comment.