Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Dec. 18, 2010 02:46:17

s.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Authenticate every def in views.py


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?

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 18, 2010 09:30:46

Łukasz R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Authenticate every def in views.py


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 ?

--
Łukasz Rekucki

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Dec. 18, 2010 11:24:36

Jonas H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Authenticate every def in views.py


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).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 :
eval('{0} = your_decorator({0})'.format(view)')

Of couse you should prefer the first way.

Hope this helps :-)

Jonas

--
You received this message because you are subscribed to the Google Groups "Django
users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Dec. 18, 2010 12:49:03

James B.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Authenticate every def in views.py


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

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."

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 Dec. 18, 2010 13:21:42

Łukasz R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Authenticate every def in views.py


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 :
>        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

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#6 Dec. 18, 2010 13:35:48

Łukasz R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Authenticate every def in views.py


On 18 December 2010 13:48, James Bennett <ubernost...@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

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#7 Dec. 18, 2010 13:40:01

Piotr Z.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Authenticate every def in views.py


I remember using a middleware with an exception list. (more than a year ago)

On 10-12-18 05:35, Łukasz Rekucki wrote:On 18 December 2010 13:48, James Bennett<ubernost...@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), orYou 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.--
bloghttp://piotr.zalewa.infojobshttp://webdev.zalewa.infotwithttp://twitter.com/zalunfacehttp://www.facebook.com/zaloon--
You received this message because you are subscribed to the Google Groups "Django
users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 19th of August
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support