Permalink
Browse files

Add TemplateResponseMixin (and TemplateResource)

This provides an easy entry point for using restlib2 as an
alternative to Django's CBVs, but with the benefit of not being
tied down with conventional cruft.
  • Loading branch information...
1 parent d5860e5 commit dd9e4ab6631194d087efc2929cedf31cdc0decfa @bruth committed Jan 2, 2013
Showing with 71 additions and 2 deletions.
  1. +19 −0 restlib2/mixins.py
  2. +5 −0 restlib2/resources.py
  3. +39 −2 restlib2/tests/cases.py
  4. +7 −0 restlib2/tests/settings.py
  5. +1 −0 restlib2/tests/templates/index.html
View
19 restlib2/mixins.py
@@ -0,0 +1,19 @@
+from django.template import loader
+from django.template import RequestContext
+from django.http import HttpResponse
+
+
+class TemplateResponseMixin(object):
+ template_name = None
+ template_string = None
+
+ def render(self, request, context, *args, **kwargs):
+ "Expects the method handler to return the `context` for the template."
+ if isinstance(self.template_name, (list, tuple)):
+ template = loader.select_template(self.template_name)
+ elif self.template_name:
+ template = loader.get_template(self.template_name)
+ else:
+ template = loader.Template(self.template_string)
+ context = RequestContext(request, context)
+ return HttpResponse(template.render(context))
View
5 restlib2/resources.py
@@ -10,6 +10,7 @@
from django.utils.cache import patch_cache_control
from .http import codes, methods
from .serializers import serializers
+from .mixins import TemplateResponseMixin
EPOCH_DATE = datetime(1970, 1, 1, 0, 0, 0)
MAX_CACHE_AGE = 60 * 60 * 24 * 30
@@ -773,3 +774,7 @@ def process_response(self, request, response):
self.set_last_modified(request, response)
return response
+
+
+class TemplateResource(TemplateResponseMixin, Resource):
+ pass
View
41 restlib2/tests/cases.py
@@ -1,11 +1,13 @@
-import unittest
from calendar import timegm
from django.test.client import RequestFactory
+from django.test import TestCase
+from django.conf.urls import patterns, url
from restlib2.resources import Resource
+from restlib2.mixins import TemplateResponseMixin
from restlib2.http import codes
-class ResourceTestCase(unittest.TestCase):
+class ResourceTestCase(TestCase):
def setUp(self):
self.factory = RequestFactory()
@@ -388,3 +390,38 @@ def get(self, request):
response = resource(request)
self.assertEqual(response['Cache-Control'], 'private')
self.assertEqual(response['Expires'], http_date(timegm((datetime.now() + timedelta(hours=1)).utctimetuple())))
+
+
+
+# Defined here to use as root urlconf
+class TestResource(TemplateResponseMixin, Resource):
+ def get(self, request):
+ pass
+
+urlpatterns = patterns('',
+ url(r'^$', TestResource(template_name='index.html'))
+)
+
+class TemplateResourceTestCase(TestCase):
+ def setUp(self):
+ self.factory = RequestFactory()
+
+ def test_string(self):
+ class TemplateResource(TemplateResponseMixin, Resource):
+ template_string = '<h1>Hello World</h1>'
+
+ def get(self, request):
+ pass
+
+ resource = TemplateResource()
+
+ request = self.factory.get('/')
+ response = resource(request)
+
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(response.content, '<h1>Hello World</h1>')
+
+ def test_template(self):
+ response = self.client.get('/')
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(response.content, '<h1>Hello from testserver</h1>\n')
View
7 restlib2/tests/settings.py
@@ -1,4 +1,5 @@
import os
+from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS
SECRET_KEY = '123abc'
@@ -9,9 +10,15 @@
}
}
+ROOT_URLCONF = 'restlib2.tests.cases'
+
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'restlib2',
'restlib2.tests',
)
+
+TEMPLATE_CONTEXT_PROCESSORS += (
+ 'django.core.context_processors.request',
+)
View
1 restlib2/tests/templates/index.html
@@ -0,0 +1 @@
+<h1>Hello from {{ request.get_host }}</h1>

0 comments on commit dd9e4ab

Please sign in to comment.