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 put(self, id): args = Arguments(request.json) args.dict("user", required=True) args.validate() current_user = get_jwt_identity() try: id = int(id) except ValueError: return {"message" : "Profiles can only be updated using the ID"}, 400 user = User.get(id=id) if not user or current_user["id"] != id: return {"message" : "You are not authorized to edit this profile"}, 401 if "id" in args.user: del args.user["id"] if "images" in args.user: del args.user["images"] try: args.user["interests"] = args.user["interests"] if args.user["interests"] else "" except Exception: pass try: args.user["preferences"] = args.user["preferences"] if args.user["preferences"] else "" except Exception: pass mail = args.user.get("email", None) if mail and mail != user.email: user.email = mail user.email_verified = False try: validation = Validation(user_id=user.id, code=secrets.token_urlsafe(256)) validation.save() send_validation_email(user, validation.code) except Exception as e: return {"message" : str(e)}, 500 user.update(args.user) try: user.save() return {"message": "User updated"}, 200 except Exception as e: return {"message": str(e)}, 400
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 post(self): current_user = get_jwt_identity() args = Arguments(request.json) args.string("viewee_username") args.validate() user = dict(args) try: viewee = User.get(username=user["viewee_username"]) if viewee: view = View({ "viewee_id": viewee.id, "viewer_id": current_user["id"] }) if View.get(viewer_id=current_user["id"], viewee_id=viewee.id): return {"message": "Already viewed."}, 200 try: view.save() return {"message": "Viewed"}, 200 except Exception as e: return {"message": str(e)}, 500 else: return {"message": "No user found."}, 404 except Exception as e: return {"message": str(e)}, 500
def post(self): current_user = get_jwt_identity() args = Arguments(request.json) args.integer("reported_id") args.string("reason") args.validate() block_request = BlockRequest(dict(args)) block_request.reporter_id = current_user["id"] try: block_request.save() return {"message": "User reported."}, 200 except Exception as e: return {"message": str(e)}, 400
def post(self): args = Arguments(request.json) args.email("email", required=True) args.validate() user = User.get(email=args.email) if user: try: validation = Validation(user_id=user.id, code=secrets.token_urlsafe(256)) validation.save() result = send_password_reset_email(user, validation.code) return {"message": result}, 200 except Exception as e: return {"message": str(e)}, 500 return {"message": "Action complete"}, 200
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 post(self): """ GET /v1/login """ args = Arguments(request.json) args.string("username", required=True) args.string("password", required=True) args.validate() if is_email(args.username): user = User.get(email=args.username) else: user = User.get(username=args.username) if user and not user.email_verified: return {"message": "Account not validated"}, 401 elif user and user.check_password(args.password): identity = { "id": user.id, "username": user.username, "email": user.email } access_token = create_refresh_token(identity=identity) try: user.date_lastseen = datetime.now().strftime( '%Y-%m-%d %H:%M:%S') user.save() except Exception as e: return {"message": str(e)}, 401 return { "access_token": access_token, "user": get_full_user(user.id) }, 200 else: return {"message": "Failed to authenticate."}, 401
def put(self): args = Arguments(request.json) args.string("code") args.string("user_id") args.string("previous_password") args.string("new_password", required=True) args.validate() if args.code != "None": validation = Validation.get(code=args.code) if validation: user = User.get(id=validation.user_id) user.passhash = user.hash_password(args.new_password) user.save() return {"message": "Password updated"}, 200 else: return {"message": "Unauthorised code"}, 401 else: if args.user_id == "" or args.user_id == "None": return {"message": "User Id is required"}, 400 if not args.previous_password: return {"message": "Previous password required"}, 400 user = User.get(id=args.user_id) if user and user.check_password(args.previous_password): user.passhash = user.hash_password(args.new_password) user.save() return {"message": "Password updated"}, 200 else: return {"message": "Your previous password is incorrect"}, 401
def post(self): """ Posting to userlist = Registration """ args = Arguments(request.json) args.email("email", required=True) args.string("username", required=True, min=3, max=255) args.string("password", required=True, max=255) args.string("fname", required=True, min=1, max=255) args.string("lname", required=True, min=1, max=255) # Validate method will abort with 400 if needed args.validate() if User.get(username=args.username): return {"message" : "Username already exists"}, 400 if User.get(email=args.email): return {"message" : "Email address already exists"}, 400 try: new = User(dict(args)) new.save() except Exception as e: return {"message" : str(e)}, 500 user = User.get(username=args.username) # Create validation entry and send email with verify link try: validation = Validation(user_id=user.id, code=secrets.token_urlsafe(256)) validation.save() except Exception as e: return {"message" : str(e)}, 500 send_validation_email(user, validation.code) return user, 200
def put(self): args = Arguments(request.json) args.integer("id") args.boolean("is_primary") args.string("image64", required=True) args.string("image_type", required=True) args.validate() user = get_jwt_identity() data = dict(args) data["user_id"] = user["id"] image = Image.get(id=data.get("id", None), user_id=user["id"]) if image: image.image64 = data["image64"] image.image_type = data["image_type"] image.is_primary = data.get("is_primary", False) else: image = Image(data) try: image.save() return {"message": "Image saved"}, 200 except Exception as e: return {"message": str(e)}, 400