Here's my view. Basically, it returns different Responses based on whether it's logged in or not.
@check_login()
def home(request):
if is_logged_in(request):
return x
else:
return y
Here's my decorator code. I just want to check if the request has headers, and if so, log him in.
#decorator to log the user in if there are headers
def check_login():
def check_dec(func):
if request.META['username'] == "blah":
login(request, user)
return check_dec
The problem is..I don't know how to write a proper decorator in this case!!! What are the arguments? What are the functions? How?
Here is a decorator I wrote to check if the user is in a certain group. It shows a bit more.
https://github.com/mzupan/django-decorators/blob/master/auth.py
You use it like
The user can be in either group1 or group2 if they are not they get a 404 page
A plain decorator is simply a function that takes a function or class and returns something else (usually the same type, but this is not required). A parametrized decorator is a function that returns a decorator.
So, with that in mind we create a closure and return it:
Use only
@check_login
instead ofcheck_login()
- otherwise your decorator has to return a decorate as you are doinghome = check_login()(home)
Here's an example decorator:
This decorator will call execute your login function if the username field is set to "blah" and then call the original method.