def new_like(): """ (PUT: like) Instantiates a new <<CheckpointLike>> from a user on a <<UserCheckpoint>> """ #req var user_id = request.form.get("user_id") signature = request.form.get("signature") user_checkpoint_id = request.form.get("user_checkpoint_id") #generated var verb = "put" noun = "like" user = get_user(user_id) user_checkpoint = get_user_checkpoint(user_checkpoint_id) access_token = user.access_token #authorization check if not is_api_key_validated(access_token, user_id, signature, verb, noun): return authorization_fail() like = add_like(user, user_checkpoint) return jsonify({ "status": "ok", "result": { "like_id": like.id, } })
def add_checkpoint_to_catalog(): """ (POST: user_checkpoint) user likes a Checkpoint from a user and wants to add it into his catalog; adds checkpoint to user's catalog """ #req vars user_id = request.form.get("user_id") signature = request.form.get("signature") to_add_user_checkpoint_id = request.form.get("user_checkpoint_id") #generated var verb = "post" noun = "catalog" user = get_user(user_id) if not authorize(verb, noun, user_id, signature): return authorization_fail() user_checkpoint = get_user_checkpoint(to_add_user_checkpoint_id) new_user_cp = add_existing_checkpoint_to_user(user, user_checkpoint) return jsonify({ "status": "ok", "result": { "user_checkpoint_id": new_user_cp.id, }, })
def delete_like(): """ (DELETE: like) Deletes an existing <<CheckpointLike>> between a user and a <<UserCheckpoint>> if it exists """ #req var user_id = request.args.get("user_id") signature = request.args.get("signature") user_checkpoint_id = request.args.get("user_checkpoint_id") #generated var verb = "delete" noun = "like" user = get_user(user_id) user_checkpoint = get_user_checkpoint(user_checkpoint_id) access_token = user.access_token #authorization check if not is_api_key_validated(access_token, user_id, signature, verb, noun): return authorization_fail() delete_like_action(user, user_checkpoint) return jsonify({ "status": "ok", })
def get_checkpoint(): """ (GET: checkpoint) """ type = request.args.get("type") user_id = request.args.get("user_id") signature = request.args.get("signature") if not authorize("get", "checkpoint", user_id, signature): return authorization_fail() if type == "search": user_checkpoints = _search_checkpoints() return jsonify({"checkpoints": user_checkpoint_sanify(user_checkpoints), "status": "ok", }) elif type == "near": friends_ucp, anon_ucp, notifications = _checkpoints_near_me() recent_checkpoints = get_recent_friend_user_checkpoints(get_user(user_id)) lon = request.args.get("longitude") lat = request.args.get("latitude") dic = {"recent_checkpoints": [ucp.serialize for ucp in recent_checkpoints], "friends_checkpoints": user_checkpoint_sanify(friends_ucp), "anon_checkpoints": user_checkpoint_sanify(anon_ucp), "notifications": notification_sanify(notifications), "friends_proximity": checkpoint_proximity(friends_ucp, lat, lon), "anon_proximity": checkpoint_proximity(anon_ucp, lat, lon), "status": "ok", } return jsonify(dic) elif type == "mine": user_checkpoints = _my_checkpoints() dic = {"checkpoints": user_checkpoint_sanify(user_checkpoints), "status": "ok"} _build_proximity(user_checkpoints, dic) return jsonify(dic) else: #single checkpoint info res = _checkpoint_details() return jsonify({ "total_likes": res.total_likes, "total_shares": res.total_shares, "current_user_like": res.current_user_like, "comments": comment_sanify(res.comments), "user": res.user, "checkpoint": res.user_checkpoint_obj.serialize, "status": "ok", })
def get_friends(): user_id = request.args.get("user_id") signature = request.args.get("signature") if not authorize("get", "friends", user_id, signature): return authorization_fail() #generated user_obj = get_user(user_id) update_social_graph(user_obj.access_token, user_obj.facebook_user) friends = get_friends_action(user_obj, exclude_self=True) return jsonify({"status": "ok", "friends": sanify_friends(friends), })
def get_stats(): user_id = request.args.get("user_id") signature = request.args.get("signature") if not authorize("get", "stats", user_id, signature): return authorization_fail() #generated user_obj = get_user(user_id) liked_cp = get_likes_user(user_obj) reshared_cp = get_shares_user(user_obj) return jsonify({"status": "ok", "total_likes": len(liked_cp), "total_reshares": len(reshared_cp), })
def del_user_checkpoint(): """ (DELETE: user_checkpoint) """ #req args user_id = request.args.get("user_id") signature = request.args.get("signature") checkpoint_id = request.args.get("checkpoint_id") if not authorize("delete", "user_checkpoint", user_id, signature): return authorization_fail() #generated vars user_obj = get_user(user_id) checkpoint_obj = get_checkpoint(checkpoint_id) remove_checkpoint_from_user(user_obj, checkpoint_obj) return jsonify({"status": "ok", })
def add_user_checkpoint(): """ (PUT: user_checkpoint) """ #req args user_id = request.form.get("user_id") signature = request.form.get("signature") checkpoint_id = request.form.get("checkpoint_id") if not authorize("put", "user_checkpoint", user_id, signature): return authorization_fail() #generated user_obj = get_user(user_id) checkpoint_obj = get_checkpoint(checkpoint_id) user_checkpoint = add_checkpoint_to_user(user_obj, checkpoint_obj) return jsonify({"status": "ok", "user_checkpoint_id": user_checkpoint.id, })
def new_comment(): """ (PUT: comment) Instantiates a new <<Comment>> from a user on a <<UserCheckpoint>> """ #req var user_id = request.form.get("user_id") signature = request.form.get("signature") user_checkpoint_id = request.form.get("user_checkpoint_id") comment = request.form.get("comment") #generated var verb = "put" noun = "comment" user = get_user(user_id) user_checkpoint = get_user_checkpoint(user_checkpoint_id) #authorization check if not authorize(verb, noun, user_id, signature): return authorization_fail() #comment validation if len(comment) > 255: return jsonify({ "status": "error", "error": "Comment too long", }) comment = add_comment(user, user_checkpoint, comment) return jsonify({ "status": "ok", "result": { "comment_id": comment.id } })
def new_checkpoint(): """ (PUT: checkpoint) *requires authorization creates a barebone checkpoint (just location and image) this checkpoint is not complete yet. """ #req vars user_id = request.form.get("user_id") signature = request.form.get("signature") name = request.form.get("name") longitude = request.form.get("longitude") latitude = request.form.get("latitude") description = request.form.get("description", None) price = request.form.get("price", None) expiry = request.form.get("expiry", None) image_encoded = request.form.get("image", None) type = request.form.get("type") share = request.form.get("share", None) facebook_post = request.form.get("facebook_post", False) image = None if image_encoded is None: image = request.files["image"] #generated vars verb = "put" noun = "checkpoint" user = get_user(user_id) expiry_datetime = None if expiry: expiry_datetime = unserialize_json_datetime(expiry) if not authorize(verb, noun, user_id, signature): print "fail" return authorization_fail() #save image from util.fileupload import save_to_s3 tmp_folder = get_resources_abs_path() if image_encoded is None: img_file_name = save_to_s3(user_id, image, tmp_folder, encoded=False) else: img_file_name = save_to_s3(user_id, image_encoded, tmp_folder) checkpoint = add_checkpoint(user_id, name, type, img_file_name, longitude, latitude, description, price, expiry_datetime, img_location="s3") user_checkpoint = add_checkpoint_to_user(user, checkpoint) #dispatch shares if not share is None: user_ids_to_share = simplejson.loads(share) for uid in user_ids_to_share: user_to = get_user(uid) share_checkpoint(user, user_to, user_checkpoint) #return success return jsonify({ "status": "ok", "result": { "user_checkpoint_id": user_checkpoint.id, }, })