Beispiel #1
0
def set_flight_meta_values(flight_id):
    """ Update the meta data for the flight

    Args:
        flight_id (int):            The flight id

    Returns:
        dict:       Api response
        int:        Api response code (optional) (default: 200)
        dict:       Api response headers (optional) (default: {})
    """

    params = api.get_params()

    # load the flight
    flight = Flight.load(flight_id)

    # error if flight doesn't exist
    if flight is None:
        raise ApiException('flight could not be found.')

    for key, value in params.iteritems():
        flight.setMeta(key, value)

    flight.save()

    return {'success': 'Flight meta updated.'}
Beispiel #2
0
def get_flight_data_paged(flight_id):
    """ Get the data for a flight in paged format

    Args:
        flight_id (int): 			The flight id

    Returns:
        dict:		Api response
        int:		Api response code (optional) (default: 200)
        dict:		Api response headers (optional) (default: {})
    """

    params = api.get_params()

    page = params['page'] if 'page' in params else 1
    per_page = params['per_page'] if 'per_page' in params else 20

    # error if flight not found
    flight = Flight.load(flight_id)
    if flight is None:
        raise ApiException('Flight not found.')

    paged_data = flight.data.paginate(page=page,
                                      per_page=per_page,
                                      error_out=False)

    return {
        'pages': paged_data.pages,
        'total': paged_data.total,
        'page': paged_data.page,
        'per_page': paged_data.per_page,
        'data': [data.asApiDict() for data in paged_data.items]
    }
Beispiel #3
0
def get_flight_meta(flight_id):
    """ Get the meta data for the flight

    Args:
        flight_id (int):            The flight id

    Returns:
        dict:       Api response
        int:        Api response code (optional) (default: 200)
        dict:       Api response headers (optional) (default: {})
    """

    # get the request params
    params = api.get_params(require=['keys'])

    if not isinstance(params['keys'], list):
        raise ApiException('keys must be provided as an array')

    # load the flight
    flight = Flight.load(flight_id)

    # error if flight doesn't exist
    if flight is None:
        raise ApiException('Flight could not be found.')

    return {
        meta.key: flight.getMeta(meta.key)
        for meta in flight.meta if meta.key in params['keys']
    }
Beispiel #4
0
def update_user(user_id):
    """ Update an existing user

    Args:
        user_id (int): 		The id of the user being updated

    Returns:
        dict:		Api response
        int:		Api response code (optional) (default: 200)
        dict:		Api response headers (optional) (default: {})
    """

    # get request parameters
    params = api.get_params()

    # get the user
    user = User.load(user_id)
    if user is None:
        raise ApiException(api.error('User not found.'))

    # error if an updated username is already in use
    if 'username' in params and User.query.filter(
            User.username == params['username'],
            User.id != user_id).count() > 0:
        raise ApiException(api.error('Username already in use.', 409))

    # update the user attributes
    for prop, val in user.filterParams(params,
                                       exclude=['id', 'created',
                                                'modified']).iteritems():
        setattr(user, prop, val)

    user.save()

    return {'success': 'User updated.'}
Beispiel #5
0
def create_flight_data(flight_id):
    """ Create a new flight data point

    Returns:
        dict:		Api response
        int:		Api response code (optional) (default: 201)
        dict:		Api response headers (optional) (default: {})
    """

    user = api.getUser()

    if user.id != 1:
        raise ApiException('Not authorized.')

    # error if flight not found
    flight = Flight.load(flight_id)
    if flight is None:
        raise ApiException('Flight not found.')

    # get request parameters
    params = api.get_params()

    data = FlightDataPoint().filterParams(params)

    # create the flight
    datapoint = FlightDataPoint(**data)
    datapoint.flight = flight
    datapoint.save()

    return {'success': 'Flight data point created.'}, 201
Beispiel #6
0
def set_user_meta_values(user_id):
    """ Update the meta data for the user

    Args:
        user_id (int):            The user id

    Returns:
        dict:       Api response
        int:        Api response code (optional) (default: 200)
        dict:       Api response headers (optional) (default: {})
    """

    params = api.get_params()

    # load the user
    user = User.load(user_id)

    # error if user doesn't exist
    if user is None:
        raise ApiException('User could not be found.')

    for key, value in params.iteritems():
        user.setMeta(key, value)

    user.save()

    return {'success': 'User meta updated.'}
Beispiel #7
0
def get_user_meta(user_id):
    """ Get the meta data for the user

    Args:
        user_id (int):            The user id

    Returns:
        dict:       Api response
        int:        Api response code (optional) (default: 200)
        dict:       Api response headers (optional) (default: {})
    """

    # get the request params
    params = api.get_params(require=['keys'])

    if not isinstance(params['keys'], list):
        raise ApiException('keys must be provided as an array')

    # load the user
    user = User.load(user_id)

    # error if user doesn't exist
    if user is None:
        raise ApiException('User could not be found.')

    return {
        meta.key: user.getMeta(meta.key)
        for meta in user.meta if meta.key in params['keys']
    }
Beispiel #8
0
def delete_user(user_id):
    """ Delete an existing user

    Args:
        user_id (int): 		The id of the user being deleted

    Returns:
        dict:		Api response
        int:		Api response code (optional) (default: 200)
        dict:		Api response headers (optional) (default: {})
    """

    # get request parameters
    params = api.get_params(require=['auth_token'])

    # get the user
    user = User.load(user_id)
    if user is None:
        raise ApiException(api.error('User not found.'))

    if AuthToken.query.filter(
            AuthToken.value == params['auth_token']).count() > 0:

        # delete the user
        #user.delete()
        """ Deleting a user will remove all their linked resources (such as tracks),
        so we should have a hard delete user, and a soft delete user """

        return {'success': False, 'message': 'Not yet implemented.'}

    # user has not been deleted
    raise ApiException(api.error('Not authorized.'))
def login():
    """ Login to application

    Returns:
        dict:		Api response
        int:		Api response code (optional) (default: 200)
        dict:		Api response headers (optional) (default: {})
    """

    from flask_jwt import _jwt

    # get request parameters
    params = api.get_params(require=['username', 'password'])

    # check if username exists
    username_exists = User.query.filter(
        User.username == params['username']).count() > 0
    if not username_exists:
        raise ApiException('Username does not exist.')

    # find user with provided username and password
    user = User.query.filter(User.username == params['username']).first()

    if user is None:
        raise ApiException('User not found.')

    # check for correct password
    if not check_password_hash(user.password, params['password']):
        raise ApiException('Incorrect password.')

    user_data = user.asApiDict()
    user_data['jwt_token'] = api.jwt.jwt_encode_callback(user)

    # remap keys for expected response
    params_to_attributes = {
        'date_created': 'created',
        'date_modified': 'modified'
    }

    for new, old in params_to_attributes.iteritems():
        user_data[new] = user_data.pop(old)

    return user_data
Beispiel #10
0
def create_user():
    """ Create a new user

    Returns:
        dict:		Api response
        int:		Api response code (optional) (default: 200)
        dict:		Api response headers (optional) (default: {})
    """

    # get request parameters
    params = api.get_params(require=['email', 'username', 'password'])

    # error if username already exists
    if User.query.filter(User.username == params['username']).count() > 0:
        raise ApiException(api.error('Username already exists.', 409))

    # error if email already exists
    if User.query.filter(User.email == params['email']).count() > 0:
        raise ApiException(api.error('Email already exists.', 409))

    # error if username matches an existing email address
    if User.query.filter(User.email == params['username']).count() > 0:
        raise ApiException(
            api.error('User with that email address already exists.'))

    data = User().filterParams(
        params, exclude=['id', 'created', 'modified', 'password'])

    # create the user
    user = User(**data)

    # set the user password
    try:
        user.setPassword(params['password'])

    except PasswordException, e:
        raise ApiException(str(e))
Beispiel #11
0
def create_flight():
    """ Create a new flight

    Returns:
        dict:		Api response
        int:		Api response code (optional) (default: 201)
        dict:		Api response headers (optional) (default: {})
    """

    user = api.getUser()

    if user.id != 1:
        raise ApiException('Not authorized.')

    # get request parameters
    params = api.get_params()

    data = Flight().filterParams(params, exclude=['id', 'created'])

    # create the flight
    flight = Flight(**data)
    flight.save()

    return {'success': 'Flight created.', 'flight_id': flight.id}, 201