fixed get_template_path to work with absolute path also

fixed quality violation

skipped test in CMS

changes after feedback from mattd
This commit is contained in:
Zia Fazal
2016-02-15 12:45:06 +05:00
parent a26d576458
commit 6cd265751a
5 changed files with 58 additions and 3 deletions

View File

@@ -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
@@ -312,7 +312,6 @@ class BaseMicrositeTemplateBackend(object):
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,
@@ -320,6 +319,7 @@ class BaseMicrositeTemplateBackend(object):
'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(
@@ -328,7 +328,7 @@ class BaseMicrositeTemplateBackend(object):
)
return path
else:
return relative_path
return template_path
def get_template(self, uri):
"""

View File

@@ -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(
@@ -114,3 +121,40 @@ class FilebasedMicrositeBackendTests(TestCase):
# 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")

View File

@@ -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')}" />

View File

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

View File

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