Esempio n. 1
0
    def put(self):
        current_user = get_jwt_identity()

        user = find_by_user_id(current_user)

        if user is None:
            response = {"success": False, "msg": "User not found."}
            return make_response(jsonify(response)), 404

        # getting JSON data from request
        put_data = request.get_json(silent=True, force=True)

        # If no data is sent at all
        if not put_data:
            response = {
                "success": False,
                "msg": 'Please provide username to update'
            }
            return make_response(jsonify(response)), 400

        # if some data is sent but it doesn't contain username
        try:
            username = put_data.get('username')
            if not username:
                response = {
                    "success": False,
                    "msg": 'username key is not present'
                }
                return make_response(jsonify(response)), 400
        except KeyError as err:
            response = {
                "success": False,
                "msg": f'{str(err)} key is not present'
            }
            return make_response(jsonify(response)), 400

        existing_user = find_by_username(username)

        if existing_user and username != user['username']:
            # There is an existing user with that username. So we can't let the current user use this username
            response = {"success": False, "msg": "Username already taken"}
            return make_response(jsonify(response)), 400

        # Update user with new info
        updated_user = update_by_id(user['id'], {"username": username})

        response = {
            "success": True,
            "msg": "User updated successfully.",
            "body": updated_user
        }
        return make_response(jsonify(response)), 200
Esempio n. 2
0
    def post(self):
        """Handle POST request for this view. Url --> /api/v1/auth/register"""
        # getting JSON data from request
        post_data = request.get_json(silent=True, force=True)

        try:
            name = post_data["name"]
            username = post_data["username"]
            email = post_data["email"]
            password = post_data["password"]
            password2 = post_data["password2"]
        except KeyError as err:
            response = {
                "success": False,
                "msg": f'{str(err)} key is not present'
            }
            return make_response(jsonify(response)), 400

        # Querying the database with requested email
        user = find_by_email(email)

        if user:
            # There is an existing user. We don't want to register users twice
            # Return a msg to the user telling them that they they already
            # exist
            response = {
                "success": False,
                "msg": "Email already exists. Please login."
            }
            return make_response(jsonify(response)), 400

        # Querying the database with requested username
        user = find_by_username(username)

        if user:
            # There is an existing username. We don't want to register users twice
            # Return a msg to the user telling them that the username already
            # exist
            response = {
                "success": False,
                "msg":
                "UserName already exists. Please choose a different one."
            }
            return make_response(jsonify(response)), 400

        # There is no user so we'll try to register them

        # If passwords don't match, return error
        if password != password2:
            response = {
                "success": False,
                "msg": "Both passwords does not match"
            }
            return make_response(jsonify(response)), 400
        """Save the new User."""
        try:
            user = User(email=email,
                        password=password,
                        name=name,
                        username=username)
            user = save(user)
        except Exception as err:
            print("Error occured: ", err)
            response = {"success": False, "msg": "Something went wrong!!"}
            return make_response(jsonify(response)), 500

        response = {
            "success": True,
            "msg": "You registered successfully. Please log in.",
            "body": user
        }

        # return a response notifying the user that they registered
        # successfully
        return make_response(jsonify(response)), 201