示例#1
0
def update(table_name, record_id=None, username=None, updates={}):
    """
    Updates a single record (handles GET traffic)
    """
    try:
        if record_id:
            return make_response(
                jsonify(
                    Interactions.update(table_name,
                                        record_id=record_id,
                                        updates=updates)), client.OK)
        else:
            return make_response(
                jsonify(
                    Interactions.update(table_name,
                                        filters={'username': username},
                                        updates=updates)), client.OK)
    except RqlRuntimeError as runtime_err:
        return make_response(jsonify({'Error': runtime_err.message}),
                             client.INTERNAL_SERVER_ERROR)
    except RqlDriverError as rql_err:
        return make_response(jsonify({'Error': rql_err.message}),
                             client.INTERNAL_SERVER_ERROR)
    except TypeError:
        return make_response(jsonify({'Error': 'Invalid parameter(s)'}),
                             client.BAD_REQUEST)
示例#2
0
def update(table_name, record_id=None, username=None, updates={}):
    """
    Updates a single record (handles GET traffic)
    """
    try:
        if record_id:
            return make_response(
                jsonify(Interactions.update(
                    table_name, record_id=record_id, updates=updates)),
                client.OK)
        else:
            return make_response(
                jsonify(Interactions.update(
                    table_name,
                    filters={'username': username},
                    updates=updates)
                ), client.OK)
    except RqlRuntimeError as runtime_err:
        return make_response(jsonify({'Error': runtime_err.message}),
                             client.INTERNAL_SERVER_ERROR)
    except RqlDriverError as rql_err:
        return make_response(jsonify({'Error': rql_err.message}),
                             client.INTERNAL_SERVER_ERROR)
    except TypeError:
        return make_response(
            jsonify({'Error': 'Invalid parameter(s)'}), client.BAD_REQUEST)
示例#3
0
def reset_key(username, key_type):
    """
    Resets either a secret key or api key
    """
    try:
        # Note: The validate_username_in_header decorator will verify the
        # username and record. The api_key_restricted_resource will validate
        # the username as well as a valid API key
        record = Interactions.query(DEFAULT_ACCOUNTS_TABLE,
                                    filters={"username": username})
        endpoint = record[0]['endpoint']

        if not endpoint:
            return make_response(
                jsonify({'Error': 'Endpoint not found'}),
                client.NOT_FOUND
            )

        new_key = common.generate_key()
        salted_new_key = generate_password_hash(new_key)

        if not client_reset_key(endpoint, key_type, new_key):
            return make_response(
                jsonify({'Error': 'Failed to contact the endpoint or wrong '
                                  'HTTP status code returned'}),
                client.BAD_REQUEST
            )

        if key_type == 'api_key':
            update = {key_type: salted_new_key}
        else:
            update = {key_type: new_key}

        Interactions.update(DEFAULT_ACCOUNTS_TABLE,
                            filters={"username": username},
                            updates=update)

        return make_response(jsonify({'Message': 'New key sent to endpoint'}),
                             client.OK)
    except RqlRuntimeError as runtime_err:
        return make_response(jsonify({'Error': runtime_err.message}),
                             client.INTERNAL_SERVER_ERROR)
    except RqlDriverError as rql_err:
        return make_response(jsonify({'Error': rql_err.message}),
                             client.INTERNAL_SERVER_ERROR)
示例#4
0
def reset_key(username, key_type):
    """
    Resets either a secret key or api key
    """
    try:
        # Note: The validate_username_in_header decorator will verify the
        # username and record. The api_key_restricted_resource will validate
        # the username as well as a valid API key
        record = Interactions.query(DEFAULT_ACCOUNTS_TABLE,
                                    filters={"username": username})
        endpoint = record[0]['endpoint']

        if not endpoint:
            return make_response(jsonify({'Error': 'Endpoint not found'}),
                                 client.NOT_FOUND)

        new_key = common.generate_key()
        salted_new_key = generate_password_hash(new_key)

        if not client_reset_key(endpoint, key_type, new_key):
            return make_response(
                jsonify({
                    'Error':
                    'Failed to contact the endpoint or wrong '
                    'HTTP status code returned'
                }), client.BAD_REQUEST)

        if key_type == 'api_key':
            update = {key_type: salted_new_key}
        else:
            update = {key_type: new_key}

        Interactions.update(DEFAULT_ACCOUNTS_TABLE,
                            filters={"username": username},
                            updates=update)

        return make_response(jsonify({'Message': 'New key sent to endpoint'}),
                             client.OK)
    except RqlRuntimeError as runtime_err:
        return make_response(jsonify({'Error': runtime_err.message}),
                             client.INTERNAL_SERVER_ERROR)
    except RqlDriverError as rql_err:
        return make_response(jsonify({'Error': rql_err.message}),
                             client.INTERNAL_SERVER_ERROR)
def update_failed_count(account_id=None, increment_failed_count=False):
    """
    Update the failed_count field on the user's account
    """
    try:
        # Get the failed_count value
        account_record = Interactions.get(
            DEFAULT_ACCOUNTS_TABLE, record_id=account_id)
        failed_count = int(account_record['failed_count'])

        if increment_failed_count:
            failed_count += 1
        else:
            failed_count = 0

        Interactions.update(DEFAULT_ACCOUNTS_TABLE, record_id=account_id,
                            updates={'failed_count': failed_count})
    except (RqlRuntimeError, RqlDriverError, Exception):
        pass
示例#6
0
def update_failed_count(account_id=None, increment_failed_count=False):
    """
    Update the failed_count field on the user's account
    """
    try:
        # Get the failed_count value
        account_record = Interactions.get(DEFAULT_ACCOUNTS_TABLE,
                                          record_id=account_id)
        failed_count = int(account_record['failed_count'])

        if increment_failed_count:
            failed_count += 1
        else:
            failed_count = 0

        Interactions.update(DEFAULT_ACCOUNTS_TABLE,
                            record_id=account_id,
                            updates={'failed_count': failed_count})
    except (RqlRuntimeError, RqlDriverError, Exception):
        pass