public
Last active — forked from code/user_flask_appengine.py

googappapiflaskIDs

  • Download Gist
user_flask_appengine.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
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)
 

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.