Skip to content

Commit

Permalink
Merge pull request #11548 from edx/ziafazal/fix-get-template-path
Browse files Browse the repository at this point in the history
WL-320: fixed get_template_path to work with absolute path also
  • Loading branch information
ziafazal committed Feb 16, 2016
2 parents cd101d5 + 6cd2657 commit 57af11c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
6 changes: 3 additions & 3 deletions common/djangoapps/microsite_configuration/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ class BaseMicrositeTemplateBackend(object):
configuration of microsite on filesystem.
"""

def get_template_path(self, relative_path, **kwargs):
def get_template_path(self, template_path, **kwargs):
"""
Returns a path (string) to a Mako template, which can either be in
an override or will just return what is passed in which is expected to be a string
Expand All @@ -312,14 +312,14 @@ def get_template_path(self, relative_path, **kwargs):
from microsite_configuration.microsite import get_value as microsite_get_value

microsite_template_path = microsite_get_value('template_dir', None)

if not microsite_template_path:
microsite_template_path = '/'.join([
settings.MICROSITE_ROOT_DIR,
microsite_get_value('microsite_config_key', 'default'),
'templates',
])

relative_path = template_path[1:] if template_path.startswith('/') else template_path
search_path = os.path.join(microsite_template_path, relative_path)
if os.path.isfile(search_path):
path = '/{0}/templates/{1}'.format(
Expand All @@ -328,7 +328,7 @@ def get_template_path(self, relative_path, **kwargs):
)
return path
else:
return relative_path
return template_path

def get_template(self, uri):
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
"""
Test Microsite filebased backends.
"""
import unittest
from mock import patch

from django.test import TestCase
from django.conf import settings
from django.core.urlresolvers import reverse

from microsite_configuration.backends.base import (
BaseMicrositeBackend,
BaseMicrositeTemplateBackend,
)
from microsite_configuration import microsite
from student.tests.factories import CourseEnrollmentFactory, UserFactory
from xmodule.modulestore.tests.factories import CourseFactory
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase


@patch(
Expand Down Expand Up @@ -114,3 +121,40 @@ def test_set_config_by_domain(self):
# if microsite config does not exist default config should be used
microsite.set_by_domain('unknown')
self.assertEqual(microsite.get_value('university'), 'default_university')


@patch(
'microsite_configuration.microsite.TEMPLATES_BACKEND',
microsite.get_backend(
'microsite_configuration.backends.filebased.FilebasedMicrositeTemplateBackend', BaseMicrositeTemplateBackend
)
)
@unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms')
class FilebasedMicrositeTemplateBackendTests(ModuleStoreTestCase):
"""
Go through and test the FilebasedMicrositeTemplateBackend class
"""
def setUp(self):
super(FilebasedMicrositeTemplateBackendTests, self).setUp()
self.microsite_subdomain = 'testmicrosite'
self.course = CourseFactory.create()
self.user = UserFactory.create(username="Bob", email="bob@example.com", password="edx")
self.client.login(username=self.user.username, password="edx")

def test_get_template_path(self):
"""
Tests get template path works for both relative and absolute paths.
"""
microsite.set_by_domain(self.microsite_subdomain)
CourseEnrollmentFactory(
course_id=self.course.id,
user=self.user
)

response = self.client.get(
reverse('syllabus', args=[unicode(self.course.id)]),
HTTP_HOST=settings.MICROSITE_TEST_HOSTNAME,
)

self.assertContains(response, "Microsite relative path template contents")
self.assertContains(response, "Microsite absolute path template contents")
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## mako
<%namespace name='static' file='/static_content.html'/>
<%include file="${static.get_template_path('courseware/test_relative_path.html')}" />
<%include file="${static.get_template_path('/courseware/test_absolute_path.html')}" />

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## mako
<%namespace name='static' file='/static_content.html'/>
<div>Microsite absolute path template contents</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## mako
<%namespace name='static' file='/static_content.html'/>
<div>Microsite relative path template contents</div>

0 comments on commit 57af11c

Please sign in to comment.