Beispiel #1
0
def update(device_id, device):
    """
    This function updates an existing device in the devices structure
    Throws an error if a device with the name we want to update to
    already exists in the database.
    :param device_id:   Id of the device to update in the people structure
    :param device:      device to update
    :return:            updated device structure
    """
    update_device = Device.query\
        .filter(Device.device_id == device_id).one_or_none()
    model_num = device.get("model_num")
    manufacturer = device.get("manufacturer")
    existing_device = Device.query\
        .filter(Device.model_num == model_num)\
        .filter(Device.manufacturer == manufacturer)\
        .one_or_none()
    if update_device is None:
        abort(404, f"Device not found for Id: {device_id}")
    elif (existing_device is not None
          and existing_device.device_id != device_id):
        abort(409, f"Device {model_num} {manufacturer} exists already")
    else:
        schema = DeviceSchema()
        new_device = schema.load(device, session=db.session).data
        new_device.device_id = update_device.device_id
        db.session.merge(new_device)
        db.session.commit()
        data = schema.dump(update_device).data
        return data, 200
Beispiel #2
0
def read_one(id_device):
    """
    This function responds to a request for /api/device/{id_user}
    with one matching user from settings

    :param id_user:   Id of user to find
    :return:            user matching id
    """
    # Get the person requested
    query = Device.query.filter(Device.id == id_device).one_or_none()

    # Did we find a person?
    if query is not None:

        # Serialize the data for the response
        schema = DeviceSchema()
        data = schema.dump(query).data
        return data

    # Otherwise, nope, didn't find that person
    else:
        abort(
            404,
            "Device not found for Id: {id_user}".format(id_user=id_user),
        )
Beispiel #3
0
def read_all():
    """
    This function responds to a request for /api/devices
    with the complete lists of devices
    :return:        json string of list of devices
    """
    devices = Device.query.order_by(Device.manufacturer).all()
    device_schema = DeviceSchema(many=True)
    data = device_schema.dump(devices).data
    return data
Beispiel #4
0
def read_one(device_id):
    """
    This function responds to a request for /api/devices/{device_id}
    with one matching device from devices
    :param device_id:   Id of device to find
    :return:            device matching id
    """
    device = Device.query.filter(Device.device_id == device_id).one_or_none()
    if device is not None:
        device_schema = DeviceSchema()
        data = device_schema.dump(device).data
        return data
    else:
        abort(404, f"Device not found for Id: {device_id}")
Beispiel #5
0
def read_all():
    """
    This function responds to a request for /api/device
    with the complete lists of users

    :return:        json string of list of devices
    """
    # Create the list of people from our data
    query = Device.query.order_by(Device.id_user).all()

    # Serialize the data for the response
    schema = DeviceSchema(many=True)
    data = schema.dump(query).data
    return data
Beispiel #6
0
def create(device):
    """
    This function creates a new device in the devices structure
    based on the passed in device data
    :param device:  device to create in devices structure
    :return:        201 on success, 406 on device exists
    """
    model_num = device.get("model_num")
    manufacturer = device.get("manufacturer")
    existing_device = (Device.query.filter(
        Device.model_num == model_num).filter(
            Device.manufacturer == manufacturer).one_or_none())
    if existing_device is None:
        schema = DeviceSchema()
        new_device = schema.load(device, session=db.session).data
        db.session.add(new_device)
        db.session.commit()
        data = schema.dump(new_device).data
        return data, 201
    else:
        abort(409, f"Device {model_num} {manufacturer} exists already")
Beispiel #7
0
def update(id_device, device):
    """
    This function updates an existing user in the structure

    :param id_device:   id of the device to update in the default structure
    :param device:   device to update
    :return:       updated device structure
    """
    # Get the person requested from the db into session
    update_device = Device.query.filter(Device.id == id_device).one_or_none()

    # Did we find a device?
    if update_device is not None:
        update_device.updated = datetime.utcnow()
        update_device.bucket = device['bucket']
        update_device.frame_rate = device['frame_rate']
        update_device.high_threshold = device['high_threshold']
        update_device.id_user = device['id_user']
        update_device.image = device['image']
        update_device.location = device['location']
        update_device.low_threshold = device['low_threshold']
        update_device.name = device['name']
        update_device.notes = device['notes']
        update_device.prediction = device['prediction']
        update_device.refresh_rate = device['refresh_rate']
        update_device.type = device['type']

        db.session.commit()

        schema = DeviceSchema()
        data = schema.dump(update_device).data

        return data, 200

    # Otherwise, nope, didn't find that person
    else:
        abort(
            404,
            "Device not found for Id: {id_device}".format(id_device=id_device),
        )
Beispiel #8
0
def create(device):
    """
    This function creates a new entry in the device structure
    based on the passed in user id data

    :param user_name:  user name to create in user structure
    :param display_name:  display name for the user
    :param company:  the user company
    :param thumbnail:  string url to the thumbnail image

    :return:        201 on success, 406 on default exists
    """
    # Create a person instance using the schema and the passed in person
    schema = DeviceSchema()
    devices = schema.load(device, session=db.session).data

    # Add the person to the database
    db.session.add(devices)
    db.session.commit()

    # Serialize and return the newly created person in the response
    data = schema.dump(devices).data

    return data, 201