Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
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)
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
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)
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
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))
Ejemplo n.º 9
0
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))
Ejemplo n.º 10
0
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()
Ejemplo n.º 11
0
def _get_jobs():
    dbsession = DBSession()
    jobs = dbsession.query(Job.prestige, Job.job).all()
    return dict([(job.job.lower(), job.prestige) for job in jobs])
Ejemplo n.º 12
0
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])
Ejemplo n.º 13
0
def _get_user_from_fb_id(fb_id):
    dbsession = DBSession()
    return dbsession.query(User).filter(User.fb_id == fb_id).first()
Ejemplo n.º 14
0
def _get_friends(user):
    dbsession = DBSession()
    return dbsession.query(Friend).filter(Friend.user_id==user.id).\
            order_by(desc(Friend.score)).all()
Ejemplo n.º 15
0
def _get_friends(user):
    dbsession = DBSession()
    return dbsession.query(Friend).filter(Friend.user_id==user.id).\
            order_by(desc(Friend.score)).all()
Ejemplo n.º 16
0
def _get_user_from_fb_id(fb_id):
    dbsession = DBSession()
    return dbsession.query(User).filter(User.fb_id==fb_id).first()
Ejemplo n.º 17
0
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))
Ejemplo n.º 18
0
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])
Ejemplo n.º 19
0
def _get_top_companies():
    dbsession = DBSession()
    
    companies = dict([(c.name, 1) 
                      for c in dbsession.query(Company.name).all()])
    return companies
Ejemplo n.º 20
0
def _get_jobs():
    dbsession = DBSession()
    jobs = dbsession.query(Job.prestige, Job.job).all()
    return dict([(job.job.lower(), job.prestige) for job in jobs])
Ejemplo n.º 21
0
def _get_top_companies():
    dbsession = DBSession()

    companies = dict([(c.name, 1)
                      for c in dbsession.query(Company.name).all()])
    return companies
Ejemplo n.º 22
0
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()