Ejemplo n.º 1
0
def get_post_by_location():
    try:
        # query db for all posts in this area
        lat = (float)(request.args.get('latitude'))
        lon = (float)(request.args.get('longitude'))
    except:
        # Either the params are not there, or they are not convertable to floats
        return generate_error_response(ERR_400, 400)

    radius = 1  # TODO find out what number this should be
    user = retrieve_user_from_request(request)
    if (user is None):
        return generate_error_response(ERR_403, 403)

    try:
        # if time_before is given, return 15 posts before specified time
        # otherwise, return 15 newest posts
        time_before = (float)(request.args.get('time_before'))
        posts = Post.find_limited_posts_within_loc_before_time(\
         lon, lat, radius, time_before)
    except:
        posts = Post.find_limited_posts_within_loc(lon, lat, radius)

    # map from poster_id to img_tags of the poster
    img_tags = {}
    for post in posts:
        poster_id = post.get_poster_id()
        if poster_id not in img_tags:
            img_tags[poster_id] = User.find_user_by_id(poster_id)\
             .get_image_tag()

    user_id = user.get_user_id()
    to_ret = {}
    jsonified_posts = []
    for post in posts:
        # passes in the already loaded image tag
        jsonified_posts.append(json.loads(post.to_json_fields_for_FE(\
         user_id, img_tags[post.get_poster_id()])))
    to_ret['posts'] = jsonified_posts
    return json.dumps(to_ret), 200