def post_like(post_id): """ make a like Return:dict of the recation id """ if not request.get_json(): return make_response(jsonify({'error': 'Not a JSON'}), 400) f = storage.get(Post, post_id) if f is None: abort(404) #<source_user_id>/<target_user_id> mydata = request.get_json() new = Reaction() for k, v in mydata.items(): setattr(new, k, v) new.save() new_notif = Notification() new_notif.reciver_user_id = mydata["target_user_id"] new_notif.maker_user_id = mydata["source_user_id"] new_notif.type = "like" new_notif.reaction_id = str(new.id) new_notif.creation_date = datetime.utcnow() new_notif.save() my_post = storage.get(Post, post_id) new_react_num = my_post.number_of_reaction() rep = {} rep["num"] = new_react_num return make_response(jsonify(rep), 200)
def make_new_follow(follower_id, followed_id, the_secret): """ Make a new follower Return: if not valid user_id or no user found: error code: 8 if already exist: error code: 6 if success: return success code 200 else unknown error (code : 0) """ if request.method == 'POST': if the_secret != my_secret: unauto_dict = {} unauto_dict["error"] = "unauthorised request" unauto_dict["error_code"] = "1" return make_response(jsonify(unauto_dict), 401) response_dict = {} response_dict["error"] = "invalid parameter" response_dict["usage"] = "new_follow/<follower_id>/<followed_id>" response_dict["error_code"] = "8" try: uuid_obj = UUID(follower_id, version=4) uuid_obj = UUID(followed_id, version=4) except ValueError: return make_response(jsonify(response_dict), 202) user_1 = storage.get(User, follower_id) user_2 = storage.get(User, followed_id) if not user_1 or not user_2: return make_response(jsonify(response_dict), 202) if storage.get_by_two(Follow, follower_id, followed_id) is not None: al_exist = {} al_exist["error"] = "operation already exist" al_exist["usage"] = "new_follow/<follower_id>/<followed_id>" al_exist["error_code"] = "6" return make_response(jsonify(al_exist), 202) new_follow = Follow() new_follow.follower_id = follower_id new_follow.user_id = followed_id new_follow.follow_code = 1 new_follow.creation_date = datetime.utcnow() new_follow.save() new_notif = Notification() new_notif.reciver_user_id = followed_id new_notif.maker_user_id = follower_id new_notif.type = "follow" new_notif.follow_id = str(new_follow.id) new_notif.creation_date = datetime.utcnow() new_notif.save() succ = {} succ["status"] = "ok" return make_response(jsonify(succ), 200) unknown_dict = {} unknown_dict["error"] = "unknown error" unknown_dict["error_code"] = "0" return make_response(jsonify(unknown_dict), 404)