Created
November 30, 2012 21:42
-
-
Save treystout/4178879 to your computer and use it in GitHub Desktop.
Forcing login
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
diff -r ffa320b5e178 web/site/auth.py | |
--- a/web/site/auth.py Thu Nov 29 15:47:01 2012 -0500 | |
+++ b/web/site/auth.py Fri Nov 30 16:41:17 2012 -0500 | |
@@ -5,12 +5,11 @@ | |
Date Added: Fri Mar 23 20:08:15 PDT 2012 | |
""" | |
import logging | |
+from functools import partial | |
-from flask import request as R, render_template as T, redirect, flash, \ | |
- session, url_for | |
+from flask import request as R, render_template as T, redirect, url_for, g | |
from web.outland_app import app, login_manager | |
-from flaskext.login import (UserMixin, current_user, login_user, logout_user, | |
- current_user) | |
+from flaskext.login import (UserMixin, current_user, logout_user, current_user) | |
from lib.akn.exceptions import NotFound | |
from models import user | |
@@ -84,3 +83,26 @@ | |
def login_templates(): | |
token = user.get_signup_token() | |
return T('/js_templates/emailRegistration.j2', signup_token=token) | |
+ | |
+def protected(login_via): | |
+ print login_via, "inside protected decorator" | |
+ def wrapper(func): | |
+ print login_via, "inside wrapper" | |
+ def new_func(login_via, *args, **kwargs): | |
+ print login_via, "during route call" | |
+ if not current_user.is_authenticated(): | |
+ g.needs_login = True | |
+ if login_via is None: | |
+ login_via = url_for('index') | |
+ else: | |
+ login_via = url_for(login_via, needs_login=True, **kwargs) | |
+ log.info("setting login_via to %s", login_via) | |
+ return redirect(login_via) | |
+ new_func.func_name = func.func_name | |
+ new_func.func_doc = func.func_doc | |
+ new_func.__dict__ = func.__dict__ | |
+ return func(*args, **kwargs) | |
+ new_func = partial(new_func, login_via) | |
+ new_func.__name__ = func.func_name | |
+ return new_func | |
+ return wrapper | |
diff -r ffa320b5e178 web/site/jinja_templates/base.j2 | |
--- a/web/site/jinja_templates/base.j2 Thu Nov 29 15:47:01 2012 -0500 | |
+++ b/web/site/jinja_templates/base.j2 Fri Nov 30 16:41:17 2012 -0500 | |
@@ -49,4 +49,8 @@ | |
kp.views.auth.main.showHardRoadblock(); | |
{% endif %} | |
+ {% if g.needs_login %} | |
+ alert("login!"); | |
+ {% endif %} | |
+ | |
{% endblock %} | |
diff -r ffa320b5e178 web/site/user.py | |
--- a/web/site/user.py Thu Nov 29 15:47:01 2012 -0500 | |
+++ b/web/site/user.py Fri Nov 30 16:41:17 2012 -0500 | |
@@ -13,7 +13,7 @@ | |
validators, ValidationError) | |
from web.outland_app import app | |
-from web.site.auth import FlaskLoginUser | |
+from web.site.auth import FlaskLoginUser, protected | |
from web.site.facebook import create_collection_timeline, create_follow_timeline | |
from lib.akn.config import settings | |
from lib.akn import mixpanel | |
@@ -251,7 +251,7 @@ | |
return redirect(R.referrer) | |
@app.route('/u/<username>/follow') | |
-@login_required | |
+@protected('user_profile') | |
def follow_user(username): | |
user_info = user.get_by_username(username, True) | |
user.follow_user(current_user.user_id, user_info.user_id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment