From 39fcada1eefd54b8fab6b6cb12b14ed7acdb5e27 Mon Sep 17 00:00:00 2001 From: hernani Date: Tue, 31 Aug 2010 23:59:16 +0000 Subject: [PATCH] Handle redirects and other responses in parameter decorators. git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@582 0cfe37f9-358a-4d5e-be75-b63607b5c754 --- forum/modules/__init__.py | 2 +- forum/modules/decorators.py | 10 +++++++++- forum/views/users.py | 6 +++--- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/forum/modules/__init__.py b/forum/modules/__init__.py index 07cbc6e..a8a5665 100644 --- a/forum/modules/__init__.py +++ b/forum/modules/__init__.py @@ -108,4 +108,4 @@ def module_templates_loader(name, dirs=None): module_templates_loader.is_usable = True -from decorators import decorate +from decorators import decorate, ReturnImediatelyException diff --git a/forum/modules/decorators.py b/forum/modules/decorators.py index 13bddac..3c53412 100644 --- a/forum/modules/decorators.py +++ b/forum/modules/decorators.py @@ -47,7 +47,10 @@ class DecoratableObject(object): def __call__(self, *args, **kwargs): if self._params_decoration: for dec in self._params_decoration: - args, kwargs = dec(*args, **kwargs) + try: + args, kwargs = dec(*args, **kwargs) + except ReturnImediatelyException, e: + return e.ret res = self._callable(*args, **kwargs) @@ -60,6 +63,11 @@ class DecoratableObject(object): return res +class ReturnImediatelyException(Exception): + def __init__(self, ret): + super(Exception, self).__init__() + self.ret = ret + def _check_decoratable(origin, install=True): if not isinstance(origin, DecoratableObject): if inspect.ismethod(origin) and not hasattr(origin, '_decoratable_obj'): diff --git a/forum/views/users.py b/forum/views/users.py index 2e66598..60e4b38 100644 --- a/forum/views/users.py +++ b/forum/views/users.py @@ -15,7 +15,7 @@ from django.utils import simplejson from django.core.urlresolvers import reverse, NoReverseMatch from forum.forms import * from forum.utils.html import sanitize_html -from forum.modules import decorate +from forum.modules import decorate, ReturnImediatelyException from datetime import datetime, date from forum.actions import EditProfileAction, FavoriteAction, BonusRepAction, SuspendAction from forum.modules import ui @@ -259,10 +259,10 @@ def user_view(template, tab_name, tab_title, tab_description, private=False, tab def params(request, id, slug=None): user = get_object_or_404(User, id=id) if private and not (user == request.user or request.user.is_superuser): - return HttpResponseUnauthorized(request) + raise ReturnImediatelyException(HttpResponseUnauthorized(request)) if render_to and (not render_to(user)): - return HttpResponseRedirect(user.get_profile_url()) + raise ReturnImediatelyException(HttpResponseRedirect(user.get_profile_url())) return [request, user], {} -- 2.39.5