Author: batiste.bieler
Date: Thu Apr 2 08:40:50 2009
New Revision: 433
Added:
trunk/example/templates/tests/
trunk/example/templates/tests/auto_render.txt
trunk/example/templates/tests/auto_render2.txt
Modified:
trunk/pages/tests.py
trunk/pages/utils.py
Log:
Apply the patch for the auto_render decorator
Added: trunk/example/templates/tests/auto_render.txt
==============================================================================
--- (empty file)
+++ trunk/example/templates/tests/auto_render.txt Thu Apr 2 08:40:50 2009
@@ -0,0 +1 @@
+template_name: '{{template_name}}', only_context: '{{only_context}}'
Added: trunk/example/templates/tests/auto_render2.txt
==============================================================================
--- (empty file)
+++ trunk/example/templates/tests/auto_render2.txt Thu Apr 2 08:40:50 2009
@@ -0,0 +1 @@
+alternate template_name: '{{template_name}}',
only_context: '{{only_context}}'
Modified: trunk/pages/tests.py
==============================================================================
--- trunk/pages/tests.py (original)
+++ trunk/pages/tests.py Thu Apr 2 08:40:50 2009
@@ -3,8 +3,10 @@
from django.test import TestCase
import settings
from pages.models import *
+from pages.utils import auto_render
from django.test.client import Client
from django.template import Template, RequestContext, TemplateDoesNotExist
+from django.http import HttpResponse, HttpResponseRedirect
class PagesTestCase(TestCase):
fixtures = ['tests.json']
@@ -368,4 +370,113 @@
template = Template('{% load pages_tags %}'
'{% get_content page "title" as content %}'
'{{ content }}')
- self.assertEqual(template.render(context), page_data['title'])
\ No newline at end of file
+ self.assertEqual(template.render(context), page_data['title'])
+
+ def test_13_auto_render(self):
+ """
+ Call an @auto_render decorated view with allowed keyword argument
+ combinations.
+ """
+ @auto_render
+ def testview(request, *args, **kwargs):
+ assert 'only_context' not in kwargs
+ assert 'template_name' not in kwargs
+ return 'tests/auto_render.txt', locals()
+ response = testview(None)
+ self.assertEqual(response.__class__, HttpResponse)
+ self.assertEqual(response.content,
+ "template_name: 'tests/auto_render.txt', "
+ "only_context: ''\n")
+ self.assertEqual(testview(None, only_context=True),
+ {'args': (), 'request': None, 'kwargs': {}})
+ response = testview(None, only_context=False)
+ self.assertEqual(response.__class__, HttpResponse)
+ self.assertEqual(response.content,
+ "template_name: 'tests/auto_render.txt', "
+ "only_context: ''\n")
+ response = testview(None, template_name='tests/auto_render2.txt')
+ self.assertEqual(response.__class__, HttpResponse)
+ self.assertEqual(response.content,
+ "alternate
template_name: 'tests/auto_render2.txt', "
+ "only_context: ''\n")
+
+ def test_14_auto_render_httpresponse(self):
+ """
+ Call an @auto_render decorated view which returns an HttpResponse
with
+ allowed keyword argument combinations.
+ """
+ @auto_render
+ def testview(request, *args, **kwargs):
+ assert 'only_context' not in kwargs
+ assert 'template_name' not in kwargs
+ return HttpResponse(repr(sorted(locals().items())))
+ response = testview(None)
+ self.assertEqual(response.__class__, HttpResponse)
+ self.assertEqual(response.content,
+ "[('args', ()), ('kwargs', {}), ('request',
None)]")
+ self.assertOnlyContextException(testview)
+ self.assertEqual(testview(None, only_context=False).__class__,
+ HttpResponse)
+ response = testview(None, template_name='tests/auto_render2.txt')
+ self.assertEqual(response.__class__, HttpResponse)
+ self.assertEqual(response.content,
+ "[('args', ()), ('kwargs', {}), ('request',
None)]")
+
+ def test_15_auto_render_redirect(self):
+ """
+ Call an @auto_render decorated view which returns an
+ HttpResponseRedirect with allowed keyword argument combinations.
+ """
+ @auto_render
+ def testview(request, *args, **kwargs):
+ assert 'only_context' not in kwargs
+ assert 'template_name' not in kwargs
+ return HttpResponseRedirect(repr(sorted(locals().items())))
+ response = testview(None)
+ self.assertEqual(response.__class__, HttpResponseRedirect)
+ self.assertOnlyContextException(testview)
+ self.assertEqual(testview(None, only_context=False).__class__,
+ HttpResponseRedirect)
+ response = testview(None, template_name='tests/auto_render2.txt')
+ self.assertEqual(response.__class__, HttpResponseRedirect)
+
+ def test_16_auto_render_any_httpresponse(self):
+ """
+ Call an @auto_render decorated view which returns an arbitrary
+ HttpResponse subclass with allowed keyword argument combinations.
+ """
+ class MyResponse(HttpResponse): pass
+ @auto_render
+ def testview(request, *args, **kwargs):
+ assert 'only_context' not in kwargs
+ assert 'template_name' not in kwargs
+ return MyResponse(repr(sorted(locals().items())))
+ response = testview(None)
+ self.assertEqual(response.__class__, MyResponse)
+ self.assertOnlyContextException(testview)
+ self.assertEqual(response.content,
+ "[('MyResponse',
<class 'pages.tests.MyResponse'>), "
+ "('args', ()), ('kwargs', {}), ('request',
None)]")
+ self.assertEqual(testview(None, only_context=False).__class__,
+ MyResponse)
+ response = testview(None, template_name='tests/auto_render2.txt')
+ self.assertEqual(response.__class__, MyResponse)
+ self.assertEqual(response.content,
+ "[('MyResponse',
<class 'pages.tests.MyResponse'>), "
+ "('args', ()), ('kwargs', {}), ('request',
None)]")
+
+ def assertOnlyContextException(self, view):
+ """
+ If an @auto_render-decorated view returns an HttpResponse and is
called
+ with ``only_context=True``, it should raise an appropriate
exception.
+ """
+ try:
+ view(None, only_context=True)
+ except Exception, e:
+ self.assertEqual(e.__class__, Exception)
+ self.assertEqual(e.message,
+ "cannot return context dictionary because a
view "
+ "returned an HTTP response when a "
+ "(template_name, context) tuple was expected")
+ else:
+ assert False, 'Exception expected'
Modified: trunk/pages/utils.py
==============================================================================
--- trunk/pages/utils.py (original)
+++ trunk/pages/utils.py Thu Apr 2 08:40:50 2009
@@ -59,32 +59,27 @@
placeholders_recursif(node.get_parent(Context()).nodelist,
plist)
def auto_render(func):
- """Decorator that put automaticaly the template path in the context
dictionary
- and call the render_to_response shortcut"""
+ """
+ A decorator which automatically inserts the template path into the
context
+ and calls the render_to_response shortcut
+ """
def _dec(request, *args, **kwargs):
- t = None
- if kwargs.get('only_context', False):
+ template_override = kwargs.pop('template_name', None)
+ only_context = kwargs.pop('only_context', False)
+ if only_context:
# return only context dictionary
- del(kwargs['only_context'])
response = func(request, *args, **kwargs)
- if isinstance(response, HttpResponse) or isinstance(response,
HttpResponseRedirect) or isinstance(response,
HttpResponsePermanentRedirect):
- raise Except("cannot return context dictionary because a
HttpResponseRedirect as been found")
+ if isinstance(response, HttpResponse):
+ raise Exception("cannot return context dictionary because
a "
+ "view returned an HTTP response when a "
+ "(template_name, context) tuple was
expected")
(template_name, context) = response
return context
- if "template_name" in kwargs:
- t = kwargs['template_name']
- del kwargs['template_name']
response = func(request, *args, **kwargs)
- if isinstance(response, HttpResponse) or isinstance(response,
HttpResponseRedirect) or isinstance(response,
HttpResponsePermanentRedirect):
+ if isinstance(response, HttpResponse):
return response
-
- if response[1].get('http_redirect', False):
- return response[1].get('http_redirect')
-
(template_name, context) = response
- if not t:
- t = template_name
- context['template_name'] = t
+ t = context['template_name'] = template_override or template_name
return render_to_response(t, context,
context_instance=RequestContext(request))
return _dec
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pinax-updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/pinax-updates?hl=en
-~----------~----~----~----~------~----~------~--~---