Commit 43cc3d1f by Chris Jerdonek

Finished implementing and testing view.Locator.get_relative_template_location().

parent 008198b5
...@@ -15,12 +15,34 @@ from .renderer import Renderer ...@@ -15,12 +15,34 @@ from .renderer import Renderer
# TODO: rename this class to something else (e.g. ITemplateInfo) # TODO: rename this class to something else (e.g. ITemplateInfo)
class View(object): class View(object):
template_name = None """
template_path = None Subclass this class only if template customizations are needed.
The following attributes allow one to customize/override template
information on a per View basis. A None value means to use default
behavior and perform no customization. All attributes are initially
set to None.
Attributes:
template: the template to use, as a unicode string.
template_path: the path to the template file, relative to the
directory containing the module defining the class.
template_extension: the template file extension. Defaults to "mustache".
Pass False for no extension (i.e. extensionless template files).
"""
template = None template = None
template_encoding = None template_path = None
template_name = None
template_extension = None template_extension = None
template_encoding = None
_renderer = None _renderer = None
locator = TemplateLocator() locator = TemplateLocator()
...@@ -120,7 +142,6 @@ class Locator(object): ...@@ -120,7 +142,6 @@ class Locator(object):
self.search_dirs = search_dirs self.search_dirs = search_dirs
self.template_locator = template_locator self.template_locator = template_locator
# TODO: unit test
def get_relative_template_location(self, view): def get_relative_template_location(self, view):
""" """
Return the relative template path as a (dir, file_name) pair. Return the relative template path as a (dir, file_name) pair.
...@@ -128,10 +149,14 @@ class Locator(object): ...@@ -128,10 +149,14 @@ class Locator(object):
""" """
if view.template_path is not None: if view.template_path is not None:
return os.path.split(view.template_path) return os.path.split(view.template_path)
# Otherwise, we don't know the directory.
template_name = (view.template_name if view.template_name is not None else
self.template_locator.make_template_name(view))
file_name = self.template_locator.make_file_name(template_name, view.template_extension)
# TODO: finish this return (None, file_name)
return (None, None)
def get_template_path(self, view): def get_template_path(self, view):
""" """
...@@ -155,8 +180,10 @@ class Locator(object): ...@@ -155,8 +180,10 @@ class Locator(object):
""" """
if view.template is not None: if view.template is not None:
# TODO: unit test rendering with a non-unicode value for this attribute.
return view.template return view.template
path = self.get_template_path(view) path = self.get_template_path(view)
# TODO: add support for encoding.
return self.reader.read(path) return self.reader.read(path)
...@@ -193,21 +193,54 @@ class LocatorTests(unittest.TestCase, AssertIsMixin): ...@@ -193,21 +193,54 @@ class LocatorTests(unittest.TestCase, AssertIsMixin):
self.assertIs(locator.reader, reader) self.assertIs(locator.reader, reader)
# TODO: make this test real def _assert_template_location(self, view, expected):
def test_get_relative_template_location__template_path__file_name(self):
locator = self._make_locator() locator = self._make_locator()
view = View() actual = locator.get_relative_template_location(view)
self.assertEquals(actual, expected)
view.template_path = 'foo.txt' def test_get_relative_template_location(self):
self.assertEquals(locator.get_relative_template_location(view), ('', 'foo.txt')) """
Test get_relative_template_location(): default behavior (no attributes set).
# TODO: make this test real """
def test_get_relative_template_location__template_path__full_path(self): view = SampleView()
locator = self._make_locator() self._assert_template_location(view, (None, 'sample_view.mustache'))
view = View()
def test_get_relative_template_location__template_path__file_name_only(self):
"""
Test get_relative_template_location(): template_path attribute.
"""
view = SampleView()
view.template_path = 'template.txt'
self._assert_template_location(view, ('', 'template.txt'))
def test_get_relative_template_location__template_path__file_name_with_directory(self):
"""
Test get_relative_template_location(): template_path attribute.
"""
view = SampleView()
view.template_path = 'foo/bar/template.txt'
self._assert_template_location(view, ('foo/bar', 'template.txt'))
view.template_path = 'foo.txt' def test_get_relative_template_location__template_name(self):
self.assertEquals(locator.get_relative_template_location(view), ('', 'foo.txt')) """
Test get_relative_template_location(): template_name attribute.
"""
view = SampleView()
view.template_name = 'new_name'
self._assert_template_location(view, (None, 'new_name.mustache'))
def test_get_relative_template_location__template_extension(self):
"""
Test get_relative_template_location(): template_extension attribute.
"""
view = SampleView()
view.template_extension = 'txt'
self._assert_template_location(view, (None, 'sample_view.txt'))
def test_get_template_path__with_directory(self): def test_get_template_path__with_directory(self):
""" """
......
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