Exemplo n.º 1
0
def create():
    """
    Create User Function
    """
    req_data = request.get_json()
    data = user_schema.load(req_data)
    error = None
    if error:
        return custom_response(error, HTTPStatus.BAD_REQUEST)

    # check if user already exist in the db
    user_in_db = UserModel.get_user_by_email(data.get('email'))
    if user_in_db:
        message = {
            'error': 'User already exist, please supply another email address'
        }
        return custom_response(message, HTTPStatus.BAD_REQUES)

    user = UserModel(data)
    user.save()

    ser_data = user_schema.dump(user)
    token = Auth.generate_token(ser_data.get('id'))
    print(type(token))
    if type(token) is not str:
        return token
    return custom_response({'jwt_token': json.dumps(token)}, HTTPStatus.OK)
Exemplo n.º 2
0
        def decorated_auth(*args, **kwargs):
            if 'api-token' not in request.headers:
                return Response(
                    mimetype="application/json",
                    response=json.dumps({
                        'error':
                        'Authentication token is not available, please login to get one'
                    }),
                    status=400)
            token = request.headers.get('api-token')
            data = Auth.decode_token(token)
            if data['error']:
                return Response(mimetype="application/json",
                                response=json.dumps(data['error']),
                                status=400)

            user_id = data['data']['user_id']
            check_user = UserModel.get_one_user(user_id)
            if not check_user:
                return Response(mimetype="application/json",
                                response=json.dumps({
                                    'error':
                                    'user does not exist, invalid token'
                                }),
                                status=400)
            g.user = {'id': user_id}
            return func(*args, **kwargs)
Exemplo n.º 3
0
def get_me():
    """
    Get me
    """
    user = UserModel.get_one_user(g.user.get('id'))
    ser_user = user_schema.dump(user).data
    return custom_response(ser_user, HTTPStatus.OK)
Exemplo n.º 4
0
def delete():
    """
    Delete a user
    """
    user = UserModel.get_one_user(g.user.get('id'))
    user.delete()
    return custom_response({'message': 'deleted'}, HTTPStatus.NO_CONTENT)
Exemplo n.º 5
0
def get_all():
    """
    Get all users
    """
    users = UserModel.get_all_users()
    ser_users = user_schema.dump(users, many=True)
    return custom_response(ser_users, HTTPStatus.OK)
def delete_user(id):
    user = UserModel.find_by_user(id)
    if user:
        if user == current_user:
            user.delete_user()
            return redirect(url_for('list_user'))
        return redirect(url_for('list_user'))
    return redirect('/list-user')
def update_user(id):
    form = UserFormUpdate(request.form)
    user = UserModel.find_by_user(id)

    if request.method == 'POST' and form.validate() and user == current_user:
        user.username = form.username.data
        user.save_user()
        return redirect(url_for('list_user'))
    return render_template('users/update-user.html', form=form, user=user)
Exemplo n.º 8
0
    def mutate(self, info, **kwargs):
        user = UserModel(**kwargs)
        try:
            user.save()

        except Exception as e:
            raise GraphQLError("Error creating User object.", e)
        else:
            ok = True
            message = "User have been created successfully"
            return RegisterUser(ok=ok, message=message)
Exemplo n.º 9
0
def get_a_user(user_id):
    """
    Get a single user
    """
    user = UserModel.get_one_user(user_id)
    if not user:
        return custom_response({'error': 'user not found'},
                               HTTPStatus.NOT_FOUND)

    ser_user = user_schema.dump(user).data
    return custom_response(ser_user, HTTPStatus.OK)
def login():
    form = LoginForm(request.form)
    if request.method == 'POST' and form.validate():
        user = UserModel.find_email(form.email.data)

        if user.check_hash(request.form['password']) and user:

            #guardando na session
            login_user(user)
            return redirect(url_for('list_publication'))

    return render_template('users/login.html', form=form)
Exemplo n.º 11
0
def update():
    """
    Update me
    """
    req_data = request.get_json()
    data, error = user_schema.load(req_data, partial=True)
    if error:
        return custom_response(error, HTTPStatus.BAD_REQUES)

    user = UserModel.get_one_user(g.user.get('id'))
    user.update(data)
    ser_user = user_schema.dump(user).data
    return custom_response(ser_user, HTTPStatus.OK)
def create_user():
    form = UserForm(request.form)
    if request.method == 'POST' and form.validate():
        new_user = UserModel(username=form.username.data,
                             email=form.email.data,
                             password=form.password.data)

        new_user.hash_password()
        new_user.save_user()
        return redirect(url_for('list_user'))
    return render_template('users/create-user.html', form=form)
Exemplo n.º 13
0
def login():
    """
    User Login Function
    """
    req_data = request.get_json()

    data, error = user_schema.load(req_data, partial=True)
    if error:
        return custom_response(error, HTTPStatus.BAD_REQUEST)
    if not data.get('email') or not data.get('password'):
        return custom_response(
            {'error': 'you need email and password to sign in'},
            HTTPStatus.BAD_REQUEST)
    user = UserModel.get_user_by_email(data.get('email'))
    if not user:
        return custom_response({'error': 'invalid credentials'},
                               HTTPStatus.BAD_REQUEST)
    if not user.check_hash(data.get('password')):
        return custom_response({'error': 'invalid credentials'},
                               HTTPStatus.BAD_REQUEST)
    ser_data = user_schema.dump(user).data
    token = Auth.generate_token(ser_data.get('id'))
    return custom_response({'jwt_token': token}, HTTPStatus.OK)