0

I want to ask that:

  1. What is the purpose of @login_required in below piece of code?

    (Yes, "@login_required" is defined in another .py file)

  2. How this piece of code actually works? Does it means whenever class TaskCreateHandler will be used the function login_required will called?

  3. What if we write @login_required inside def post(self)?

Thanks.

class TaskCreateHandler(BaseHandler):
@login_required
def post(self):
Md. Rezwanul Haque
  • 2,882
  • 7
  • 28
  • 45
Muhammad Hamid Raza
  • 185
  • 2
  • 3
  • 13

3 Answers3

1

I suppose you are using Flask (with flask-login) or something like that.

1) Purpose of @login_required is to assure, that only user with valid session is able to access the resource. If you do not have a valid session flask-login will return 401 (Authorization required) and you cannot access the resource. See https://flask-login.readthedocs.io/en/latest/#login-example.

2) As stated by Mohamed, login_required is a decorator. It will wrap-up your function (post), which means that whenever someone calls TaskCreateHandler.post, the login_required is called first, it does something (like checking for valid session) and then possibly calling your function. Oncae your function returns, the control is handled back to @login required, which will probably just return result from post. See https://wiki.python.org/moin/PythonDecorators.

3) Will not work, as there is nothing to decorate...

Karel Jakubec
  • 342
  • 2
  • 13
  • Thanks alot for this detailed Answer Karel. Can you please elaborate "Oncae your function returns, the control is handled back to @login required, which will probably just return result from post." Do you mean after function returns control is handled back to decorator? – Muhammad Hamid Raza Aug 01 '17 at 12:49
  • Yes, thats exactly what happens. Think about decorator really as just a wrapper. Basically it is a function, which has another function (the function which is being decorated, in your example the post) as its first argument. And it is up to decorator what it does. It may run some code, then call your function and after your function returns, it can transform the result in some way and then return it. Or it may check some external properties and depending on them call or not call your function (like @login required does). Or it may just log start and end time of your function... – Karel Jakubec Aug 01 '17 at 13:25
0

I think that's a decorator that would verify if the user is logged in before calling the method. For more info please check this nice blog post.

HTH

Alberto
  • 446
  • 5
  • 14
  • Thanks. ;-) If i write @login_required inside a function, then it means that function will check if user is logged in after function calling. right? – Muhammad Hamid Raza Aug 01 '17 at 12:43
0

What you have here is a decorator. The syntax is wholly equivalent to:

def post: #code
post = login_required(post)

A Python decorator is just a regular function. It takes another function as an argument and does with it... anything really. You could define:

def add12(func): return 1+2

and use it as a decorator. Then if you decorated post by adding @add12 above it, any call to post() would return 3 no matter what you passed into it. Look at the equivalent syntax snippet above again if it's not clear why.

If you wrote it inside the def it wouldn't do much of use here. I'm not 100% sure how it would be treated; I suspect it'll throw a SyntaxError.

jkm
  • 46
  • 1