Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decorators for requiring authentication via django-twitterauth.
- Loading branch information
Showing
1 changed file
with
23 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from django.http import HttpResponseRedirect | ||
from django.core.urlresolvers import reverse | ||
from twiver.auth.models import User | ||
|
||
def wants_user(f): | ||
def new(*args, **kw): | ||
if 'authed' in args[0].session and args[0].session['authed']: | ||
try: user = User.objects.get(pk=args[0].session['user_id']) | ||
except: user = None | ||
args[0].user = user | ||
else: | ||
args[0].user = None | ||
return f(*args, **kw) | ||
return new | ||
|
||
def needs_user(url): | ||
def decorator(f): | ||
@wants_user | ||
def new(*args, **kw): | ||
if not args[0].user: return HttpResponseRedirect(reverse(url)) | ||
else: return f(*args, **kw) | ||
return new | ||
return decorator |