Author: batiste.bieler
Date: Sun Apr 26 08:13:00 2009
New Revision: 478
Modified:
trunk/pages/tests.py
trunk/pages/utils.py
trunk/pages/views.py
Log:
Create a new exception for the auto_render decorator
Modified: trunk/pages/tests.py
==============================================================================
--- trunk/pages/tests.py (original)
+++ trunk/pages/tests.py Sun Apr 26 08:13:00 2009
@@ -3,7 +3,7 @@
from django.test import TestCase
import settings
from pages.models import *
-from pages.utils import auto_render
+from pages.utils import auto_render, AutoRenderHttpError
from django.test.client import Client
from django.template import Template, RequestContext, TemplateDoesNotExist
from django.http import HttpResponse, HttpResponseRedirect
@@ -567,10 +567,6 @@
try:
view(None, only_context=True)
except Exception, e:
- self.assertEqual(e.__class__, Exception)
- self.assertEqual(e[0],
- "cannot return context dictionary because a
view "
- "returned an HTTP response when a "
- "(template_name, context) tuple was expected")
+ self.assertTrue(isinstance(e, AutoRenderHttpError))
else:
assert False, 'Exception expected'
Modified: trunk/pages/utils.py
==============================================================================
--- trunk/pages/utils.py (original)
+++ trunk/pages/utils.py Sun Apr 26 08:13:00 2009
@@ -71,6 +71,11 @@
if isinstance(node, ExtendsNode):
placeholders_recursif(node.get_parent(Context()).nodelist,
plist)
+class AutoRenderHttpError(Exception):
+ """cannot return context dictionary because a view returned an HTTP
+ response when a (template_name, context) tuple was expected"""
+ pass
+
def auto_render(func):
"""
A decorator which automatically inserts the template path into the
context
@@ -83,9 +88,7 @@
# return only context dictionary
response = func(request, *args, **kwargs)
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")
+ raise AutoRenderHttpError
(template_name, context) = response
return context
response = func(request, *args, **kwargs)
Modified: trunk/pages/views.py
==============================================================================
--- trunk/pages/views.py (original)
+++ trunk/pages/views.py Sun Apr 26 08:13:00 2009
@@ -9,9 +9,24 @@
def details(request, slug=None, lang=None):
"""
- Example view that get the root pages for navigation,
- and the current page if there is any root page.
+ This example view get the root pages for navigation
+ and the current page to display if there is any.
+
All is rendered with the current page's template.
+
+ This view use the auto_render decorator. It means
+ that you can use the only_context extra parameter to get
+ only the local variables of this view without rendering
+ the template.
+
+ >>> from pages.views import details
+ >>> from pages.utils import get_request_mock
+ >>> request = get_request_mock()
+ >>> context = details(request, only_context=True)
+
+ This can be usefull if you want to write your own
+ view and reuse the following code without having to
+ copy and paste it.
"""
pages = Page.objects.navigation().order_by("tree_id")
current_page = False
@@ -32,7 +47,7 @@
lang = get_language_from_request(request, current_page)
if current_page.redirect_to:
- # return this object if you want to active redirections
+ # return this object if you want to activate redirections
http_redirect = HttpResponsePermanentRedirect(
current_page.redirect_to.get_absolute_url(lang))
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---