コード例 #1
0
def create_orm_override(attr, orm_device, orm_template):
    try:
        target = int(attr['id'])
    except ValueError:
        LOGGER.error(f" Unknown attribute {attr['id']} in override list")
        raise HTTPRequestError(
            400, 'Unknown attribute {} in override list'.format(attr['id']))

    found = False
    for orm_attr in orm_template.attrs:
        if target == orm_attr.id:
            found = True
            if 'static_value' in attr and attr['static_value'] is not None:
                orm_override = DeviceOverride(
                    device=orm_device,
                    attr=orm_attr,
                    static_value=attr['static_value'])
                db.session.add(orm_override)
                LOGGER.debug(f" Added overrided form {orm_override}")

            # Update possible metadata field
            if 'metadata' in attr:
                for metadata in attr['metadata']:
                    try:
                        metadata_target = int(metadata['id'])
                        LOGGER.debug(f" Updated metadata {metadata_target}")
                    except ValueError:
                        LOGGER.error(
                            f" metadata attribute {attr['id']} in override list"
                        )
                        raise HTTPRequestError(
                            400,
                            'Unknown metadata attribute {} in override list'.
                            format(metadata['id']))

                    found = False
                    # WARNING: Adds no-autoflush here, without it metadata override fail during device update
                    with db.session.no_autoflush:
                        for orm_attr_child in orm_attr.children:
                            if metadata_target == orm_attr_child.id:
                                found = True
                                if 'static_value' in metadata and metadata[
                                        'static_value'] is not None:
                                    orm_override = DeviceOverride(
                                        device=orm_device,
                                        attr=orm_attr_child,
                                        static_value=metadata['static_value'])
                                    db.session.add(orm_override)
                                    LOGGER.debug(
                                        f" Added overrided form {orm_override}"
                                    )

    if not found:
        LOGGER.error(f" Unknown attribute {attr['id']} in override list")
        raise HTTPRequestError(
            400, 'Unknown attribute {} in override list'.format(target))
コード例 #2
0
def auto_create_template(json_payload, new_device):
    if ('attrs' in json_payload) and (new_device.templates is None):
        device_template = DeviceTemplate(label="device.%s template" %
                                         new_device.id)
        db.session.add(device_template)
        new_device.templates = [device_template]
        load_attrs(json_payload['attrs'], device_template, DeviceAttr, db)

    # TODO: perhaps it'd be best if all ids were random hex strings?
    if ('attrs' in json_payload) and (new_device.templates is not None):
        for attr in json_payload['attrs']:
            orm_template = find_template(new_device.templates,
                                         attr['template_id'])
            if orm_template is None:
                raise HTTPRequestError(
                    400, 'Unknown template "{}" in attr list'.format(template))

            target = int(attr['id'])
            found = False
            for orm_attr in orm_template.attrs:
                if target == orm_attr.id:
                    found = True
                    orm_override = DeviceOverride(
                        device=new_device,
                        attr=orm_attr,
                        static_value=attr['static_value'])
                    db.session.add(orm_override)
            if not found:
                raise HTTPRequestError(
                    400,
                    "Unkown attribute \"{}\" in override list".format(target))