Skip to content

Commit

Permalink
Add a CBGV for serving templates
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasw committed Sep 27, 2014
1 parent 8138e1a commit b07896c
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 2 deletions.
4 changes: 2 additions & 2 deletions djplatter/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class TemplateDirectory(object):
def __init__(self, template_dir, config_file_name='config.json',
class ContentCollection(object):
def __init__(self, template_dir, config_file_name='overrides.json',
file_types=('.html',), ignore_prefixes=('_',)):
self.template_dir = template_dir
self.config_file_name = config_file_name
Expand Down
27 changes: 27 additions & 0 deletions djplatter/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from django.shortcuts import render
from django.views.generic import View

from django.http import Http404
from djplatter.models import ContentCollection


class ServeTemplates(View):
template_dir = None

def __init__(self, *args, **kwargs):
assert self.template_dir, (
'Configuration error. Class variable template_dir must be set.')
return super(View, self).__init__(*args, **kwargs)

def get_content_collection(self):
return ContentCollection(self.template_dir)

def dispatch(self, request, *args, **kwargs):
content = self.get_content_collection()

try:
template_path = content.select(request, self.kwargs['path'])
except:
raise Http404("No template found for that path.")

return render(request, template_path)
7 changes: 7 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import os

from django.conf import settings

# Configure django so that our tests work correctly
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.test_settings'
settings._setup()
22 changes: 22 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
DEBUG = True
TEMPLATE_DEBUG = DEBUG

SECRET_KEY = 'fake_secret'

ROOT_URLCONF = 'tests.test_urls'

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'irrelevant.db'
}
}

INSTALLED_APPS = (
)

MEDIA_ROOT = '/media/'
STATIC_ROOT = '/static/'
STATIC_URL = '/'

APPEND_SLASH = False
52 changes: 52 additions & 0 deletions tests/view_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from django.test.client import RequestFactory
from mock import patch
from unittest2 import TestCase

from djplatter.views import ServeTemplates


class ServeTemplatesInstantiation(TestCase):
def setUp(self):
self.bad_st_cbgv = type('TemplateServer', (ServeTemplates,), {})
self.good_st_cbgv = type('TemplateServer', (ServeTemplates,), {
'template_dir': '/my/template/dir/'
})

def test_fails_if_no_template_dir_is_set_on_cbgv(self):
"""fails if no template_dir is set on cbgv"""
self.assertRaises(AssertionError, self.bad_st_cbgv)

def test_succeeds_if_template_dir_is_set(self):
""""succeeds if template_dir is set on cbgv"""
self.assertIsInstance(self.good_st_cbgv(), ServeTemplates)


class ServeTemplatesAsView(TestCase):
def setUp(self):
content_collection_patcher = patch('djplatter.views.ContentCollection')
self.content_collection_mock = content_collection_patcher.start()
self.content_collection = self.content_collection_mock.return_value
self.content_collection.select.return_value = '/a/template.html'
self.addCleanup(content_collection_patcher.stop)

render_patcher = patch('djplatter.views.render')
self.render_mock = render_patcher.start()
self.addCleanup(render_patcher.stop)

self.my_st_cbgv = type('TemplateServer', (ServeTemplates,), {
'template_dir': '/template/dir/'
})
self.view = self.my_st_cbgv.as_view()
self.request = RequestFactory().get('/some/path/')
self.response = self.view(self.request, path='/path/')

def test_instantiates_a_content_collection(self):
self.content_collection_mock.assert_called_once_with('/template/dir/')

def test_selects_a_template_from_a_content_collection(self):
self.content_collection.select.assert_called_once_with(
self.request, '/path/')

def test_renders_the_selected_template(self):
self.render_mock.assert_called_once_with(
self.request, '/a/template.html')

0 comments on commit b07896c

Please sign in to comment.