Exemple #1
0
def delete_events(_, snapshot: dict):
    """
    Deletes the events that were created with the snapshot.
    """
    if snapshot.get('@type') == 'devices:Snapshot':
        for event_id in snapshot['events']:
            try:
                # If the first event is Register, erasing the device will erase the rest of events
                event = DeviceEventDomain.get_one(event_id)
            except EventNotFound:
                pass
            else:
                try:
                    execute_delete(Naming.resource(event['@type']), event['_id'])
                except InnerRequestError as e:
                    if e.status_code != 404:
                        raise e
Exemple #2
0
def delete_events(_, snapshot: dict):
    """
    Deletes the events that were created with the snapshot.
    """
    if snapshot.get('@type') == 'devices:Snapshot':
        for event_id in snapshot['events']:
            try:
                # If the first event is Register, erasing the device will erase the rest of events
                event = DeviceEventDomain.get_one(event_id)
            except EventNotFound:
                pass
            else:
                try:
                    execute_delete(Naming.resource(event['@type']),
                                   event['_id'])
                except InnerRequestError as e:
                    if e.status_code != 404:
                        raise e
Exemple #3
0
def check_migrate(_, resource: dict):
    """
    Raises an exception if any of the device(s) in the resource is in another database, as a result of a Migrate event.

    This is done to avoid adding/modifying/deleting events or places that have a relationship with a device that
    is not legally bound in a DeviceHub.

    The method checks if the last Migrate has a 'to' field, meaning the device is in another database and has not
    come back.
    :raises DeviceHasMigrated
    """
    devices = ([resource['device']] if 'device' in resource else []) + resource.get('devices', [])
    for device_id in devices:
        with suppress(EventNotFound):
            # todo can it be done with only one access to the DB for all devices (optimization)?
            # Note that this is executed for every post / delete /update / patch, resulting in queries = n of devices
            query = {'@type': Migrate.type_name, 'devices': {'$in': [device_id]}}
            last_migrate = DeviceEventDomain.get_one({'$query': query, '$orderby': {'_created': pymongo.DESCENDING}})
            if 'to' in last_migrate:
                raise DeviceHasMigrated(device_id, last_migrate)
Exemple #4
0
def check_migrate(_, resource: dict):
    """
    Raises an exception if any of the device(s) in the resource is in another database, as a result of a Migrate event.

    This is done to avoid adding/modifying/deleting events or places that have a relationship with a device that
    is not legally bound in a DeviceHub.

    The method checks if the last Migrate has a 'to' field, meaning the device is in another database and has not
    come back.
    :raises DeviceHasMigrated
    """
    devices = ([resource['device']] if 'device' in resource else []) + resource.get('devices', [])
    for device_id in devices:
        with suppress(EventNotFound):
            # todo can it be done with only one access to the DB for all devices (optimization)?
            # Note that this is executed for every post / delete /update / patch, resulting in queries = n of devices
            query = {'@type': Migrate.type_name, 'devices': {'$in': [device_id]}}
            last_migrate = DeviceEventDomain.get_one({'$query': query, '$orderby': {'_created': pymongo.DESCENDING}})
            if 'to' in last_migrate:
                raise DeviceHasMigrated(device_id, last_migrate)