Exemplo n.º 1
0
def removeRole():
    try:
        claims = get_jwt()
        if claims["role"] == "admin":
            data = request.json
            required_keys = ['user_id', 'role_id']
            if not has_required_keys(data, required_keys):
                return get_client_error_response(
                    message='Invalid request. Fields required ({})'.format(
                        required_keys),
                    error_code=422)
            user_details = User.find_by_id(data.get('user_id'))
            role_details = Role.find_by_id(data.get('user_id'))
            if user_details is None or role_details is None:
                return get_error_response(message='Invalid user details')
            user_details.roles.remove(role_details)
            db.session.commit()
            return get_success_response(message='Role has been assigned',
                                        response_code=200)
        else:
            return get_client_error_response(message='Unauthorize',
                                             error_code=401)
    except Exception as e:
        print(e)
        return get_server_error_response()
Exemplo n.º 2
0
def create():
    try:
        claims = get_jwt()
        if claims["role"] == "admin":
            data = request.json
            required_keys = ['first_name', 'last_name', 'email']
            if not has_required_keys(data, required_keys):
                return get_client_error_response(
                    message='Invalid request. Fields required ({})'.format(
                        required_keys),
                    error_code=422)
            if not is_valid_email(data.get('email')):
                return get_client_error_response(
                    message='Invalid email ({})'.format(data.get('email')))
            exists = User.find_by_email(data.get('email'))
            if exists is not None:
                return {
                    'message':
                    'Email {} already exists'.format(data.get('email'))
                }
            password = User.generate_hash('password')
            new_user = User(email=data.get('email'),
                            first_name=data.get('first_name'),
                            last_name=data.get('last_name'),
                            password=password,
                            user_type=2)
            new_user.save_to_db()
            return get_success_response(message='User has been created',
                                        response_code=201)
        else:
            return get_client_error_response(message='Unauthorize',
                                             error_code=401)
    except Exception as e:
        print(e)
        return get_server_error_response()
Exemplo n.º 3
0
def login():
    try:
        data = request.json
        required_keys = ['email', 'password']
        if not has_required_keys(data, required_keys):
            return get_client_error_response(
                message='Invalid request. Fields required ({})'.format(
                    required_keys),
                error_code=422)
        if not is_valid_email(data.get('email')):
            return get_client_error_response(
                message='Invalid email ({})'.format(data.get('email')))
        admin_user_details = User.query.filter_by(email=data.get('email'),
                                                  user_type=1).first()
        if admin_user_details is None:
            return get_client_error_response(message='Invalid credentials!',
                                             error_code=401)
        if admin_user_details.deleted_at is not None:
            return get_client_error_response(message='Invalid credentials!',
                                             error_code=401)
        status = User.verify_hash(data.get('password'),
                                  admin_user_details.password)
        if status == False:
            return get_client_error_response(message='Invalid credentials!',
                                             error_code=401)
        additional_claims = {"role": "admin"}
        access_token = create_access_token(admin_user_details,
                                           additional_claims=additional_claims)
        admin_user_details.access_token = access_token
        db.session.commit()
        return get_success_response(message='Login successful',
                                    data=admin_user_details.to_json())
    except Exception as e:
        print(e)
        return get_server_error_response()
Exemplo n.º 4
0
def share_price():
    try:
        data = request.json
        required_keys = ['share_name']
        if not has_required_keys(data, required_keys):
            return get_client_error_response(message='Invalid request. Fields required ({})'.format(required_keys), error_code=422)
        yahoo = Share(data.get('share_name'))
        price = yahoo.get_price()
        print(price)
        # resp = {}
        # resp['share_name'] = data.get('share_name')
        # resp['price']  = price
        # return get_success_response(message='Price details', data=resp)
    except Exception as e:
        print(e)
        return get_server_error_response()
Exemplo n.º 5
0
def create():
    try:
        claims = get_jwt()
        if claims["role"] == "admin":
            data = request.json
            required_keys = ['role_name', 'description']
            if not has_required_keys(data, required_keys):
                return get_client_error_response(
                    message='Invalid request. Fields required ({})'.format(
                        required_keys),
                    error_code=422)
            new_role = Role(role_name=data.get('role_name'),
                            description=data.get('description'))
            new_role.save_to_db()
            return get_success_response(message='Role has been created',
                                        response_code=201)
        else:
            return get_client_error_response(message='Unauthorize',
                                             error_code=401)
    except Exception as e:
        print(e)
        return get_server_error_response()