Skip to content

Commit

Permalink
Handle redirects and other responses in parameter decorators.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@582 0cfe37f9-358a-4d5e-be75-b63607b5c754
  • Loading branch information
hernani committed Aug 31, 2010
1 parent 8b75f5c commit 39fcada
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion forum/modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 9 additions & 1 deletion forum/modules/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ def _decorate_result(self, fn, needs_params=False):
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)

Expand All @@ -60,6 +63,11 @@ def __call__(self, *args, **kwargs):

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'):
Expand Down
6 changes: 3 additions & 3 deletions forum/views/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
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
Expand Down Expand Up @@ -259,10 +259,10 @@ def decorator(fn):
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], {}

Expand Down

0 comments on commit 39fcada

Please sign in to comment.