Skip to content

Instantly share code, notes, and snippets.

@Demeter
Forked from code/user_flask_appengine.py
Created September 19, 2011 23:54
Show Gist options
  • Save Demeter/1227945 to your computer and use it in GitHub Desktop.
Save Demeter/1227945 to your computer and use it in GitHub Desktop.
googappapiflaskIDs
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