def post(self): """ Doc string to describe function """ args = Arguments(request.json) args.string("matchee_id", required=True) args.validate() user = get_jwt_identity() images = Image.check_images(user_id=user["id"]) if not images["has_images"]: return { "message": "You cannot like a user if you have no profile images.", "no_photo": True }, 401 if Match.get(matchee_id=args.matchee_id, matcher_id=user["id"]): return {"message": "Already liked."}, 200 try: match = Match(matchee_id=args.matchee_id, matcher_id=user["id"]) match.save() except Exception as e: return {"message": str(e)}, 500 return {"message": "User liked."}, 200
def put(self, id): current_user = get_jwt_identity() user = User.get(id=current_user["id"]) if not user.is_admin: return { "message": "You are not authorised to review block requests" }, 401 else: args = Arguments(request.json) args.boolean("blocked") args.string("admin_comments") args.validate() data = dict(args) data["id"] = id block_request = BlockRequest.get(id=data.get("id", None)) if block_request: if block_request.blocked: match = Match.check_match(block_request.reporter_id, block_request.reported_id) if match["liked"] or match["matched"]: my_like = Match.get( matcher_id=block_request.reporter_id, matchee_id=block_request.reported_id) their_like = Match.get( matcher_id=block_request.reported_id, matchee_id=block_request.reporter_id) if match["liked"] and match["matched"]: try: my_like.delete() their_like.delete() except Exception as e: return {"message": str(e)}, 500 elif match["liked"] and not match["matched"]: try: my_like.delete() except Exception as e: return {"message": str(e)}, 500 block_request.reviewed = True block_request.blocked = data["blocked"] block_request.admin_comments = data["admin_comments"] try: block_request.save() msg = "Request reviewed. User blocked." if block_request.blocked == 1 else "Request reviewed. User NOT blocked." return {"message": "{}".format(msg)}, 200 except Exception as e: return {"message": str(e)}, 400 else: return { "messgae": "The block request you are trying to update does not exist" }, 400
def delete(self, user_id): current_user = get_jwt_identity() match = Match.check_match(current_user["id"], user_id) if match["liked"] or match["matched"]: my_like = Match.get(matcher_id=current_user["id"], matchee_id=user_id) their_like = Match.get(matcher_id=user_id, matchee_id=current_user["id"]) if match["liked"] and match["matched"]: try: my_like.delete() their_like.delete() except Exception as e: return {"message": str(e)}, 500 elif match["liked"] and not match["matched"]: try: my_like.delete() except Exception as e: return {"message": str(e)}, 500 try: my_block = { "reporter_id": current_user["id"], "reported_id": user_id, "reason": "unmatch", "reviewed": 1, "blocked": 1, "admin_comments": "unmatch" } their_block = { "reporter_id": user_id, "reported_id": current_user["id"], "reason": "unmatch", "reviewed": 1, "blocked": 1, "admin_comments": "unmatch" } block_them = BlockRequest(my_block) block_me = BlockRequest(their_block) block_me.save() block_them.save() return { "message": "Successfully removed user relationship." }, 200 except Exception as e: return {"message": str(e)}, 500 else: return { "message": "Unable to unlike/unmatch. No relationship exists with this user." }, 200
def like(): like_data = request.get_json() like_instance = Like(liker_id=g.current_user.id, liked_id=like_data['liked_id']) like_instance.save() likers_of_user = Like.query.filter_by( liked_id=g.current_user.id, liker_id=like_data['liked_id']).first() if not likers_of_user: return like_schema.jsonify(like_data) else: match = Match(user_1_id=likers_of_user.liked_id, user_2_id=likers_of_user.liker_id) match.save() return "Match"
def get(self): """ GET : /v1/matches (requires JWT) """ current_user = get_jwt_identity() temp = Match() connection = temp.pool.get_conn() matches = [] with connection.cursor() as c: c.execute( """ SELECT * FROM users WHERE id IN ( SELECT rhs.matcher_id FROM matches lhs LEFT JOIN matches rhs ON lhs.matcher_id = %s WHERE rhs.matchee_id = %s AND lhs.matchee_id = rhs.matcher_id ) """, (current_user["id"], current_user["id"])) for m in c.fetchall(): user = User.get(id=m["id"]) user.get_primary_image() matches.append(user) temp.pool.release(connection) return matches, 200
def get(self, user_id): user = get_jwt_identity() match = Match.get(matcher_id=user["id"], matchee_id=user_id) if match: return match, 200 else: return {"message": "No relationship found with this user."}, 404
def put(self, user_id): user = get_jwt_identity() args = Arguments(request.json) args.integer("rating", required=True, min=1, max=5) args.validate() user = get_jwt_identity() has_match = Match.check_match(user["id"], user_id) if has_match and has_match["matched"]: match = Match.get(matcher_id=user["id"], matchee_id=user_id) try: match.rating = args.rating match.save() return {"message": "Rating successful"}, 200 except Exception as e: return {"message": str(e)}, 500 else: return { "message": "You cannot rate this user, you are not matched." }, 400
def delete(self, user_id): user = get_jwt_identity() match = Match.check_match(user["id"], user_id) return match or {"matched": False, "liked": False}, 200
def get(self): current_user = get_jwt_identity() return Match.get_likes(self, user_id=current_user["id"])