Exemplo n.º 1
0
def create_users():
    user1 = User(email="*****@*****.**",
            username='******',
            password=User.generate_hash('helloworld'),
            isVerified=True).create()
    user2 = User(email="*****@*****.**",
            username='******',
            password=User.generate_hash('helloworld')).create()
Exemplo n.º 2
0
def create_users():
    user1 = User(username='******', email="*****@*****.**", \
                 password=User.generate_hash('hello'), isVerified=True).create()

    user2 = User(username='******', email="*****@*****.**", \
                 password=User.generate_hash('hello')).create()

    user3 = User(username='******', email= "*****@*****.**", \
                 password=User.generate_hash('hello'), isVerified=True, role ="admin").create()
Exemplo n.º 3
0
def create_user():
    user1 = User(
        username='******',
        email='*****@*****.**',
        password=User.generate_hash('nghi!abc123'),
        is_verified=True,
    ).create()

    user2 = User(
        username='******',
        email='*****@*****.**',
        password=User.generate_hash('nghi!abc123'),
        is_verified=False,
    ).create()
Exemplo n.º 4
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_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)
Exemplo n.º 5
0
def create_user():
    # try:
    data = request.get_json()
    data['password'] = User.generate_hash(data['password'])
    user = user_schema.load(data)
    result = user_schema.dump(user.create())
    return response_with(resp.SUCCESS_201)
Exemplo n.º 6
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)
Exemplo n.º 7
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)
def create_user_in_db(db: Session, user: UserCreate):
    db_user = User(
        email=user.email,
        password=User.generate_hash(user.password),
        username=user.email,
        role=user.role,
    )
    db.add(db_user)
    db.commit()
    db.refresh(db_user)
    return db_user
Exemplo n.º 9
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)
Exemplo n.º 10
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)
Exemplo n.º 11
0
def update_user(user_id):
    try:
        data = request.get_json()
        get_user = User.query.get_or_404(user_id)
        current_user = User.query.get(get_jwt_identity())

        if ('root' in data) and current_user.role != 'root':
            return jsonify(msg="You cannot make a root!"), 403

        for field in data:  #modify only the attributes asked
            setattr(get_user, field, data[field])

        if "password" in data:  #use the right password
            get_user.password = User.generate_hash(data['password'])

        if get_user.role not in ACCESS.keys():
            return jsonify(
                msg="{} is not a valid role!".format(get_user.role)), 403

        # Comment from this line if you don't want use email.
        # if "email" in data:
        #     get_user.isVerified = False
        #     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(get_user.email, subject, html)
        # Comment to this line if you don't want use email.

        db.session.add(get_user)
        db.session.commit()
        user_schema = UserSchema(only=['id', 'username', 'email', 'role'])
        user = user_schema.dump(get_user)
        return response_with(resp.SUCCESS_200, value={"user": user})

    except Exception as e:
        print(e)
        return response_with(resp.INVALID_INPUT_422)
Exemplo n.º 12
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)