Created
March 30, 2011 16:53
-
-
Save code/894791 to your computer and use it in GitHub Desktop.
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
from flask import g, session | |
from google.appengine.api import users | |
class GuestUser(object): | |
def email(self): | |
return None | |
def nickname(self): | |
return 'Guest' | |
def is_admin(self): | |
return False | |
def user_type(self): | |
return 'guest' | |
class GoogleUser(object): | |
def __init__(self, google_user): | |
self.google_user = google_user | |
def email(self): | |
return self.google_user.email() | |
def nickname(self): | |
return self.google_user.nickname() | |
def is_admin(self): | |
return users.is_current_user_admin() | |
def user_type(self): | |
return 'google' | |
class TwitterUser(object): | |
# add these to twitter user. | |
def email(self): | |
return None | |
def nickname(self): | |
return self.screen_name | |
def is_admin(self): | |
authorized_users = ['maxart', 'lukeinth'] | |
return self.screen_name in authorized_users | |
def user_type(self): | |
return 'twitter' | |
def get_current_user(): | |
user_id = session.get('user_id') | |
user = None | |
if user_id: | |
user = TwitterUser.get_by_user_id(session['user_id']) | |
else: | |
user = users.get_current_user() | |
if user: | |
user = GoogleUser(user) | |
if not user: | |
user = GuestUser() | |
return user | |
@app.before_request | |
def add_user_to_globals(): | |
g.user = get_current_user() | |
@app.route('/some/path') | |
def some_path(): | |
user = g.user | |
if user.is_admin(): | |
return render_template('admin.html', user=user) | |
else: | |
return render_template('normal.html', user=user) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment