best option to override login_required decorator

527 views
Skip to first unread message

Robert

unread,
Sep 16, 2009, 4:53:37 AM9/16/09
to Django users
As a business requirement we have to show login form in an ajax
response for some login_required actions. I want to change the
login_required decorator to behaive differently in case the request is
an ajax request and not a normal GET / POST request.

What do you think is the best way to override the login_required
decorator without changing django code ?

For now just changed the django/contrib/auth/decorators.py to add at
the bottom this code:

def __call__(self, request, *args, **kwargs):
if self.test_func(request.user):
return self.view_func(request, *args, **kwargs)
path = urlquote(request.get_full_path())
if request.is_ajax():
return HttpResponse(render_to_string(
settings.LOGIN_TEMPLATE_AJAX,
{self.redirect_field_name: path,},
),mimetype='text/plain')
tup = self.login_url, self.redirect_field_name, path
return HttpResponseRedirect('%s?%s=%s' % tup)


Any suggestion will be welcomed !

thanks in advance, robert

Karen Tracey

unread,
Sep 16, 2009, 9:56:27 AM9/16/09
to django...@googlegroups.com
On Wed, Sep 16, 2009 at 4:53 AM, Robert <robe...@gmail.com> wrote:

As a business requirement we have to show login form in an ajax
response for some login_required actions. I want to change the
login_required decorator to behaive differently in case the request is
an ajax request and not a normal GET / POST request.

What do you think is the best way to override the login_required
decorator without changing django code ?


If Django's login_required doesn't do what you want, create your own decorator that does, and name it something else.  Trying to use the same name as a Django-provided function and make it do something slightly different is bound to lead to confusion at some point.

Karen
Reply all
Reply to author
Forward
0 new messages