def create_user():
    try:
        data = request.get_json()
        if User.find_by_email(
                data['email']) is not None or User.find_by_username(
                    data['username']) is not None:
            return response_with(resp.INVALID_INPUT_422)
        data['password'] = User.generate_hash(data['password'])
        user_schema = UserSchema()
        user = user_schema.load(data)
        token = generate_verification_token(data['email'])
        verification_email = url_for('user_routes.verify_email',
                                     token=token,
                                     _external=True)
        html = render_template_string(
            "<p>Welcome! Thanks for signing up. Please follow this link to activate your "
            "account:</p> <p><a href='{{ verification_email }}'>{{ verification_email "
            "}}</a></p> <br> <p>Thanks!</p>",
            verification_email=verification_email)
        subject = "Please Verify your email"
        send_email(user.email, subject, html)
        result = user_schema.dump(user.create())
        return response_with(resp.SUCCESS_201, value={'author': result})
    except Exception as e:
        print(e)
        return response_with(resp.INVALID_INPUT_422)
Beispiel #2
0
def create_user():
    try:
        data = request.get_json()
        if User.find_by_username(data['username']) is not None:
            return response_with(resp.INVALID_INPUT_422)
        data['password'] = User.generate_hash(data['password'])
        user_schmea = UserSchema()
        user = user_schmea.load(data)
        if user.role not in ACCESS.keys():  #check if it's a valid role
            return jsonify(
                msg="{} is not a valid role!".format(user.role)), 403

        current_user = User.query.get(get_jwt_identity())

        if user.role == 'root' and current_user.role != 'root':
            return jsonify(msg="{You are not allowed to make a root!"), 403

        # Comment from this line if you don't want use email.

        # token = generate_verification_token(data['email'])
        # verification_email = url_for('user_routes.verify_email', token=token, _external=True)
        # html = render_template_string("<p>Welcome! Thanks for signing up. Please follow this link \
        #                               to activate your account:</p> <p><a href='{{ verification_email }}'\
        #                               >{{ verification_email }}</a></p> <br> <p>Thanks!</p>",\
        #                               verification_email=verification_email)
        # subject = "Please Verify your email"
        # send_email(user.email, subject, html)

        # Comment to this line if you don't want use email.

        user.create()
        return response_with(resp.SUCCESS_201)
    except Exception as e:
        print(e)
        return response_with(resp.INVALID_INPUT_422)
Beispiel #3
0
def create_user():
    try:
        data = request.get_json()
        data['password'] = User.generate_hash(data['password'])
        user_schema = UserSchema()
        user = user_schema.load(data)
        result = user_schema.dump(user.create())
        return response_with(resp.SUCCESS_201)
    except Exception as e:
        return response_with(resp.INVALID_INPUT_422)
Beispiel #4
0
def create_root():
    if User.find_by_id(1):
        return jsonify(msg="there is already a root!"), 403
    data = request.get_json()
    data['password'] = User.generate_hash(data['password'])
    user_schmea = UserSchema()
    user = user_schmea.load(data)
    user.role = 'root'
    user.isVerified = True
    user.create()
    return response_with(resp.SUCCESS_201)
Beispiel #5
0
def create_user():
    try:
        data = request.get_json()
        if (User.find_by_email(data['email']) is not None
                or User.find_by_username(data['username']) is not None):
            return response_with(resp.INVALID_INPUT_422)
        data['password'] = User.generate_hash(data['password'])
        user_schmea = UserSchema()
        user = user_schmea.load(data)
        sendEmailToken(data['email'])
        result = user_schmea.dump(user.create())
        return response_with(resp.SUCCESS_201)

    except Exception as e:
        print(e)
        return response_with(resp.INVALID_INPUT_422)
Beispiel #6
0
def create_user():
    """
    Create user endpoint
    ---
    parameters:
        - in: body
          name: body
          schema:
            id: UserSignup
            required:
                - username
                - password
                - email
            properties:
                username:
                    type: string
                    description: Unique username of the user
                    default: "Johndoe"
                password:
                    type: string
                    description: Password of the user
                    default: "somethingstrong"
            email:
                type: string
                description: email of the user
                default: "*****@*****.**"
            responses:
                    201:
                        description: User successfully created
                        schema:
                        id: UserSignUpSchema
                        properties:
                            code:
                            type: string
                    422:
                        description: Invalid input arguments
                        schema:
                        id: invalidInput
                        properties:
                            code:
                                type: string
                            message:
                                type: string
    """
    try:
        data = request.get_json()
        if (User.find_by_email(data["email"]) is not None
                or User.find_by_username(data["username"]) is not None):
            return response_with(resp.INVALID_FIELD_NAME_SENT_422)
        data["password"] = User.generate_hash(data["password"])
        user_schema = UserSchema()
        user = user_schema.load(data)
        token = generate_verification_token(data["email"])
        verification_email = url_for("user_routes.verify_email",
                                     token=token,
                                     _external=True)
        html = render_template_string(
            "<p>Welcome! Thanks for signing up. Please follow this link to activate your account:</p> <p><a href='{{ verification_email }}'>{{ verification_email }}</a></p> <br> <p>Thanks!</p>",
            verification_email=verification_email,
        )
        subject = "Please Verify your email"
        # send_email(user.email, subject, html)
        result = user_schema.dump(user.create())
        return response_with(resp.SUCCESS_201)

    except Exception as e:
        print(e)
        return response_with(resp.INVALID_FIELD_NAME_SENT_422)