-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |