Ejemplo n.º 1
0
def create_sensor_data(sensor_id):
    ''' Attempt to create a new sensor data entry. '''
    # Ensure the associated sensor exists.
    _ = Sensor.query.filter(
        Sensor.id == sensor_id,
        Sensor.deleted == None,
    ).first_or_404()

    # Ensure the sensor value is in the correct format.
    document = request.get_json()
    value = document.get('value')
    if type(value) != float:
        raise exceptions.InvalidClientRequest('Value must be a float!')

    # Create a new sensor data entry from the provided payload.
    candidate = SensorData(
        value=value,
        sensor_id=sensor_id,
        created=to_datetime(document.get('created'))
    )
    db.session.add(candidate)

    try:
        db.session.commit()
    except sqlalchemy.exc.IntegrityError:
        raise exceptions.InternalServerError('Unable to create sensor data')

    # Confirm addition with an HTTP 201.
    response = jsonify()
    response.status_code = 201
    return response
Ejemplo n.º 2
0
def update_plant(plant_id):
    ''' Attempt to update a given plant. '''
    candidate = Plant.query.filter(Plant.id == plant_id, ).first_or_404()

    # Map in all fields that can be modified.
    document = request.get_json()
    if document.get('name'):
        candidate.name = document.get('name')
    if document.get('description'):
        candidate.description = document.get('description')

    # Ensure the provided vessel exists, or 404.
    if document.get('vessel'):
        _ = Vessel.query.filter(
            Vessel.id == document.get('vessel'), ).first_or_404()
        candidate.vessel_id = document.get('vessel')

    try:
        db.session.commit()
    except sqlalchemy.exc.IntegrityError:
        raise exceptions.InternalServerError('Unable to update plant')

    # Confirm update with an HTTP 204.
    response = jsonify()
    response.status_code = 204
    return response
Ejemplo n.º 3
0
def create_plant():
    ''' Attempt to create a plant. '''
    document = request.get_json()

    # Ensure the provided vessel exists, or 404.
    vessel = Vessel.query.filter(
        Vessel.id == document.get('vessel'), ).first_or_404()

    # Create a new plant from the provided payload.
    candidate = Plant(
        name=document.get('name'),
        vessel_id=vessel.id,
        description=document.get('description'),
    )
    db.session.add(candidate)

    try:
        db.session.commit()
    except sqlalchemy.exc.IntegrityError:
        raise exceptions.InternalServerError('Unable to create plant')

    # Return the newly created vessel to the user.
    # TODO: Perhaps reference the account in the HTTP 'Location' header, rather
    #       than returning a body on the HTTP 201?
    response = jsonify(candidate.for_json())
    response.status_code = 201
    return response
Ejemplo n.º 4
0
def delete_vessel(vessel_id):
    ''' Attempt to delete a given project. '''
    candidate = Vessel.query.filter(
        Vessel.id == vessel_id,
        Vessel.deleted == None,
    ).first_or_404()

    # Mark deleted.
    candidate.deleted = datetime.datetime.utcnow()

    try:
        db.session.commit()
    except sqlalchemy.exc.IntegrityError:
        raise exceptions.InternalServerError('Unable to delete vessel')

    # Confirm deletion with an HTTP 204.
    response = jsonify()
    response.status_code = 204
    return response
Ejemplo n.º 5
0
def update_vessel(vessel_id):
    ''' Attempt to update a given vessel. '''
    candidate = Vessel.query.filter(Vessel.id == vessel_id, ).first_or_404()

    # Map in all fields that can be modified.
    document = request.get_json()
    if document.get('name'):
        candidate.name = document.get('name')
    if document.get('location'):
        candidate.location = document.get('location')

    try:
        db.session.commit()
    except sqlalchemy.exc.IntegrityError:
        raise exceptions.InternalServerError('Unable to update vessel')

    # Confirm update with an HTTP 204.
    response = jsonify()
    response.status_code = 204
    return response
Ejemplo n.º 6
0
def delete_sensor_category(sensor_category_id):
    ''' Attempt to delete a given sensor category. '''
    candidate = SensorCategory.query.filter(
        SensorCategory.id == sensor_category_id,
        SensorCategory.deleted == None,
    ).first_or_404()

    # Mark deleted.
    candidate.deleted = datetime.datetime.utcnow()

    try:
        db.session.commit()
    except sqlalchemy.exc.IntegrityError:
        raise exceptions.InternalServerError(
            'Unable to delete sensor category')

    # Confirm deletion with an HTTP 204.
    response = jsonify()
    response.status_code = 204
    return response
Ejemplo n.º 7
0
def update_sensor_category(sensor_category_id):
    ''' Attempt to update a given sensor. '''
    candidate = SensorCategory.query.filter(
        SensorCategory.id == sensor_category_id, ).first_or_404()

    # Map in all fields that can be modified.
    document = request.get_json()
    if document.get('name'):
        candidate.name = document.get('name')
    if document.get('units'):
        candidate.units = document.get('units')

    try:
        db.session.commit()
    except sqlalchemy.exc.IntegrityError:
        raise exceptions.InternalServerError(
            'Unable to update sensor category')

    # Confirm update with an HTTP 204.
    response = jsonify()
    response.status_code = 204
    return response
Ejemplo n.º 8
0
def create_vessel():
    ''' Attempt to create a vessel. '''
    document = request.get_json()

    # Create a new project from the provided payload.
    candidate = Vessel(
        name=document.get('name'),
        size=VesselSize.POT_TWELVE_CM,
        location=document.get('location'),
    )
    db.session.add(candidate)

    try:
        db.session.commit()
    except sqlalchemy.exc.IntegrityError:
        raise exceptions.InternalServerError('Unable to create vessel')

    # Return the newly created vessel to the user.
    # TODO: Perhaps reference the account in the HTTP 'Location' header, rather
    #       than returning a body on the HTTP 201?
    response = jsonify(candidate.for_json())
    response.status_code = 201
    return response
Ejemplo n.º 9
0
def create_sensor_category():
    ''' Attempt to create a sensor category. '''
    document = request.get_json()

    # Create a new sensor category from the provided payload.
    candidate = SensorCategory(
        name=document.get('name'),
        units=document.get('units'),
    )
    db.session.add(candidate)

    try:
        db.session.commit()
    except sqlalchemy.exc.IntegrityError:
        raise exceptions.InternalServerError(
            'Unable to create sensor category')

    # Return the newly created sensor category to the user.
    # TODO: Perhaps reference the account in the HTTP 'Location' header, rather
    #       than returning a body on the HTTP 201?
    response = jsonify(candidate.for_json())
    response.status_code = 201
    return response