def _get_all_world_schools(): dbsession = DBSession() schools = dbsession.query(WorldSchools.rank, WorldSchools.name, WorldSchools.short_name).all() names = [s.name for s in schools] short_names = [s.short_name for s in schools] return dict(names=names, short_names=short_names)
def _get_all_national_schools(): dbsession = DBSession() schools = dbsession.query(NationalSchools.rank, NationalSchools.name, NationalSchools.short_name).all() names = [s.name for s in schools] short_names = [s.short_name for s in schools] return dict(names=names, short_names=short_names)
def _get_world_rank(): dbsession = DBSession() world_top = dbsession.query(Friend).order_by(desc(Friend.score)).\ limit(100).all() world_top_users = dbsession.query(User).order_by(desc(User.score)).\ limit(100).all() world_top.extend(world_top_users) return sorted(world_top, key=lambda k: k.score, reverse=True)
def _get_world_rank(): dbsession = DBSession() world_top = dbsession.query(Friend).order_by(desc(Friend.score)).\ limit(100).all() world_top_users = dbsession.query(User).order_by(desc(User.score)).\ limit(100).all() world_top.extend(world_top_users) return sorted(world_top, key=lambda k:k.score, reverse=True)
def _initTestingDB(): from sqlalchemy import create_engine from lifescore.models import DBSession, Base engine = create_engine('sqlite://') session = DBSession() session.configure(bind=engine) Base.metadata.bind = engine Base.metadata.create_all(engine) return session
def dashboard(request): dbsession = DBSession() settings = request.registry.settings cookie = facebook.get_user_from_cookie(request.cookies, settings['facebook.app.id'], settings['facebook.app.secret']) if cookie: graph = _get_graph(cookie['access_token']) profile = graph.get_object('me') fb_id = profile['id'] user = dbsession.query(User).filter(User.fb_id==fb_id).first() if not user: user = User(fb_id, cookie['access_token'], profile['updated_time'], profile['name'], 'gender' in profile and profile['gender'] or None, 'location' in profile and profile['location']['name'] or None, _get_lifescore(profile)) dbsession.add(user) dbsession.commit() return dict(profile=profile, score=user.score, friends_id=_get_friends_id(graph).encode('ascii','ignore'), world_rank=_get_world_rank()[0:20]) elif user.fb_access_token != cookie['access_token']: user.fb_access_token = cookie['access_token'] if user.fb_updated_time != profile['update_time']: user.score = _get_lifescore(profile) dbsession.merge(user) dbsession.commit() try: debug = request.GET['debug'] return dict(profile=profile, score=user.score, friends_id=_get_friends_id(graph).encode('ascii','ignore'), world_rank=_get_world_rank()[0:20]) except KeyError: pass return dict(profile=profile, score=user.score, friends_rank=_get_friends(user)[0:20], world_rank=_get_world_rank()[0:20]) else: return HTTPFound(location=route_url('home', request))
def home(request): dbsession = DBSession() settings = request.registry.settings fb_perms = [ 'user_education_history', 'friends_education_history', 'publish_stream', 'offline_access', 'user_relationships', 'friends_relationships', 'user_work_history', 'friends_work_history', 'user_location', 'friends_location' ] cookie = facebook.get_user_from_cookie(request.cookies, settings['facebook.app.id'], settings['facebook.app.secret']) if cookie: graph = facebook.GraphAPI(cookie['access_token']) profile = graph.get_object('me') dashboard_url = route_url('dashboard', request, fb_id=profile['id']) request.response_cookies = request.cookies return dict(dashboard_url=dashboard_url, facebook_app_id=settings['facebook.app.id']) else: return dict(facebook_app_id=settings['facebook.app.id'], facebook_perms=','.join(fb_perms))
from celery.task import task from celery.loaders.default import Loader from sqlalchemy import create_engine from lifescore.models import DBSession, Base, Friend loader = Loader() engine = create_engine(loader.read_configuration()['CELERY_RESULT_DBURI']) dbsession = DBSession() dbsession.configure(bind=engine) Base.metadata.bind = engine @task def save_friends(friends, scores, user): for i in range(len(friends)): friend = Friend(friends[i]['id'], friends[i]['name'], 'gender' in friends[i] and friends[i]['gender'] or None, 'location' in friends[i] and friends[i]['location']['name'] or None, scores[i]['score']) friend.user_id = user.id dbsession.add(friend) dbsession.commit()
def _get_jobs(): dbsession = DBSession() jobs = dbsession.query(Job.prestige, Job.job).all() return dict([(job.job.lower(), job.prestige) for job in jobs])
def _get_all_majors(): dbsession = DBSession() majors = dbsession.query(Major.name).order_by( desc(Major.avg_starting_salary)).limit(40) return dict([(m.name, 1) for m in majors])
def _get_user_from_fb_id(fb_id): dbsession = DBSession() return dbsession.query(User).filter(User.fb_id == fb_id).first()
def _get_friends(user): dbsession = DBSession() return dbsession.query(Friend).filter(Friend.user_id==user.id).\ order_by(desc(Friend.score)).all()
def _get_user_from_fb_id(fb_id): dbsession = DBSession() return dbsession.query(User).filter(User.fb_id==fb_id).first()
def dashboard(request): dbsession = DBSession() settings = request.registry.settings cookie = facebook.get_user_from_cookie(request.cookies, settings['facebook.app.id'], settings['facebook.app.secret']) if cookie: graph = _get_graph(cookie['access_token']) profile = graph.get_object('me') fb_id = profile['id'] user = dbsession.query(User).filter(User.fb_id == fb_id).first() if not user: user = User( fb_id, cookie['access_token'], profile['updated_time'], profile['name'], 'gender' in profile and profile['gender'] or None, 'location' in profile and profile['location']['name'] or None, _get_lifescore(profile)) dbsession.add(user) dbsession.commit() return dict(profile=profile, score=user.score, friends_id=_get_friends_id(graph).encode( 'ascii', 'ignore'), world_rank=_get_world_rank()[0:20]) elif user.fb_access_token != cookie['access_token']: user.fb_access_token = cookie['access_token'] if user.fb_updated_time != profile['update_time']: user.score = _get_lifescore(profile) dbsession.merge(user) dbsession.commit() try: debug = request.GET['debug'] return dict(profile=profile, score=user.score, friends_id=_get_friends_id(graph).encode( 'ascii', 'ignore'), world_rank=_get_world_rank()[0:20]) except KeyError: pass return dict(profile=profile, score=user.score, friends_rank=_get_friends(user)[0:20], world_rank=_get_world_rank()[0:20]) else: return HTTPFound(location=route_url('home', request))
def _get_top_companies(): dbsession = DBSession() companies = dict([(c.name, 1) for c in dbsession.query(Company.name).all()]) return companies
from celery.task import task from celery.loaders.default import Loader from sqlalchemy import create_engine from lifescore.models import DBSession, Base, Friend loader = Loader() engine = create_engine(loader.read_configuration()['CELERY_RESULT_DBURI']) dbsession = DBSession() dbsession.configure(bind=engine) Base.metadata.bind = engine @task def save_friends(friends, scores, user): for i in range(len(friends)): friend = Friend( friends[i]['id'], friends[i]['name'], 'gender' in friends[i] and friends[i]['gender'] or None, 'location' in friends[i] and friends[i]['location']['name'] or None, scores[i]['score']) friend.user_id = user.id dbsession.add(friend) dbsession.commit()