Authenticate every def in views.py

79 views
Skip to first unread message

suckerfish

unread,
Dec 17, 2010, 9:43:50 PM12/17/10
to Django users
Hi guys

I've added a decorator to *each* def in views.py to require
authentication. Is there a simpler way that allows me to apply
authentication automatically to every def in the file?

Łukasz Rekucki

unread,
Dec 18, 2010, 4:30:18 AM12/18/10
to django...@googlegroups.com

Depends on your code editor ?

--
Łukasz Rekucki

Jonas H.

unread,
Dec 18, 2010, 6:24:31 AM12/18/10
to django...@googlegroups.com

That's not very DRY, is it :-) Try class-based views (Django 1.3/trunk).

If you can't/don't want to use Django 1.3, you could do some automagic
"decorating" at the end of the file:

for view in ['view1, 'view2', ...]:
eval('{0} = your_decorator({0})'.format(view)')

Of couse you should prefer the first way.

Hope this helps :-)

Jonas

James Bennett

unread,
Dec 18, 2010, 7:48:52 AM12/18/10
to django...@googlegroups.com

1. Write a middleware which will force authentication on all your
views (easy), or

2. If you absolutely must have this happen at the view level, stick
some code in the views file which loops over the views defined in the
file and applies the auth decorator to each one.


--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

Łukasz Rekucki

unread,
Dec 18, 2010, 8:21:11 AM12/18/10
to django...@googlegroups.com
On 18 December 2010 12:24, Jonas H. <jo...@lophus.org> wrote:
> On 12/18/2010 10:30 AM, Łukasz Rekucki wrote:
>>
>> On 18 December 2010 03:43, suckerfish<yip...@gmail.com>  wrote:
>>>
>>> Hi guys
>>>
>>> I've added a decorator to *each* def in views.py to require
>>> authentication. Is there a simpler way that allows me to apply
>>> authentication automatically to every def in the file?
>>
>> Depends on your code editor ?
>
> That's not very DRY, is it :-)  Try class-based views (Django 1.3/trunk).

Class-based views won't help much here. Were do you put the
"login_required" decorator. You can make a common base class or a
mixin, but then you have to remember to inherit from that - pretty
much the same amount of work as using a decorator.

>
> If you can't/don't want to use Django 1.3, you could do some automagic
> "decorating" at the end of the file:
>
>    for view in ['view1, 'view2', ...]:
>        eval('{0} = your_decorator({0})'.format(view)')
>

You can always do some "automagic", but as the project grows it and
you introduce more magical decorators, you really start to understand
why "explicit is better the implicit". So I wouldn't recommend that.

--
Łukasz Rekucki

Łukasz Rekucki

unread,
Dec 18, 2010, 8:35:18 AM12/18/10
to django...@googlegroups.com
On 18 December 2010 13:48, James Bennett <ubern...@gmail.com> wrote:
> On Fri, Dec 17, 2010 at 8:43 PM, suckerfish <yip...@gmail.com> wrote:
>> I've added a decorator to *each* def in views.py to require
>> authentication. Is there a simpler way that allows me to apply
>> authentication automatically to every def in the file?
>
> 1. Write a middleware which will force authentication on all your
> views (easy), or

You just have to remember to exclude the login view somehow. This is
troublesome if you want to use a 3rd party app for authentication.
Using decorator like @login_not_required will make you fork it. Second
options is to hardcode excluded views in the middleware. I had this
problem too and decided to bite the bullet for the sake of simplicity.

>
> 2. If you absolutely must have this happen at the view level, stick
> some code in the views file which loops over the views defined in the
> file and applies the auth decorator to each one.
>

Now that's some "automagic" :). IMHO, putting a single line in front
of a view isn't THAT bad and gives important information to the person
reading the code.

--
Łukasz Rekucki

Piotr Zalewa

unread,
Dec 18, 2010, 8:39:48 AM12/18/10
to django...@googlegroups.com
I remember using a middleware with an exception list. (more than a year ago)


--
blog http://piotr.zalewa.info
jobs http://webdev.zalewa.info
twit http://twitter.com/zalun
face http://www.facebook.com/zaloon

Reply all
Reply to author
Forward
0 new messages