Exemplo n.º 1
0
def user_login():
    """
    (PUT: user)
    Method to handle when a user authenticates (from Facebook), be it a new user, or recurring user
    """

    fb_code = request.form.get("fb_code", None)
    access_token = request.form.get("access_token", None)
    
    if access_token is None:
        access_token = get_user_access_token(fb_code, APP_ID, APP_SECRET)
    
    fb_user_info, user = save_user(access_token, fb_code)
    api_key = gen_api_key(access_token, user.id)
    
    friends = get_friends(user, exclude_self=True)

    return jsonify({"status": "ok",
                    "result": {
                        "user": {
                                 "id": user.id,
                                 "name": user.facebook_user.name, 
                                 },
                        "friends": sanify_friends(friends),
                        "facebook_user_id": fb_user_info["id"],
                        "facebook_portrait_url": "https://graph.facebook.com/%s/picture" % user.facebook_user.id,
                        "api_key": api_key,
                    }
                    })
Exemplo n.º 2
0
 def test_get_friends(self):
     user_a = self.create_saved_test_user()
     a_friend = self.create_saved_test_user()
     a_friend_2 = self.create_saved_test_user()
     self.befriend_test_user(user_a, [a_friend, a_friend_2])
     
     all_friends = get_friends(user_a.user_obj)
     
     assert len(all_friends) == (len([a_friend, a_friend_2]) + 1)
Exemplo n.º 3
0
def get_nearby_checkpoints(user_obj, point_coord, radius_in_kilometres):
    """
    get UserCheckpoints in a given radius sorted according to proximity
    returns in tuple, (friends_user_checkpoints_list, anonymous_user_checkpoints_list)
    """
    from db import UserCheckpoint, db, Checkpoint
    
    #bounding box
    lat, lon = point_coord[0], point_coord[1]
    
    dlat, dlon = bounding_box(lat, lon, radius_in_kilometres)
    min_lat, max_lat = lat-dlat, lat+dlat
    min_lon, max_lon = lon-dlon, lon+dlon
    
    radius_cond = and_(Checkpoint.latitude <= max_lat,
                       Checkpoint.latitude >= min_lat,
                       Checkpoint.longitude <= max_lon,
                       Checkpoint.longitude >= min_lon
                       )
    
    ucp_in_radius = (db.session.query(UserCheckpoint).
                     join(UserCheckpoint.checkpoint).
                     filter(radius_cond).
                     filter(Checkpoint.demo == False)
                     )
    
    #removing dupes and making sure that the oldest creator always gets credited
    unduped_ucp = {}
    all_ucp = ucp_in_radius.all()
    for ucp in all_ucp:
        key = ucp.checkpoint.id 
        if key in unduped_ucp:
            compared_ucp = unduped_ucp[key]
            if ucp.user_id == user_obj.id:
                unduped_ucp[key] = ucp
            elif ucp < compared_ucp and compared_ucp.user_id != user_obj.id: #older
                unduped_ucp[key] = ucp
        else: unduped_ucp[key] = ucp
    all_ucp = [v for k,v in unduped_ucp.iteritems()]
    
    ucp_namedtuples = _checkpoints_to_location_namedtuples(all_ucp)
    sorted_ucp = proximity_sort((lat, lon), ucp_namedtuples, ucp_in_radius.count())
    
    #separate into friends and anon ucp
    friend_list = get_friends(user_obj)
    friends = []
    anon = []
    for ucp in sorted_ucp:
        if ucp.user_checkpoint.user in friend_list:
            friends += [ucp.user_checkpoint]
        else:
            anon += [ucp.user_checkpoint]
    
    return friends, anon