Commit 57af11c0 by Zia Fazal

Merge pull request #11548 from edx/ziafazal/fix-get-template-path

WL-320: fixed get_template_path to work with absolute path also
parents cd101d5d 6cd26575
...@@ -303,7 +303,7 @@ class BaseMicrositeTemplateBackend(object): ...@@ -303,7 +303,7 @@ class BaseMicrositeTemplateBackend(object):
configuration of microsite on filesystem. 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 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 an override or will just return what is passed in which is expected to be a string
...@@ -312,7 +312,6 @@ class BaseMicrositeTemplateBackend(object): ...@@ -312,7 +312,6 @@ class BaseMicrositeTemplateBackend(object):
from microsite_configuration.microsite import get_value as microsite_get_value from microsite_configuration.microsite import get_value as microsite_get_value
microsite_template_path = microsite_get_value('template_dir', None) microsite_template_path = microsite_get_value('template_dir', None)
if not microsite_template_path: if not microsite_template_path:
microsite_template_path = '/'.join([ microsite_template_path = '/'.join([
settings.MICROSITE_ROOT_DIR, settings.MICROSITE_ROOT_DIR,
...@@ -320,6 +319,7 @@ class BaseMicrositeTemplateBackend(object): ...@@ -320,6 +319,7 @@ class BaseMicrositeTemplateBackend(object):
'templates', 'templates',
]) ])
relative_path = template_path[1:] if template_path.startswith('/') else template_path
search_path = os.path.join(microsite_template_path, relative_path) search_path = os.path.join(microsite_template_path, relative_path)
if os.path.isfile(search_path): if os.path.isfile(search_path):
path = '/{0}/templates/{1}'.format( path = '/{0}/templates/{1}'.format(
...@@ -328,7 +328,7 @@ class BaseMicrositeTemplateBackend(object): ...@@ -328,7 +328,7 @@ class BaseMicrositeTemplateBackend(object):
) )
return path return path
else: else:
return relative_path return template_path
def get_template(self, uri): def get_template(self, uri):
""" """
......
""" """
Test Microsite filebased backends. Test Microsite filebased backends.
""" """
import unittest
from mock import patch from mock import patch
from django.test import TestCase from django.test import TestCase
from django.conf import settings
from django.core.urlresolvers import reverse
from microsite_configuration.backends.base import ( from microsite_configuration.backends.base import (
BaseMicrositeBackend, BaseMicrositeBackend,
BaseMicrositeTemplateBackend,
) )
from microsite_configuration import microsite 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( @patch(
...@@ -114,3 +121,40 @@ class FilebasedMicrositeBackendTests(TestCase): ...@@ -114,3 +121,40 @@ class FilebasedMicrositeBackendTests(TestCase):
# if microsite config does not exist default config should be used # if microsite config does not exist default config should be used
microsite.set_by_domain('unknown') microsite.set_by_domain('unknown')
self.assertEqual(microsite.get_value('university'), 'default_university') 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")
## 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')}" />
## mako
<%namespace name='static' file='/static_content.html'/>
<div>Microsite absolute path template contents</div>
\ No newline at end of file
## mako
<%namespace name='static' file='/static_content.html'/>
<div>Microsite relative path template contents</div>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment